How to Download Youtube Videos in Linux (Ubuntu or Debian) from the command line!

(8 comments)

LATER EDIT: updated python code to PEP8 format.

Hi! Did you find that super rare cool YouTube video, and you would like to save it on your local PC, possibly convert it to mp3 ? If your answer is yes, here is how to do that in Linux (Ubuntu or Debian distros) from the command line. (for Mac OS X users check it here)

 First, install all the tools needed:

sudo apt-get update
sudo apt-get install youtube-dl avconv lame

 Then run the below Python script like in the following example:

 utub 'http://www.youtube.com/watch?v=VMJrvUSaAEA' '/home/userid/Music/ben_harper-thank_me_in_the_morning' 

 The Python script ,  just copy it, save it (don't forget to make it executable) and name it 'utub' :-) :

#! /usr/bin/python
# Filename: utub
# Author: Andrei Ionita <contact@andrei-ionita.com>

import sys
import os
import subprocess


def main():
    if len(sys.argv) < 2:
        print '\nUsage:'
        print 'utub you_tube_http_link local_songname'
        print " Example : utub 'http://www.youtube.com/watch?v="\
              "VMJrvUSaAEA' '/home/userid/Music/ben_harper-thank_me_in_"\
              "the_morning' "
        print " for support contact: contact@andrei-ionita.com"
        sys.exit(1)
    try:
        mycwd, songname = os.path.split(sys.argv[2])
        if not mycwd:
            mycwd = os.getcwd()
        elif not os.path.exists(mycwd):
            os.makedirs(mycwd)
        print '-> Files will be saved in:' + mycwd
        print '-> Audio:' + songname + '.mp3'
        print '-> Video:' + songname + '.flv'
        # youtube-dl
        my_command = '/usr/bin/youtube-dl '+sys.argv[1]
        proc = subprocess.Popen(my_command, bufsize=0, shell=True,
                                cwd=mycwd)
        print '-> Running youtube-dl:'
        proc.communicate()
        # avconv
        u2_songname = sys.argv[1].rsplit('?v=')
        for name in os.listdir(mycwd):
            if u2_songname[1] in name:
                u2_songname[1] = name
        my_command = 'avconv -i "'+u2_songname[1]+'" "'+u2_songname[1]+'.wav"'
        proc = subprocess.Popen(my_command, bufsize=0, shell=True,
                                cwd=mycwd)
        print '-> Running: avconv'
        proc.communicate()
        # lame
        my_command = 'lame "'+u2_songname[1]+'.wav" "'+songname+'.mp3"'
        proc = subprocess.Popen(my_command, bufsize=0, shell=True,
                                cwd=mycwd)
        print '-> Running: lame'
        proc.communicate()
        #Clean up
        os.remove(mycwd+'/'+u2_songname[1]+'.wav')
        ext = u2_songname[1].rsplit('.')
        os.rename(mycwd+'/'+u2_songname[1], mycwd+'/'+songname+'.'
                  + ext[1])
        print '-> Done!'
    except:
        print "Oops: Something really bad happened! :-)"

if __name__ == '__main__':
    main()

Enjoy it!

Current rating: 3

Comments

Comment deleted 3 years, 8 months ago

dragos 3 years, 8 months ago

cool mahn.. thnks ;)

Link | Reply
Current rating: 5

Andrei Ionita 3 years, 8 months ago

cu plăcere! ;-)

Link | Reply
Currently unrated

David Walker 2 years, 10 months ago

Andrei, "avconv" package showing "missing" in Ubuntu 14.04

Link | Reply
Currently unrated

karthik devuluri 4 months, 1 week ago

nice post and article

Link | Reply
Currently unrated

karthik devuluri 4 months, 1 week ago

super post and article

Link | Reply
Currently unrated

sadas 1 month, 2 weeks ago

Nice topic

Link | Reply
Currently unrated

rushessays 1 month, 2 weeks ago

Any user can watch online youtube videos and they can download them in linux ubuntu or debin from command. The features they are chosen to understood the values and assignments. Then you will get more instructions and essay features.

Link | Reply
Currently unrated

New Comment

required

required (not published)

optional