0

yt-dlp has the ability to search the web for videos from the command line and download the first result, but I'd like to limit the search to results with Closed Captioning (㏄) or subtitles, if available.

For example, the following returns a video without subtitles, which is not what I want:

yt-dlp --embed-subs --default-search=auto '"A year ago last Thursday"'

I know this should be possible as I am able to do the search I want using a web browser by appending &sp=EgIoAQ to the search URL, like so:

How can I do this from the command line using yt-dlp?


Note 1: I only want human-generated, not machine generated, subtitles.

Note 2: --match-filter=requested_subtitles does not work as the filter is performed after the search.

3
  • If that URL is valid, you should be able to just do yt-dlp https://www.youtube.com/results?search_query="A+year+ago+last+Thursday"&sp=EgIoAQ --embed-subs --max-downloads. Not too sure it is though.
    – Destroy666
    Commented Sep 27, 2023 at 6:24
  • 1
    That looks more valid: yt-dlp https://www.youtube.com/results?search_query=%22A+year+ago+last+Thursday%22&sp=EgIoAQ%253D%253D --embed-subs --max-downloads 1
    – Destroy666
    Commented Sep 27, 2023 at 6:29
  • 1
    @Destroy666 Thanks for the suggestion. I built off of it and came up with something that works for me and posted it as an answer. (It turns out --max-downloads 1 doesn't work when --remux-video 'opus>mka' is also used.)
    – hackerb9
    Commented Oct 12, 2023 at 6:54

1 Answer 1

1

Use the youtube.com search URL as a playlist

yt-dlp --embed-subs --playlist-items 1:1 \
  'http://youtube.com/results?search_query=makeba&sp=EgIoAQ%253D%253D' 

That will download just the top result that has closed captions (CC).

Wrapper script for songs/videos

If you feel like that's too much typing, I made a bash script to wrap searching and downloading song audio including the lyrics. You can download it from: https://github.com/hackerb9/getsong/. It is quite easy to use if you have a UNIX box:

getsong -c "A week ago last Thursday"

Note that the script requires the -c option to restrict the search to results with closed captions. (See the Caveats below for why I kept the default off).

If you rename the script getvideo, it will download videos, not just the audio.

Caveats

Note, that this answer does not do precisely what was requested: instead of simply preferring results with closed captioning, this makes captions a requirement. Ideally, we'd get an error message if the desired video is not available with captions, but instead YouTube mangles and ignores the search terms until it finds an incorrect video that does have closed captions.

Another possible issue: I had hoped to get lyrics synced with songs this way, but YouTube doesn't look at the quality or even quantity of the captions, only whether they exist or not. Occasionally, I've hit files which contain only a single subtitle which reads, unhelpfully, "🎜 (singing) 🎝". In those cases, using the 2nd or 3rd result usually helps.



Thanks to @Destroy666 for the suggestion to try using the YouTube search URL as the argument for yt-dlp.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .