Download multiple youtube videos + Linux Python code

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:

youtube-page-url.png

Then create a text file in this format:

<youtube video url 1>,<filename1>

<youtube video url 2>,<filename2>

for example:

http://youtube.com/watch?v=skwY-Uij19U,mgs3 full story part1
http://youtube.com/watch?v=9thb0F98OOg,mgs3 full story part2

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][:-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[0])
			print namae
	except ValueError, msg:
		exit(msg)
	file.close()

if isempty(options):
	help_message()

for a in options[:]:
    if a[0] == '-h':
        help_message()

for a in options[:]:
	if a[0] == '--file' and a[1] != '':
		youtubedl(a[1])
		options.remove(a)
		break
	elif a[0] == '--file' and a[1] == '':
		print '--file expects an argument'
		sys.exit(0)

for a in options[:]:
    if a[0] == '--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.

Advertisements

Tags: , , ,

9 Responses to “Download multiple youtube videos + Linux Python code”

  1. Bluebeetle(one) Says:

    Hi,

    How long have spent writing your Python script?

    I’m currently testing an open source software named Miro, previously called Democracy Player.

    It allows researches & of course also downloads from:
    – YouTube
    – DailyMotion
    – Google video
    – & a few more I don’t even heard about before.

    http://www.getmiro.com/download/

    Bluebeetle(one).

  2. f241vc15 Says:

    Hi Bluebeetle,
    I actually wrote the whole Python script in less than an hour. It’s just actually a combination of my hobbies (programming + watching/downloading videos) so I don’t really get bothered.
    Anyhow, I have heard/read about Democracy before, but I didn’t know it’s been changed to Miro.
    I would think that it is a great addition to one’s collection of software for playing/getting videos.
    But if you only have python and would like to get downloading from youtube done with, my good old code would fit nicely (^)__(^) Like what I used to say, I guess I’m just a bit old-fashioned, always getting away with things in the command line.
    Anyway, I haven’t checked how Miro does it. Any idea?

  3. youtube download script update « F241vc15′ Weblog Says:

    […] download script update Actually, there’s no real update at my older post regarding the python script I created for automatically downloading multiple youtube videos. And there’s also nothing wrong with it. Problem is, I tried downloading videos last week and […]

  4. Mr.Legend Says:

    Hi,
    I use to download and save youtube videos
    http://www.downloadyoutubevids.com
    Here you can also search, watch and download youtue videos.
    All in One 🙂

  5. f241vc15 Says:

    Wow that’s great 🙂 will check it out asap. Do they also let you queue up video downloads so you won’t have to wait for every single video to load, just like in my python program?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: