How to save YouTube videos on Linux

Have you ever found interesting videos on YouTube, and wanted to download and save them on your hard drive (for offline access or archiving purpose)? There is a handy open-source Linux tool that does exactly that: download YouTube videos.

A tool called youtube-dl is a command-line program written in Python that downloads videos from various online video sharing sites, including YouTube, DailyMotion, Vimeo, etc. Since the tool is written in Python, it can run on any platform that has Python interpreter (in particular, Python version 2.6, 2.7, or 3.3+). A binary for Windows platform is also available.

Here is a guide on how to download and save YouTube videos by using youtube-dl.

First install youtube-dl on your Linux system.

To install youtube-dl on Debian:

$ sudo apt-get install python-dev python-pip gcc
$ sudo pip install --upgrade youtube_dl

To install youtube-dl on Ubuntu or Linux Mint:

$ sudo apt-get install youtube-dl

To install youtube-dl on CentOS or RHEL, first set up EPEL repository on your system, and then run:

$ sudo yum install youtube-dl

To install youtube-dl on Fedora, simply run:

$ sudo yum install youtube-dl

Instead of using a package manager, you can also download youtube-dl Python code directly from its website, and use it.

Once you have installed youtube-dl, it is straightforward to download a YouTube video. You just need to pass a YouTube video link to youtube-dl. It will go ahead and fetch the video and store it as mp4/flv format.

$ youtube-dl http://www.youtube.com/watch?v=ykp8fxHWcVo
 Setting language
 ykp8fxHWcVo: Downloading video webpage
 ykp8fxHWcVo: Downloading video info webpage
 ykp8fxHWcVo: Extracting video information
[download] Destination: ykp8fxHWcVo.mp4
[download]   3.5% of 56.72M at  367.13k/s ETA 02:32^C

Resume Interrupted Downloads of YouTube Videos

The youtube-dl tool supports resuming interrupted downloads. If youtube-dl is killed (for example by Ctrl-C or due to loss of Internet connectivity) in the middle of download, you can simply re-run it with the same YouTube video url. It will automatically resume the unfinished download, as long as a partial download is present in the current directory.

Download YouTube Videos in Multiple Formats

Most of YouTube videos nowadays are available in multiple resolutions and formats in order to support various devices and Internet connections. The youtube-dl tool actually allows you to choose a specific resolution and format when downloading YouTube videos.

Since different YouTube videos may have different resolutions ready, you first need to check available video formats of a given YouTube video. For that, run youtube-dl with "-F" option. It will show you a list of available formats.

$ youtube-dl -F http://www.youtube.com/watch?v=BlXaGWbFVKY
 Setting language
 BlXaGWbFVKY: Downloading video webpage
 BlXaGWbFVKY: Downloading video info webpage
 BlXaGWbFVKY: Extracting video information
Available formats:
37      :       mp4     [1080x1920]
46      :       webm    [1080x1920]
22      :       mp4     [720x1280]
45      :       webm    [720x1280]
35      :       flv     [480x854]
44      :       webm    [480x854]
34      :       flv     [360x640]
18      :       mp4     [360x640]
43      :       webm    [360x640]
5       :       flv     [240x400]
17      :       mp4     [144x176]

Now among the available video formats, choose one that you like:

$ youtube-dl -f 17 http://www.youtube.com/watch?v=BlXaGWbFVKY

Download YouTube Videos in a Batch

If you want to download multiple YouTube videos in one shot, all you have to do is to prepare a text file that contains urls of all the videos. Then run youtube-dl with "-a" option.

$ cat video.list
http://www.youtube.com/watch?v=BlXaGWbFVKY
http://www.youtube.com/watch?v=6Rhd4JqEY2Y
. . . .
$ youtube-dl -a video.list

Extract Audio (MP3) From YouTube Videos

If you would like to extract audio from YouTube video and save it as a MP3 file, you can do the following.

First, install FFmpeg on your system.

Then use the following command to save the audio of YouTube video as a separate MP3 file. youtube-dl will use FFmpeg to perform audio extraction.

$ youtube-dl http://www.youtube.com/watch?v=BlXaGWbFVKY --extract-audio --audio-format mp3

Define Default Options of Youtube-dl

You can put commonly used options (e.g., --extract-audio --audio-format mp3) in youtube-dl's configuration file, which is located at either /etc/youtube-dl.conf (system-wide) or ~/.config/youtube-dl.conf (per-user), in which case those options become "default" arguments that you do not have to specify explicitly from the command line.

Subscribe to Xmodulo

