Category Archives: Python Scripts

Automate download of Binary Video Files using Python

I had a need to download about 100 or so videos from Infoq.com

I had the list of mp4 files which needed to be downloaded and I stored them in a text file with the following formt




So I wrote a quick 20 min Python code to download all the binary/video files and ran it overnight and had all the files on my HDD in the morning

The 1st parameter to this code – is the filename where all the files ( from the URL ) needed to be downloaded is stored.
The local file name where the bytes are stored is formed by parsing the URL and for my case the name was like 12-jun-the12factorapp.mp4 – and I formed the local file name as the12factorapp.mp4

** Only thing I later realized would have been better to have in the code is to have a timeout

**I purposely did not put any exception handling code just for time saving purpose - this is a quick way to code this - not necessarily resilent and robust


import urllib2, sys, re

videoListFileName = sys.argv[1]
fileWithVideosToDownload = open(videoListFileName, ‘r’)

for line in fileWithVideosToDownload:
lst = line.split(‘-‘)
outFileName = lst[2].strip()
fw = open(outFileName, mode=’wb’)

fo = urllib2.urlopen(line)
data = fo.read(10240)
l = len(data)
print ‘Starting Download ‘ + lst[2] + ‘ ….’
while(l != 0 ):
fw.write(data)
data = fo.read(10240)
l = len(data)

print ‘Downloading ‘ + lst[2] + ‘ done ‘
fo.close()
fw.close()

Advertisements