How can programmatically get subtitles of a playing YouTube video?
Initially I've tried to do it offline via YouTube API, but as it seems YouTube forbids to fetch subtitles of videos you are not the owner.
Now I'm trying to do it online. I haven't found YouTube Player Api methods for captions, also I've tried to get YouTube captions as TextTrack with videojs player in the way it could be done for usual videos, but the following doesn't work:
<html>
<head>
<link href="//vjs.zencdn.net/4.12/video-js.css" rel="stylesheet">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script type="text/javascript" src="//vjs.zencdn.net/4.12/video.js"></script>
<script type="text/javascript" src="../lib/youtube.js"></script>
</head>
<body>
<video id="myvideo"
class="video-js vjs-default-skin vjs-big-play-centered"
controls
preload="auto"
width="640"
height="360">
</video>
<script type="text/javascript">
var myvideo = videojs(
"myvideo",
{
"techOrder": ["youtube"],
"src": "https://www.youtube.com/watch?v=jNhtbmXzIaM"
},
function() {
console.log('Tracks: ' + this.textTracks().length); //zero here :(
/*var aTextTrack = this.textTracks()[0];
aTextTrack.on('loaded', function() {
console.log('here it is');
cues = aTextTrack.cues();
console.log('Ready State', aTextTrack.readyState())
console.log('Cues', cues);
});
aTextTrack.show();*/
});
</script>
</body>
</html>
I've also tried an ugly solution with parsing YouTube Player IFrame (there is a div inside it with current subtitles' line), but it doesn't work because of origin mismatch security issues.
Is there any way my goal can be achieved in java (for offline solutions) or javascript (for online solutions)?
THIS SOLUTION STOPPED WORKING
UPDATES: The URL from Google stopped working. This approach is paused at the moment.
Solution: How I managed to get the captions from a youtube video is by making a simple request to this url https://video.google.com/timedtext?lang={LANG}&v={videoId}
I have tried to use the Youtube API v3 but at the moment it doesn't work. When you do a request with the Youtube API v3 on a certain video you need that the person which uploaded the video to approve the caption's download, if not you'll have a 403 error in the console. It's normal to have the error, the server doesn't receive the approval so it returns an error.
You can download the captions from your own video with the Youtube API v3.
Something similar to this will do the job. The response will come in an XML format:
$.ajax({
type: "POST",
url: "https://video.google.com/timedtext?lang=en&v=5MgBikgcWnY"
}).done(function (response) {
console.log(response);
}).fail(function (response) {
console.log();
});