Ubuntu – How to bulk download videos and rename them simultaneously using youtube-dl

16.04command linevideoyoutubeyoutube-dl

I am using Ubuntu 16.04 LTS. I want to download some YouTube videos. I came to know about the command line tool youtube-dl from this question. As mentioned by Yasser, using the command,

youtube-dl youtube.com/videolink --format mp4

I am able to download those video files in mp4 format.

Now I want to download ten videos from different channels. I have a text file with the YouTube links and the names by which I want to save them. The text file with name data.txt looks like,

http://youtube.com/link1    name1
http://youtube.com/link2    name2
http://youtube.com/link3    name3 
.........                   ......
.........                   ......
http://youtube.com/link10    name10

Now I want to run the youtube-dl command only once so that it takes the arguments from that text file automatically and save them with the required names.

If I have only the links in data.txt, i.e.,


I am able to download the videos using the command only once with option -a,

youtube-dl --format mp4 -a data.txt

But I could not rename them simultaneously. From man page of youtube-dl I found that there is an option --output to set the filename.

How could I do that?

Best Answer

If you are only renaming files because you don't like the style by which youtube-dl names them, then you can use --output (or -o) with a template to customize the way it names all the files from Youtube metadata.

For example, -o %(title)s.%(ext)s will cause it to leave out the Youtube IDs from all of the filenames.

There are a lot of options, which may depend on your version of youtube-dl, so full details on this can be found at the terminal by typing:

man youtube-dl

However if you do want to name each file individually, you could instead run youtube-dl once for each line in the text file like this:

(while read URL NAME; do [ "$URL" ] && youtube-dl --format mp4 -o "$NAME" "$URL"; done) < data.txt

For each line, split it at the first run of one or more tabs and spaces into a URL and NAME, then if the line is not blank, pass them to youtube-dl. If the line just has a URL, the file will not be renamed.

Related Question