How can I get captions of a youtube video and display it separately?
Asked Answered
A

3

6

I am making a bilingual app to learn English. I will insert a youtube video into the app, and show subtitles below. Is there any way I can get the captions directly from the youtube video player and display it separately?

enter image description here

I see some current apps that are doing that as well. Don't know how they did? Or do I have to manually get the subtitles from each video then save them to the database, then process and display them? But the amount of data will be huge, with tens of millions of videos, and each video has dozens of subtitles in different languages.

enter image description here

I searched a lot but didn't find anyone answering this. There are posts from many years ago that also have no answers. Does anyone have any ideas? Thank you.

Anson answered 27/9, 2022 at 7:13 Comment(1)
find a good solution?Periwig
F
7

After a lot of searching I finally found the solution.

You have to GET the whole page first from the video url (e.g. https://www.youtube.com/watch?v=someID) and then from inside the whole resposne, search for a url like this: https://www.youtube.com/api/timedtext?...

It contains the temporary signature and everything else you need to download the transcript. (DISCLAIMER: haven't tested if the signature expires --> EDIT: it expires in approx. 24h)

After that you just need to extract that whole url - I used Regex - and decode it using decodeURI() method to escape all Unicode characters :)

It should return something like:

enter image description here

Flattie answered 8/11, 2023 at 15:3 Comment(4)
I'm trying the same method but it's giving 404. are you sure that this is working?Inappropriate
Note that you need to un-escape the timed text URL before fetching it, which when I tried it meant replacing all the instances of \u0026 with & .Quaternary
@BrettDonald is right but there is a simpler way... 1. first I extract the whole URL 2. then just use decodeURI() method on that URL and it worksFlattie
:'( After some time of using the described method. YouTube blocks me by IP and the method stops working. I need to look for a proxy.Herwin
C
0

You can use the Youtube API for this:

  1. First retrieve the list of captions using https://developers.google.com/youtube/v3/docs/captions/list
  2. Download the caption from https://developers.google.com/youtube/v3/docs/captions/download

Then you can have access to controls for a media player (and the current time) to display the correct subtitle: see HTMLMediaElement API for this.

Compressor answered 27/9, 2022 at 7:27 Comment(2)
1. Have you read my post carefully? And have you used the youtube API to download captions before giving advice? Firstly, using youtube API I can only get subtitles of videos on my channel, but my aim is to get subtitles of any video on youtube. Second, youtube API has a quota limit, so I won't be able to get subtitles of videos in bulk. From the very beginning, I thought I wouldn't use it. But because of your answer, I wasted a whole day in vain trying the way you said it, and in the end, got no results.Anson
2. I want to get the subtitles of millions of videos, and since the data is very large. So I want to ask, other than how I download the subtitles of each video and save it, and then display it. Is there any way I can get the subtitles from the youtube video player as soon as I insert youtube videos into my app? Simply youtube has subtitles available in the youtube video player already, I just want to display it in another position that I want. That way I won't have to download millions of subtitles and store them. So did you understand my problem?Anson
B
0

You should check the youtube api to get captions :https://developers.google.com/youtube/v3/docs/captions/list?apix_params=%7B%22part%22%3A%22snippet%22%2C%22videoId%22%3A%22PRU2ShMzQRg%22%7D

From there you can have separately captions and video

Bensky answered 27/9, 2022 at 7:27 Comment(2)
1. Have you read my post carefully? And have you used the youtube API to download captions before giving advice? Firstly, using youtube API I can only get subtitles of videos on my channel, but my aim is to get subtitles of any video on youtube. Second, youtube API has a quota limit, so I won't be able to get subtitles of videos in bulk. From the very beginning, I thought I wouldn't use it. But because of your answer, I wasted a whole day in vain trying the way you said it, and in the end, got no results.Anson
2. I want to get the subtitles of millions of videos, and since the data is very large. So I want to ask, other than how I download the subtitles of each video and save it, and then display it. Is there any way I can get the subtitles from the youtube video player as soon as I insert youtube videos into my app? Simply youtube has subtitles available in the youtube video player already, I just want to display it in another position that I want. That way I won't have to download millions of subtitles and store them. So did you understand my problem?Anson

© 2022 - 2024 — McMap. All rights reserved.