sealedtx / java-youtube-downloader

Simple, almost zero-dependency java parser for retrieving youtube video metadata
Other
413 stars 115 forks source link

Add captions/subtitles extraction #36

Closed sealedtx closed 4 years ago

sealedtx commented 4 years ago

Resolves #31. Not sure if subtitles functionality is completely done... Will appreciate if someone can review and share thoughts

xibr commented 4 years ago

hi, In this case, I cannot know the subtitle file type if I do not use the default download provided in the library. I am using android download manager.

If it is possible to check the Extension of the file translation, maybe if you add something like this

    for (SubtitlesInfo info : subtitles) {
                if (info.getLanguage().equals("en") && !info.isAutoGenerated() && info.getExtension().equals("vtt")) {
                    String subtitleUrl = info.getUrl();
                }
            }

I think he misses something like this to check. getExtension()

sealedtx commented 4 years ago

@xibr Actually it could be no format provided in url by youtube, and I don't know which format is default, for most videos I have checked it is "srv1", but I can't be sure that it is default for all. Thank you for your feedback, probably I need to think about improvement api to support external downloaders as fully as default downloader: in such way so you can format/translate subtitle and than download it a way you want

xibr commented 4 years ago

Yes, I thought about it too, I think it would be a good idea.

sealedtx commented 4 years ago

@xibr is it better now? You can use it like:

for (SubtitlesInfo info : subtitles) {
   if (info.getLanguage().equals("en") && !info.isAutoGenerated()) {
      String subtitlesUrl = info.getSubtitles()
                        .formatTo(Extension.WEBVTT)
                        .getDownloadUrl();
   }
}
xibr commented 4 years ago

Yes, it now returns the link and can be easily downloaded.

https://www.youtube.com/api/timedtext?v=9mwRQ96CuHg&asr_langs=de,en,es,fr,it,ja,ko,nl,pt,ru&caps=asr&xorp=true&hl=en&ip=0.0.0.0&ipbits=0&expire=1595725796&sparams=ip,ipbits,expire,v,asr_langs,caps,xorp&signature=BAFB43065BB7F31DD0A5675D821666F506BB9DE2.3D9C56B09D690223B85CEEB7D5CEFB4E8B1C5CD5&key=yt8&lang=en&fmt=vtt

I've tried several videos with subtitles so far and they all work properly.

xibr commented 4 years ago

i have this error when try to download from this video id y9pfCQ5qQYY This video has no subtitles.

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean com.alibaba.fastjson.JSONArray.isEmpty()' on a null object reference
        at com.github.kiulian.downloader.parser.DefaultParser.getSubtitlesInfoFromCaptions(DefaultParser.java:117)
        at com.github.kiulian.downloader.YoutubeDownloader.getVideo(YoutubeDownloader.java:71)
sealedtx commented 4 years ago

@xibr fixed and covered with tests cases when no subtitles

xibr commented 4 years ago

I have tried some videos and it seems to be working fine.