main About me Back to Posts Personal Projects Links Privacy Contact
(This is a re-post from my old wordpress site so I don’t lose this info, I just cleaned it up a little for 2023. I hope someone finds it as useful as I do)
Not long ago, I realized I had uploaded some of my home movies to YouTube but no longer had local copies. These are videos I recorded of my friends' bands performing. To me, the audio is the important part; the video is pretty horrible. Since I don’t trust the closed-source ‘free’ web sites you find when I do a “download youtube” search, I decided to see if I could use any Linux tools help me with this. It turns out, it can.
The magic bits come from this open source project on GitHub: youtube-dl.
The initial setup is really simple and then you have lots of options for running the software once it’s installed. You can also explore the Options shown in the project’s ReadMe and so improve on what I’ve written here, depending on your needs.
Follow these steps to install it and get it setup:
Note: This worked just fine for me, on Ubuntun 22.04. Your mileage may vary.
Log in to your PC. Get yourself to a terminal prompt.
Type this then press enter:
sudo apt-get update
If like me, you will want to have the tool automatically extract the audio out of the videos, then type this and press enter:
sudo apt-get install libav-tools
Note: libav-tools used to be known as ffmpeg. I am using it to extract audio from my video files. It’s an official Debian package.
When that’s done, to install youtube-dl, type this and then press enter:
sudo curl -L https://yt-dl.org/downloads/latest/youtube-dl -o /usr/local/bin/youtube-dl
Make the tool executable by typing this and then press enter:
sudo chmod a+rx /usr/local/bin/youtube-dl
Next, let’s create a configuration file so you don’t have to enter the same options every time you use the tool.
Type this and press enter:
sudo nano /etc/youtube-dl.conf
The file probably won’t exist before this, but that’s OK. You can enter any of these Configuration / Options you like into your youtube-dl conf file. For me, I just want it to always pull the audio out of the video, so this is what my configuration file has in it:
# extract the audio
-x
# Use MP3 format
--audio-format "mp3"
Note: if you want to keep the video around after that extraction, be sure you use the “-k” option or youtube-dl will delete the file after the audio is extracted.
While still in the nano editor, when you’re done editing your file, press Ctrl-X, then Y, then press enter, to save the file and exit nano.
Now that this is all setup, you can test it out. Go and get the URL for a video from youtube.com (or lots of other video web sites), and copy it to your clipboard. Then enter this command, pasting your URL at the end:
youtube-dl {any options you want to use} -o "make up some video name here.flv" {paste the YouTube URL here}
When you press enter on that, youtube-dl should show you that it’s downloading the video to the file name you put after “-o”, and then say it’s doing whatever else you told it to, either at the command line or in your conf file. For instance, for me it auto-extracted the audio from the video to an MP3 file and then deleted the video file.
One way to make this even nicer, create a shell script to shorten the command you have to enter to do step 12 above.
At the terminal again, type this (or name the sh file anything you want) and press enter:
nano yt.sh
Assuming you created the config file as above, then in Nano, you can just type:
#
# youtube-dl shotcut script
#
echo -n "Select a file name: "
read filename
echo -n "Enter the URL to the video: "
read url
youtube-dl -o $filename.mp3 $url
Then type Ctrl-X, press Y and then press enter to save the file and exit from Nano.
Make that shell script executable with this command:
sudo chmod +x ./yt.sh
And you are done. Now any time you need to download one of your videos from YouTube, just jump into your Pi’s terminal and enter the command like this:
Note that this command starts with a period and then a forward slash. And the URL here is just an example; substitute with your video’s URL.
./yt.sh "My favorite YT video" https://www.youtube.com/watch?v=dQw4w9WgXcQ
…and in a minute or a few, you’ll have your video or MP3 or whatever you set up the tool to create.
You can explore all of the options made available via the youtube-dl application, including its support for authentication, subtitle handling, the “-g” option for when you only want to stream the video to another local application, requests with multiple URLs at once (for downloading playlists), and a lot more.
Enjoy! :)
Thanks to Rahul Kukreja on Unsplash for the header image I used here.