Do you want to receive Linux FAQs, detailed tutorials and tips published at Xmodulo? Enter your email address below, and we will deliver our Linux posts straight to your email box, for free. Delivery powered by Google Feedburner.

3 thoughts on “How to save YouTube videos on Linux

  1. I dont know what is happeng, but this is what happens when I try to download a video:

    phyn@phyn-Satellite-C660 ~ $ youtube-dl -F http://www.youtube.com/watch?v=8zO6Wnmp1DU
    [youtube] Setting language
    [youtube] 8zO6Wnmp1DU: Downloading video webpage
    [youtube] 8zO6Wnmp1DU: Downloading video info webpage
    [youtube] 8zO6Wnmp1DU: Extracting video information
    Traceback (most recent call last):
    File "/usr/lib/python2.7/runpy.py", line 162, in _run_module_as_main
    "__main__", fname, loader, pkg_name)
    File "/usr/lib/python2.7/runpy.py", line 72, in _run_code
    exec code in run_globals
    File "/usr/bin/youtube-dl/__main__.py", line 7, in
    File "/usr/bin/youtube-dl/__init__.py", line 536, in main

    File "/usr/bin/youtube-dl/__init__.py", line 520, in _real_main

    File "/usr/bin/youtube-dl/FileDownloader.py", line 475, in download
    File "/usr/bin/youtube-dl/InfoExtractors.py", line 80, in extract
    File "/usr/bin/youtube-dl/InfoExtractors.py", line 405, in _real_extract
    File "/usr/bin/youtube-dl/InfoExtractors.py", line 405, in
    KeyError: 'sig'

    I will appreciate your assistance.

  2. #!/bin/bash
    # filename ./my-current-youtube-dl-sequential-renaming-script
    # 2013-07-08
    #
    # My original renaming idea.
    #
    # COUNT=1
    # for STRNG in $(cat ./download); do
    # FILENAME=$(youtube-dl -tc --get-filename $STRNG)
    # youtube-dl -tc $STRNG
    # mv "$FILENAME" $COUNT\-"$FILENAME"
    # COUNT=`expr $COUNT + 1`
    # done
    #
    # Alpha-Numeric ordering by computer made 10's preceed 1 and 20's
    # preceed 2, etc... also youtube-dl's naming scheme mixes ordering.
    # Edited to enter a starting number, and rename 1 thru 9 to 01 thru 09
    # followed by 10 and onward.
    # Attempt at making it 001 thru 999 failed. IDK why.
    # 01 thru 99 is more than enough anyway.
    #
    echo "Enter starting number"
    read COUNT
    for STRNG in $(cat ./download); do
    FILENAME=$(youtube-dl -tc --get-filename $STRNG)
    youtube-dl -tc $STRNG
    if [ $COUNT -lt 10 ]
    then
    mv "$FILENAME" 0$COUNT-"$FILENAME"
    else
    mv "$FILENAME" $COUNT-"$FILENAME"
    fi
    COUNT=`expr $COUNT + 1`
    done
    #
    # # cat of ./download
    #
    # http://www.youtube.com/watch?v=tPEE9ZwTmy0
    # http://www.youtube.com/watch?v=tPEE9ZwTmy0
    #
    # # Run output
    #
    # 'bash my-current-youtube-dl-sequential-renaming-script'
    # Enter starting number
    # 9
    # [youtube] Setting language
    # [youtube] tPEE9ZwTmy0: Downloading video webpage
    # [youtube] tPEE9ZwTmy0: Downloading video info webpage
    # [youtube] tPEE9ZwTmy0: Extracting video information
    # [download] Destination: Shortest Video on Youtube-tPEE9ZwTmy0.flv
    # [download] 100.0% of 128.74KiB at 654.21KiB/s ETA 00:00
    # [youtube] Setting language
    # [youtube] tPEE9ZwTmy0: Downloading video webpage
    # [youtube] tPEE9ZwTmy0: Downloading video info webpage
    # [youtube] tPEE9ZwTmy0: Extracting video information
    # [download] Destination: Shortest Video on Youtube-tPEE9ZwTmy0.flv
    # [download] 100.0% of 128.74KiB at 652.51KiB/s ETA 00:00
    #
    # # And output of 'ls' after run
    #
    # 09-Shortest Video on Youtube-tPEE9ZwTmy0.flv
    # 10-Shortest Video on Youtube-tPEE9ZwTmy0.flv
    # download
    # my-current-youtube-dl-sequential-renaming-script

Leave a comment

Your email address will not be published. Required fields are marked *

Current day month ye@r *