How to get “transcript” in youtube-api v3

前端 未结 4 940
迷失自我
迷失自我 2020-12-07 18:14

I have started using v3 of the YouTube apis on an android device, using the java client library. Some videos that I am interested in have transcripts that I can access on th

4条回答
  •  悲&欢浪女
    2020-12-07 18:43

    Heres some code I wrote which grabs all the caption tracks from any youtube video without having to use the API. Just plug the video URL in the $video_url variable.

    // get video id from url
    $video_url = 'https://www.youtube.com/watch?v=kYX87kkyubk';
    preg_match("#(?<=v=)[a-zA-Z0-9-]+(?=&)|(?<=v\/)[^&\n]+(?=\?)|(?<=v=)[^&\n]+|(?<=youtu.be/)[^&\n]+#", $video_url, $matches);
    
    // get video info from id
    $video_id = $matches[0];
    $video_info = file_get_contents('http://www.youtube.com/get_video_info?&video_id='.$video_id);
    parse_str($video_info, $video_info_array);
    
    if (isset($video_info_array['caption_tracks'])) {
        $tracks = explode(',', $video_info_array['caption_tracks']);
    
        // print info for each track (including url to track content)
        foreach ($tracks as $track) {
            parse_str($track, $output);
            print_r($output);
        }
    }
    

提交回复
热议问题