4

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

professor_png
  • 43
  • 1
  • 1
  • 4
  • 1
    You can use Python's `os.system` to run an external command, so: 1. read the `list.txt`, 2. store the data line by line in a list, 3. create a `for` loop iterating the list to run `os.system("youtube-dl.exe -f bestaudio ytsearch:" + item)` – Vinícius Figueiredo Mar 07 '18 at 22:32
  • 1
    [Your answer should not be edited into the question](https://meta.stackoverflow.com/q/319747/35070). Instead, [write an answer yourself](https://stackoverflow.com/help/self-answer). Notice that [as I (as the core developer of youtube-dl) laid out in the official FAQ, both `-c` and `--title` are not generally helpful](https://github.com/rg3/youtube-dl/blob/master/README.md#do-i-always-have-to-pass--citw). youtube-dl will resume by default, `-c` will force resumption even in cases when it breaks. `--title` has no effect. –  Mar 08 '18 at 08:08

2 Answers2

4

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.

  • Hey thanks for this, I was able to use your example to help and another guys to help cobble together a command that works with my specific list. ill add my command and what the flags do for future people with this. Thanks again – professor_png Mar 08 '18 at 07:09
-1

If you were willing to not use python, and as you are on windows, you could just use Powershell to iterate over lines in the file, see Read file line by line in PowerShell

alastairtree
  • 3,960
  • 32
  • 49