Download and Watch YouTube videos from Linux Shell Prompt

Publishing your own video on YouTube, or watching other people’s videos, is all the rage these days. Why are we talking about YouTube, in a column about the CLI? Because this week we’re writing about youtube-dl, a clever little CLI tool that’s easy to install and and use to fetch YouTube videos.

ADVERTISEMENTS

Youtube-dl is a Python script, licensed under the “non-copyleft” free software MIT/X11 license. It is not platform-specific; it can run under Linux, Mac OS X, or Windows platforms so long as a recent — 2.4 or later — version of Python is installed. It is a small command-line program to download videos from YouTube.com and a few more sites. It requires the Python interpreter (2.6, 2.7, or 3.3+), and it is not platform specific. It should work in your Unix box, in Windows or in Mac OS X. It is released to the public domain, which means you can modify it, redistribute it or use it however you like.

Update: Check out this bash script – it does same thing without using python.

🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source & DevOps topics via:
CategoryList of Unix and Linux commands
File Managementcat
Network Utilitiesdig host ip nmap
Package Managerapk apt
Processes Managementbg chroot disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w

ADVERTISEMENTS
26 comments… add one
  • bash Aug 14, 2006 @ 15:15

    http://bashscripts.org/viewtopic.php?t=210


    #!/bin/bash
    bu="http://youtube.com/get_video.php?";mkdir -p ~/YouTube;cd ~/YouTube;read -p "YouTube url? " ur;read -p "Name? " nv
    wget ${ur} -O /tmp/y1;uf=${bu}`grep player2.swf /tmp/y1 | cut -d? -f2 | cut -d" -f1`;wget "${uf}" -O /tmp/y.flv
    ffmpeg -i /tmp/y.flv -ab 56 -ar 22050 -b 500 -s 320x240 ${nv}.mpg;rm /tmp/y.flv; rm /tmp/y1;rm gmon.out; exit

    BASH RULES 🙂

  • 🐧 nixCraft Aug 14, 2006 @ 18:27

    Hee

    Nice and dirty bash work

    Appreciate your post.

  • Ivan Aug 18, 2006 @ 7:46

    lol @ rm gmon.out

  • Ray Aug 18, 2006 @ 8:02

    Great information, thanks very much!

    You might try this for Zsh (maybe modern Bournes?):

    ytdl () {
    video_id=${1#http://*youtube.com/watch?v=}
    video_id=${video_id#http://*youtube.com/v/}
    video_id=${video_id%&*}
    player_line=`ftp -o - "http://www.youtube.com/watch?v=$video_id"|grep /player2.swf`
    base_getter_url=http://youtube.com/get_video.php?
    url=${player_line/*player2.swf?/$base_getter_url}
    url=${url%%"*}
    ftp -o $video_id.flv "$url"
    ffmpeg -i $video_id.flv -ab 56 -ar 22050 -b 500 -s 320x240 $video_id.mpg
    echo
    echo mplayer $video_id.mpg
    }

    If you have an ftp with HTTP support.

  • 🐧 nixCraft Aug 19, 2006 @ 18:17

    Nice to see you are using Zsh 🙂

    Appreciate your script/post.

  • luke Sep 19, 2006 @ 7:39

    great 😀
    thats real hacking lol

  • TjL Oct 21, 2006 @ 3:12

    The ZSH code has a problem:

    ffmpeg version 0.4.9-pre1, build 4718, Copyright (c) 2000-2004 Fabrice Bellard
    built on Apr 4 2005 06:01:28, gcc: 3.4.2 [FreeBSD] 20040728
    Input #0, flv, from ‘j0u_F8-oYQE.flv’:
    Duration: N/A, bitrate: N/A
    Stream #0.0: Audio: mp3, 22050 Hz, mono
    Stream #0.1: Video: flv, 320×240, 15.00 fps
    Output #0, mpeg, to ‘j0u_F8-oYQE.mpg’:
    Stream #0.0: Video: mpeg1video, 320×240, 15.00 fps, q=2-31, 500 kb/s
    Stream #0.1: Audio: mp2, 22050 Hz, mono, 56 kb/s
    Stream mapping:
    Stream #0.1 -> #0.0
    Stream #0.0 -> #0.1
    [mpeg1video @ 0x282f4010]MPEG1/2 doesnt support 15/1 fps
    Error while opening codec for stream #0.0 – maybe incorrect parameters such as bit_rate, rate, width or height

  • James Feb 14, 2007 @ 4:32

    Here’s the bash script, made more solid. I fixed the ffmpeg call (it doesn’t like making 15fps mpeg files), and kept the flv at the end.

    If you want to use the flv from your own apache server you’ll need to add

    video/x-flv flv

    to /etc/mime.types and restart apache, then go and get flash_flv player from http://www.jeroenwijering.com/upload/flash_flv_player.zip

    unpack it, put flvplayer.swf and flvplayer.html on your site with the .flv, and modify flvplayer.html appropriately

    #!/bin/bash
    bu=”http://youtube.com/get_video.php?”
    tmp=`mktemp`
    touch ${tmp}.flv
    read -p “YouTube url? ” ur
    wget ${ur} -O ${tmp}
    uf=${bu}`grep player2.swf ${tmp} | cut -d? -f2 | cut -d” -f1`
    nv=`cat ${tmp} | grep ‘h1 id=”video_title”‘ | cut -d “>” -f 2 | cut -d “

  • James Feb 14, 2007 @ 4:35

    #!/bin/bash
    bu=”http://youtube.com/get_video.php?”
    tmp=`mktemp`
    touch ${tmp}.flv
    read -p “YouTube url? ” ur
    wget ${ur} -O ${tmp}
    uf=${bu}`grep player2.swf ${tmp} | cut -d? -f2 | cut -d” -f1`
    nv=`cat ${tmp} | grep ‘h1 id=”video_title”‘ | cut -d “>” -f 2 | cut -d “

  • James Feb 14, 2007 @ 4:36

    nv=`cat ${tmp} | grep ‘h1 id=”video_title”‘ | cut -d “>” -f 2 | cut -d “lt” -f 1`

    *** i can’t post a less than symbol, so change the lt in that command to a less than symbol

    echo “Title ${nv}”
    wget “${uf}” -O ${tmp}.flv
    ffmpeg -i ${tmp}.flv -r 30 -ab 56 -ar 22050 -b 1500k -s 320×240 “${nv}.mpg”
    mv ${tmp}.flv “${nv}.flv”
    rm ${tmp}
    exit

  • 🐧 nixCraft Feb 15, 2007 @ 4:39

    James,

    You can use
    <pre>code</pre>

    or
    <code>code</code> tags 🙂

  • SaveTube Feb 19, 2007 @ 6:34

    I use http://www.savetube.com to save any youtube videos. They also got a latest saved section.

  • Jen Feb 28, 2007 @ 22:14

    I recommend http://www.snatchvid.com it’s very easy and fast to use!

  • James Attard Jun 19, 2007 @ 14:22

    does anybody know how he arrived to the get_video.php ? i mean how did he know that that php file existed on youtube servers?

  • Coolclu3 Aug 28, 2007 @ 16:55

    Uhm…youtube has changed their code. now this script doesn’t work any more, from something like 20th August – 20007. Anyone can help fix the code? savetube.com has done it

  • Roger Oct 12, 2007 @ 11:24

    A good script is Mortube
    Demo: http://www.morllaines.com/busca_youtube/?tag=brasil (Portuguese)
    Download: http://www.morllaines.com/downloads/arquivo.php?file=54 (English)

    The script require youtube apy key on config.php
    And Curl installed on server

  • wearetherock Apr 2, 2008 @ 6:05

    #!/bin/bash

    ### mr-paul
    ### paul@sci.ubu.ac.th

    if [ “$1” = “” ]; then
    echo -e “nUsage: getyoutube http://www.youtube.com/watch?v=UHvAXLBMWGIn
    else
    url=$1
    name=${url:(-11)} ### get the last 11 chars
    #echo $name

    fullscreenURL=$( wget -q -nv $url -O – | grep &video_id= )
    echo $fullscreenURL
    title=${fullscreenURL##*=} ### delete to last occurance of ‘=’
    title=${title/’;/} ### replace ‘; with nothing ${title:(-2)} or #{title#(-2)} do not work’ 🙁
    echo $title

    videoID=${fullscreenURL:54} ### cut first 53 chars
    videoID=${videoID/’;/} ##
    echo $videoID

    wget -q -nv “http://www.youtube.com/get_video?$videoID” -O “$title.flv”
    fi

  • clarjon1 May 20, 2008 @ 14:20

    Hmm, anyone got a perl one liner for this job? 😉

  • Rob May 29, 2008 @ 21:47

    There’s now a software tool to allow linux users to download youtube videos to a preferred directory, in a variety of formats (mpeg, wmv, mp4, flv, avi), specifying the resolution… all in a GTK theme’d graphical tool.

    Check it here:
    http://yourtubedownloader.awardspace.com/

    Feedback most appreciated, via the forums or by email.

  • macias Jul 13, 2008 @ 19:19

    Wearetherock, MANY THANKS! Simplicity is what I like, great script.

  • Webdesign Nov 26, 2008 @ 15:19

    Nice youtube script, thanks so much!

    Mark

  • Jim Bowe Dec 17, 2008 @ 13:36

    Good script to do this in bash here:
    http://blog.johnlawrence.net/2008/12/youtube-download-shell-script/

    Works with the latest YouTube versions. (As of December 2008)

  • xijhing Aug 18, 2009 @ 5:16

    usage: utube NAME youtubeurl

    #!/bin/bash
    if [ $# -ne 2 ]
    then
    echo “Usage : $0 ”
    echo “e.g : $0 steve_jobs http://www.youtube.com/watch?v=D1R-jKKp3NA”
    else
    todnload=$(youtube-dl -g -b $2 | xargs wget –spider 2>&1 | grep Location: | awk '{print $2}')
    #echo -e “Got the file…n$todnload”
    axel $todnload -q -o ~/$1.flv
    echo “Download Completed…”
    ffmpeg -i $1.flv -r 50 -ar 48000 -ab 196000 -b 300k -croptop 0 -cropbottom 0 -cropleft 0 -cropright 0 $1.mp4
    echo "Cleaning up..."
    rm ~/$1.flv
    rm ~/videoplayback?ip*
    echo "Done! Enjoy watching $1"
    fi
  • phar0z Sep 14, 2009 @ 20:24
    #!/bin/bash
    # checking whether you have youtube-dl
    if [ ! -x `which youtube-dl`  ];then
    echo “Error! youtube-dl isn’t installed.”
    exit 0
    fi
    
    #checking whether you have vbrfix
    if [ ! -x `which vbrfix`  ];then
    echo “Error! vbrfix isn’t installed.”
    exit 0
    fi
    
    #download video(s)
    for URL in $*; do
    youtube-dl -t “$1&fmt=18″
    done
    
    #process
    for VIDEO in `ls *.flv`; do
    OUTPUT=${VIDEO:0:${#VIDEO}-4}”.mp3″
    ffmpeg -i $VIDEO -acodec copy $OUTPUT
    #move video
    mv $OUTPUT /home/$USER/music
    vbrfix /home/$USER/music/$OUTPUT /home/$USER/music/$OUTPUT
    rm -rf vbrfix.log vbrfix.tmp
    #clean-up
    rm -rf $VIDEO
    done
    echo “Done.”
  • Rafael Soares Apr 8, 2011 @ 17:53

    There’s a new script for linux to search and watch youtube’s video on linux shell, called YouShell.

    http://code.google.com/p/youshell/

  • Davide Mirtillo May 6, 2011 @ 7:39

    I think you should update the article, the project has apparently moved to github:

    http://rg3.github.com/youtube-dl/

Leave a Reply

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

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.