分享一个Python下载视频脚本

分享一个Python下载视频脚本(通过flvcd.com代理的)

代码如下:

#!/usr/local/bin/python

#test for command line parameter(s)
#import sys
#print 'scriptname: ', sys.argv[0]
#(i, len) = (1, len(sys.argv))
#while i < len:
#  print 'command parameter', i, sys.argv[i]
#  i = i+1
#exit(0)

import sys

argc = len(sys.argv)
if argc == 2:
  format = 'high'
elif argc == 3:
  format = sys.argv[2]
else:
  print("Usage: %s videourl [videoquality=normal|high|super|...]" % sys.argv[0])
  print(" e.g.");
  print("   %s http://v.youku.com/v_show/id_XMzMzMjE0MjE2.html super" % sys.argv[0])
  exit(1)

videourl = sys.argv[1];

import urllib2
import urllib
url = 'http://www.flvcd.com/parse.php?kw=' + urllib.quote(videourl)  + '&format=' + format;

req = urllib2.Request(url);
# add some headers to fake Firefox Browser(if we don't do so, there will be a problem when try to get tudou video)
req.add_header('host', 'www.flvcd.com');
req.add_header('Referer', url[:-4]);
req.add_header('User-Agent', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:2.0) Gecko/20100101 Firefox/4.0');
req.add_header('Accept-Language', 'en-us,en;q=0.5');
req.add_header('Accept-Encoding', 'gzip, deflate');
req.add_header('Accept-Charset', 'ISO-8859-1,utf-8;q=0.7,*;q=0.7');
req.add_header('Keep-Alive', '115');
res = urllib2.urlopen(req);
html = res.read()

import re
pattern = re.compile('(.+)');
result = urlpattern.findall(urls);

data = [result[i:i+2] for i in range(0, len(result), 2)]

print '\n--- Start to download from url "%s" (%d block(s) in total):' % (videourl, len(data))
for k, v in enumerate(data):
  print '  >downloading Block %.2d ...' % (k+1,)
  urllib.urlretrieve(v[1], v[0] + '.flv')
  print '  downloaded Block.%.2d completely<' % (k+1,)
print '--- finished ---\n'

保存到~/dl.py

进入欲下载到的目录, 然后执行~/dl.py 在线观看视频地址 [视频质量]

例如:

~/dl.py http://v.youku.com/v_show/id_XMzMzMjE0MjE2.html super

将会下载超清的视频

对于优酷视频来说, super超清, high高清, normal标清

其他的视频网站参考下flvcd.com的format参数


updated log:

2011-12-20 16:15 更新解决编码转换问题(flvcd.com是gbk编码的, 所以之前flvcd.com网站上写的gb2312和偶写的gb2312都有问题)


优酷, 土豆都是没有问题的, 试一下



你可能感兴趣的:(Python)