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!
Share on Twitter Share on Facebook
Comments
Comment deleted 4 years, 3 months ago
dragos 4 years, 3 months ago
cool mahn.. thnks ;)
Link | ReplyAndrei Ionita 4 years, 3 months ago
cu plăcere! ;-)
Link | ReplyDavid Walker 3 years, 5 months ago
Andrei, "avconv" package showing "missing" in Ubuntu 14.04
Link | Replykarthik devuluri 11 months ago
nice post and article
Link | Replykarthik devuluri 11 months ago
super post and article
Link | Replysadas 8 months, 2 weeks ago
Nice topic
Link | Replyrushessays 8 months, 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 | Replyletmyflowersgrow.com 2 weeks, 3 days ago
Do you know there is a lot more coming this year and we believe you would get desired results from our network? we have been working online because after my graduation, I've tried my level best to help people online through my stuff. Make the best use of it and follow us to learn basics of agriculture.
Link | ReplyNew Comment