Howto use youtube-dl

Intro

If you followed the Raspberry Pi project to create a music video player you may want to know how to create a suitable video collection to play!? Read on...


Install

ssh to your Raspberry Pi as per the instructions on the project page. Install youtube-dl with: sudo apt install youtube-dl -y

You only need to do this once.


Download

Change directory to the Videos folder with: cd Videos

You may want to create artist container directories to keep things organised. Remember with Unix, if you enter the command: mkdir Duran Duran you will end up with one folder called "Duran" and an error. You either need to create the directory with a hyphen in the middle; i.e. mkdir Duran-Duran or place quotes around the spaced name to stop shell-expansion: mkdir "Duran Duran"

Change to the directory with: cd "Duran Duran" (enter "Du" and press the Tab key for auto-complete).

In a browser of your choice, find the video you want to download. Right-click the mouse to copy the link.

copy link

Back on the command line, enter the command: youtube -F and paste the video link on the end. e.g. youtube-dl -F https://www.youtube.com/watch?v=J5ebkj9x5Ko

youtube-dl will output the formats offered to view the video.

youtube-dl

You are looking for the best video quality in avc1 as mp4. In this case 135. You also want the best format audio, and that is 140 (m4a). 136 is usually 720p video, and 137 is usually 1080p video. It is your choice if you want to download 4k versions. You will need the latest Raspberry Pi model that supports 4k resolution and 4k playback.

To download the video, use: youtube-dl -f 135+140 https://www.youtube.com/watch?v=J5ebkj9x5Ko

If this was available in 1080p, you would use youtube-dl -f 137+140 https://...

youtube-dl will download the video file, then the audio file, before using ffmpeg to stitch them back together.

Be careful when copying links from play-lists. An example: https://www.youtube.com/watch?v=Z9z0e1Wm64M&list=PL9tY0BWXOZFsDrXsO8GNIbioWe_HW853w&index=4

The & in Unix means you want to leave a programme running in memory. You only need the first part before &list, so delete the rest; e.g.

https://www.youtube.com/watch?v=Z9z0e1Wm64M&list=PL9tY0BWXOZFsDrXsO8GNIbioWe_HW853w&index=4 <-- delete the part in red.

You may want to rename the files to remove the extra bits youtube-dl adds on. Enter the command: mv along with the first few characters of the filename, then press the Tab key to auto-complete. Add a space and enter the name you want - remember to add it in quotes if you want it in spaces. For example:

mv Duran\ Duran\ -\ The\ Reflex\ \(Official\ Music\ Video\)-J5ebkj9x5Ko.mp4 "The Reflex.mp4"

It is a good idea to back-up your video collection!!

Once you have all of your videos loaded, reboot your Raspberry Pi and enjoy!

If you add videos to your collection whilst the current playlist is playing, you might wonder why they are not being shown. You will need to reboot in order for the script to create a new playlist.


Page updated: 3rd December 2022