Search code examples
pythonyoutubemp3youtube-dl

how to use youtube-dl search so it can pull out of a list from a .txt file?


I have a firend who gave me a very specific problem, he has a list of plain text words (for purpose of writing i'll just call this list he has list.txt and i'll populate it will basic words) and he wants to pull an audio only file of each word on this list from youtube. So i decided youtube-dl would be the fastest tool.

Since the list.txt is a plain words list and not a list youtube links it makes it harder to download in bulk. After reading all the documents I think the simplest way is to use the search feature built into youtube-dl.

List.txt

  1. blue
  2. river
  3. red
  4. (ETC...)

So basically something that does this. but since there is a lot more than just 3 items this isn't really practical.

youtube-dl.exe -f bestaudio ytsearch:blue
youtube-dl.exe -f bestaudio ytsearch:river
youtube-dl.exe -f bestaudio ytsearch:red

I've been looking all day for something similar to this bit of code below that can take a .txt file and search youtube and download 1 audio/video per item on this list.

youtube-dl.exe -f bestaudio ytsearch:list.txt

I work more with network stuff rather than coding so im kinda out of my depth here and only really have basic coding skills so any help is much appreciated


Solution that ended up working for me

also because i need file conversion i used ffmpeg which has built in support for youtube-dl

youtube-dl -c --title --batch-file test.txt --default-search "ytsearch" -x
  • -c Force resume of partially downloaded files.

  • --title not really sure doesnt seem to make a diffrence anyhow

  • -x as alastairtree pointed out its a better way to get audio

Ps. Thanks to all who tried to help me and my friend solve our problem


Solution

  • If list.txt contains just the words, use the --batch-file (or -a for short option) and set a default search provider (YouTube search in this case):

    youtube-dl -a list.txt --default-search ytsearch -x
    

    Note that I replaced -f bestaudio with -x (or --extract-audio). This has two advantages: It works with videos without dedicated audio streams (extremely rare these days), and it corrects the m4a file so that it can be read on all music players. You can also pass in --audio-format mp3 (or e.g. opus instead of mp3) to get the videos all in a desired output format.