How can I search inside a youtube playlist?

随声附和 提交于 2019-12-03 07:47:56
asashnov

For doing full-text search in closed captions (CC) in a Youtube channel you may download all the subtitles from the channel and do local text search in it:

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

that will download about 160 .vtt text files

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

Note: Option --write-auto-sub should be used when author of videos didn't upload text. Otherwise use --write-sub.

I have read the YouTuBe API documentation regarding requests on playlists and they don't provide such functionality, however you could use this npm module to achieve that:

npm install youtube-playlist-search

or

yarn install youtube-playlist-search

The way to use it is something like this on 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);
});

Or you can also take a look here where I'm using it with React, the code it will be something like:

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;

Unfortunately youtube data API 3 does not support searching inside a playlist, but as a work around we can add a string to user's search so that the results will be near to what is needed, in my case for example I have prefixed all the video titles in a specific playlist with the name of that playlist and then I had to prefix users search query with the specified playlist name, the only drawback is that I had to rename all of the videos to include the prefix.

playlist name: exotic cars

search query: exotic cars + user input

And don't forget to search channel videos only.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!