如何在YouTube播放列表中搜索?

8
我需要知道youtube API V3是否支持在特定的Youtube播放列表中搜索?是否有其他方法可以实现此功能?
3个回答

4

我已经阅读了有关播放列表请求的YouTuBe API文档,但它们不提供这样的功能。不过,你可以使用这个npm模块来实现:

npm install youtube-playlist-search

或者

yarn install youtube-playlist-search

使用该技术的方式类似于JavaScript上的以下示例:
var search = require('youtube-playlist-search');

var params = {
  part: 'snippet,contentDetails',
  maxResults: 25,
  key: 'your_api_key'
};

search('searchTerm', params, function(err, videos) {
  if(err) return console.log(err);
  console.dir(videos);
});

或者你也可以在这里查看我是如何在React中使用它的,代码会类似于:

import _ from 'lodash';
import YTSearch from 'youtube-playlist-search';
import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';
import SearchBar from './components/search_bar';
import VideoList from './components/video_list';
import VideoDetail from './components/video_detail';

class App extends Component {
  constructor(props) {
    super(props);

    this.state = {
      videos: [],
      selectedVideo: null
    };

    this.key = process.env.REACT_APP_YTB_API_KEY_DEV
    if (process.env.NODE_ENV === 'production') {
      this.key = process.env.REACT_APP_YTB_API_KEY_PROD
    }

    this.params = {
      part: 'snippet,contentDetails',
      playlistId: 'PLH99prTh-VPqO7ld0o2Sny6bLxpf80Js0',
      key: this.key
    };

    this.videoSearch('')
  }

  videoSearch(term) {
    YTSearch(term, this.params, (err, videos) => {
      this.setState({
        videos: videos,
        selectedVideo: videos[0]
      });
    });
  }

  render() {
    const videoSearch = _.debounce((term) => {this.videoSearch(term)}, 300);
    return (
      <div className="App">
        <header className="App-header">
          <img src={logo} className="App-logo" alt="logo" />
          <h1 className="App-title">Welcome to React</h1>
        </header>
        <SearchBar onSearchTermChange={videoSearch}/>
        <VideoDetail video={this.state.selectedVideo}/>
        <VideoList
          onVideoSelect={selectedVideo => this.setState({selectedVideo})}
          videos={this.state.videos}/>
      </div>
    );
  }
}

export default App;

4

若要在Youtube频道中进行闭路字幕(CC)的全文搜索,您可以下载该频道的所有字幕并在本地文本中进行搜索:

$ youtube-dl --write-auto-sub --skip-download "https://www.youtube.com/channel/UC6oh54zIYKyW6hgBiZzLLsA"

将会下载大约160个.vtt文本文件

$ grep -i -C 2 'autumn' *.vtt

注意:当视频的作者没有上传文字时,应使用选项--write-auto-sub。否则,请使用--write-sub

1

很不幸,Youtube数据API 3不支持在播放列表内进行搜索,但是我们可以通过在用户搜索中添加字符串来解决这个问题,以便结果接近所需内容。例如,在我的情况下,我已经在特定的播放列表中给所有视频标题加上了该播放列表的名称前缀,然后我必须在指定的播放列表名称前缀中添加用户搜索查询,唯一的缺点是我必须重命名所有的视频以包含该前缀。

播放列表名称:exotic cars

搜索查询:exotic cars + 用户输入

并且不要忘记仅搜索频道视频。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接