For those of you who like downloading videos from youtube.com, I made a simple Python code so you guys can download as many as you like at the same time.
For those of you who don’t know how to download videos from youtube.com yet,here’s a simple guide:
In youtube.com’s page, copy the url of the video you want to download, shown here encircled in red, for example:
Then create a text file in this format:
<youtube video url 1>,<filename1>
<youtube video url 2>,<filename2>
Place all the video urls you want to download at the same time (including the filename of the videos) in that text file. Remember to press the Return (Enter) button after every url/filename line on the text file. The video url and video filename you want for the file should be separated only by a single comma. The white spaces in the video filename you’ll give will be transformed into underscores (e.g. “video 1 part – 1” becomes “video_1_part_-_1”).
To run the program I made type
./dl-youtube.py --file=<text file with youtube video url>
Anyway, there’s a help message included.
The code also assumes you have youtube-dl (found here) code in your $PATH. Copy/download the youtube-dl code and save it on your PC. You can just do a
chmod 775 youtube-dl
to make the code executable. Then copy it to, say, your bin folder (which is in your $PATH by default):
cp -v youtube-dl /usr/bin
Here’s the code:
#!/usr/bin/python try: import commands, os, sys, string, getopt except ImportError, msg1: exit(msg1) try: options, xarguments = getopt.getopt(sys.argv[1:],'h', ['file=','version']) except getopt.error: print "Error: You tried to use an unknown option or the argument for an option that requires it was missing. Try `dl-youtube.py -h' for more information." sys.exit(0) def isempty(self) : return (len(self) == 0) def help_message(): print '''This program assumes youtube-dl is in $PATH. You basically create a text file containing all the youtube videos that you want to download in this format: ,video_title e.g. The title and the link must be separated by a single comma only, as seen from the example above. Options: -h displays this help message --file= give the text file containing the formatted , --version displays Python version ''' sys.exit(0) def youtubedl(dl_list): try: #change my_proxy to whatever proxy you want to use. If you don't want any, just delete the line after this. os.system("export http_proxy=my_proxy") file = open(dl_list,"r") lines = file.readlines() for x in lines: lnknme = x.split(",") namae = lnknme[:-1].replace(' ',"_") #replace username and passwd with your youtube username and password. If you don't have any, just remove this part: '-u username -p passwd' os.system("youtube-dl -u username -p passwd -o "+namae+".flv "+lnknme) print namae except ValueError, msg: exit(msg) file.close() if isempty(options): help_message() for a in options[:]: if a == '-h': help_message() for a in options[:]: if a == '--file' and a != '': youtubedl(a) options.remove(a) break elif a == '--file' and a == '': print '--file expects an argument' sys.exit(0) for a in options[:]: if a == '--version': print 'dl-youtube.py version 1.0' sys.exit(0)
If you have comments/suggestion/questions, please feel free to post and ask them.
You can download the source code as an odt file here=>dl-youtube.odt
Remember to save it as a text file with a .py extension for the Python interpreter. Unfortunately, WordPress only accepts jpg, jpeg, png, gif, pdf, doc, ppt, and odt for file uploads so I wasn’t able to upload the dl-youtube.py code. Just be sure to remember that indention is very important to Python.
Or better yet, if you don’t want to download the source code, you can view this page’s source code (in Firefox, right-click page then click View Page Source) and just copy the original and correct code with proper indentions, to a text editor, and save it with a .py extension.