Fork me on GitHub
Vijai Kumar S
A Space to share my views on World, Science and People

Make your life much simpler with wget and youtube-dl

It is one thing to download videos from a playlist with just 5 videos, but it is a whole different situation when you have playlists with over 100 videos and in special cases where you want to download an entire channel. There are some canned softwares that claim to do this easily for you, but they are not opensource or even free and some free softwares force you to upgrade if you want to download bigger playlists. Once you start using youtube-dl, there is no looking back. This is mainly because of its awesome community and its support to over 100 sites other than youtube. They constantly update the regex and any other bugs and have a monthly release. You can do stuff like downloading entire channels, downloading playlists, downloading videos at multiple resolutions and what not. It is also good to go for direct downloads. Things you might want to accomplish with wget can easily be substituted with youtube-dl. But wget and curl have an wider scope than that and we won't discuss that here. Let me share a few examples of what you can do with youtube-dl.

If you want to simply download a single video, here are some tips. Let us consider an example video shown below.

If you want to download the above video, you would have to enter the following command in your terminal.

youtube-dl https://www.youtube.com/watch?v=9bZkp7q19f0  

The above command will download the youtube video to your desktop. If you are not sure about the quality of the downloaded video, youtube usually has multiple resolutions of the same video depending upon the quality of the uploaded video. You can simply get a list of format and resolution available using the following command.

youtube-dl -F https://www.youtube.com/watch?v=9bZkp7q19f0  

The above command should return something similar to the output shown below.

[youtube] 9bZkp7q19f0: Downloading webpage
[youtube] 9bZkp7q19f0: Downloading video info webpage
[youtube] 9bZkp7q19f0: Extracting video information
[youtube] 9bZkp7q19f0: Downloading DASH manifest
[info] Available formats for 9bZkp7q19f0:
format code  extension  resolution note  
249          webm       audio only DASH audio , opus @ 50k  
250          webm       audio only DASH audio , opus @ 70k  
251          webm       audio only DASH audio , opus @160k  
140          m4a        audio only DASH audio  127k , m4a_dash container, aac  @128k (44100Hz), 3.82MiB  
171          webm       audio only DASH audio  128k , vorbis@128k (44100Hz), 3.49MiB  
141          m4a        audio only DASH audio  255k , m4a_dash container, aac  @256k (44100Hz), 7.67MiB  
278          webm       256x144    DASH video  100k , webm container, vp9, 1fps, video only, 2.57MiB  
160          mp4        256x144    DASH video  111k , avc1.4d400c, 12fps, video only, 3.28MiB  
242          webm       426x240    DASH video  217k , vp9, 1fps, video only, 5.53MiB  
133          mp4        426x240    DASH video  248k , avc1.4d4015, 24fps, video only, 7.37MiB  
243          webm       640x360    DASH video  396k , vp9, 1fps, video only, 10.37MiB  
134          mp4        640x360    DASH video  622k , avc1.4d401e, 24fps, video only, 15.96MiB  
244          webm       854x480    DASH video  872k , vp9, 1fps, video only, 21.15MiB  
135          mp4        854x480    DASH video 1141k , avc1.4d401e, 24fps, video only, 29.63MiB  
247          webm       1280x720   DASH video 1791k , vp9, 1fps, video only, 40.43MiB  
136          mp4        1280x720   DASH video 2265k , avc1.4d401f, 24fps, video only, 56.86MiB  
248          webm       1920x1080  DASH video 3295k , vp9, 1fps, video only, 69.28MiB  
137          mp4        1920x1080  DASH video 4229k , avc1.640028, 24fps, video only, 113.12MiB  
17           3gp        176x144  
36           3gp        320x240  
5            flv        400x240  
43           webm       640x360  
18           mp4        640x360  
22           mp4        1280x720   (best)  

It is clear from the output above that the resolution corresponding to the code 22 is the best quality available. Eventhough youtube-dl usually picks the best quality, you can use the above command to make sure that you are getting the best quality video available. To the download the desired quality video from the output, the following syntax must be followed.

youtube-dl -f 22 https://www.youtube.com/watch?v=9bZkp7q19f0  

The above syntax should get you the desired quality of the video you want. I have explained the method to download a single video using youtube-dl. If you want to download a whole bunch of videos such as from playlists, an user account or videos from a text file you must read further.

Let us consider a youtube playlist from a python tutorial channel.

If you want to download all the videos from the above playlist, enter the following command.

youtube-dl -citw https://www.youtube.com/playlist?list=PLEA1FEF17E1E5C0DA  

This will go through the entire playlist and download all the videos from the playlist. There is a small issue with playlists that are created with videos from different channels / uploaders. You might not get consistent resolution across all videos. It is too difficult to manually specify the resolution for every single video hence you may leave it upto youtube-dl and it will always choose the best available option for you. Apart from downloading youtube playlists, you can also try downloading a complete user account. To demonstrate, i will try to download all the videos from urban dance camp's youtube channel.

youtube-dl -citw ytuser:UrbanDanceCamp  

The above command will download all the videos from the user account named UrbanDanceCamp to the current folder. If you want to download multiple videos from different sites, you can put them in a text file with one url per row and simply download them all using the following command.

youtube-dl -c --batch-file=filename.txt  

This will download videos from all url's in the text file. youtube-dl supports a whole bunch of sites other than youtube and you can get the whole list from supported url list. Your batch file can contain url's from any of the sites listed in the above link. If you collected url's from webcrawling or by some other means and you do not have them in proper format, you would have to do some cleanup and pass the url's as a python list or a tuple. This can be accomplished with a very simple program. I will include it sometime later in the future to this post. I would like to finish this by giving a very important tip everyone should remember. Sites like youtube constantly change ciphers and the way they manage content. Hence regular expressions written to obtain direct links will get updated from time to time. Hence i suggest you to keep your youtube-dl installation always updated.

If you installed directly from your package manager, try the following command to update.

sudo youtube-dl -U  

If you installed it using pip, then i suggest you to use the following command.

sudo pip install youtube-dl --upgrade  
comments powered by Disqus
subscribe