Mac下下载B站弹幕,视频,拼装的东西

更新版本:http://www.cnbeining.com/?p=330
 
爱咋咋地了,擦。
难看就难看吧。
等下整个函数化后,把其他站加上。
 
用途:
下载弹幕
下载视频
拼成一个文件
 
 
Todo:
其他站支持
函数化
加个判断视频名的功能,新建目录扔进去

import os
from StringIO import StringIO
import gzip
import urllib2
def main(vid, p):
    cid = 0
    biliurl = 'http://www.bilibili.tv/video/av'+vid+'/index_'+p+'.html'
    print('Fetching webpage...')
    request = urllib2.Request(biliurl)
    request.add_header('Accept-encoding', 'gzip')
    response = urllib2.urlopen(request)
    if response.info().get('Content-Encoding') == 'gzip':
        buf = StringIO( response.read())
        f = gzip.GzipFile(fileobj=buf)
        data = f.read()
    data_list = data.split('\n')
    for lines in data_list:
        if 'cid=' in lines:
            cid = lines.split('&')
            cid = cid[0].split('=')
            cid = cid[-1]
            print('cid is ' + cid)
            break
    if cid is 0:
        print('Cannot find cid!')
        exit()
    print('Fetching XML...')
    os.system('curl -o '+cid+'.xml --compressed  http://comment.bilibili.tv/'+str(cid)+'.xml')
    #os.system('gzip -d '+cid+'.xml.gz')
    print('The XML file, ' + cid + '.xml should be ready...enjoy!')
    print('Finding video location...')
    request = urllib2.Request('http://www.flvcd.com/parse.php?kw='+biliurl)
    request.add_header('Accept-encoding', 'gzip')
    response = urllib2.urlopen(request)
    data = response.read()
    data_list = data.split('\n')
    for items in data_list:
        if 'name' in items and 'inf' in items and 'input' in items:
            c = items
            rawurl = c[39:-5]
            rawurl = rawurl.split('|')
            vid_num = len(rawurl)
            break
    print(rawurl)
    print(vid_num)
    for i in range(vid_num):
        print(i)
        os.system('aria2c -c -s16 -x16 -k1M --out '+str(i)+'.flv '+rawurl[i])
    f = open('ff.txt', 'w')
    ff = ''
    os.getcwd()
    for i in range(vid_num):
        ff = ff + 'file \'' + str(os.getcwd()) + '/'+ str(i) + '.flv\'\n'
    ff = ff.encode("utf8")
    f.write(ff)
    f.close()
    os.system('ffmpeg -f concat -i ff.txt -c copy '+str(cid)+'.mp4')
    os.system('rm -r ff.txt')
    exit()
vid = str(input('av'))
p = str(input('P'))
main(vid, p)

 

Leave a Reply

Your email address will not be published. Required fields are marked *