Downloading Youtube Videos to your Raspberry Pi

Although this is a renewable energy website, the majority of questions we receive about the Raspberry Pi revolve around how to download videos from either YouTube or the BBC iPlayer! Therefore, as the Raspberry Pi does work as a very energy efficient media player, in this article we will look at how you can download videos from Raspberry Pi to watch on your television.

In order to download YouTube videos you need to install youtube-dl. At the command line enter the following command.

sudo apt-get install youtube-dl

then check to make sure that (and automatically update if you do not) the latest version of youtube-dl with the command.

sudo youtube-dl -U

Click here for the documentation for youtube-dl, but when you give up trying to understand that, here are a few commands to help you get started (where the ID of the youtube video you are trying to download is substituted below as XXXXXXXXXXX):

youtube-dl -f 18

will download the video in 360p MP4 format* (approx 3MB per minute of download).

Downloading youtube video to raspberry pi
youtube-dl -f 22

will download the video (if it was originally uploaded at this resolution or above) in 720p high definition in MP4 format (approx 15MB per minute of download).

Instead of downloading videos one at a time like this, copy the full URL of each YouTube video you would like to download into a text file and enter the following command (where in this example we saved our text file as yt.txt, and we want to download at 360p MP4:

youtube-dl -f 18 -a yt.txt

* the youtube download formats can be viewed here: Youtube Quality and Codecs.

Watching YouTube Videos on Raspberry Pi

Now that you have some YouTube videos saved to your Raspberry Pi (see our article Mount USB Memory Stick on Raspberry Pi to find out how to increase the media storage space of your Raspberry Pi for all the videos you will be downloading), you need a way to play them. For this we recommend omxplayer – a command line video player. Enter the following command to install omxplayer (if your linux distribution does not come with it ready installed):

sudo apt-get install omxplayer

Assuming you have connected your Raspberry Pi to your television via HDMI, enter the following command to watch a video of a particular filename and have the audio come out of your television’s speakers:

omxplayer -o hdmi filename.mp4

The filenames of the YouTube videos you download will be very long – they are automatically generated from the title of YouTube video followed by the unique identifier of that video dot mp4. Use the ls -l command to display a list of the files in your directory to identify the video you want to watch. Don’t worry, you will not need to type the full name. When you have typed in the first few letters of the video file name hit the TAB key and the rest will be automatically appear.

For a list of the key bindings (the keys you press to control omxplayer while it is playing a video) enter the command:

omxplayer -k

For example use the left and right arrow keys to jump back or ahead by 30 seconds respectively, 1 or 2 to decrease or increase speed respectively, p or space to pause/resume, or q to quit.