We usually watch some interesting videos in youku. Sometimes we want to download the wonderful videos and save them locally as a souvenir. In win, we can download them using software such as wei tang, but it's hard for linux users. Although some plug-ins of chrome and firefox can be downloaded, sometimes the download is a segmented video. It is too cumbersome to manually use ffmpeg to synthesize a video, and you need to click the mouse many times to download the plug-in, too bad to match the character of linuxer ~ So I used python3 to write a youkuDown. py file. The Code is as follows:
import reimport urllib.requestimport jsonimport timeimport randomimport sysdef getVideoInfo(url):ruleTitle=re.compile('<title>(.*)</title>')ruleId=re.compile('http://v.youku.com/v_show/id_(.*).html')videoTitle=ruleTitle.findall(urllib.request.urlopen(url).read().decode('utf8'))videoId=ruleId.findall(url)return videoTitle[0],videoId[0]def getTrueLink(videoid):data=urllib.request.urlopen('http://v.youku.com/player/getPlayList/VideoIDS/'+videoid)info=json.loads(data.read().decode('utf8'))segs=info['data'][0]['segs']types=segs.keys()seed=info['data'][0]['seed']source=list("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ/\\:._-1234567890")mixed=''while source:seed=(seed*211+30031)&0xFFFFindex=seed*len(source)>>16c=source.pop(index)mixed+=cids=info['data'][0]['streamfileids']['flv'].split('*')[:-1]vid=''.join(mixed[int(i)] for i in ids)sid='%s%s%s'%(int(time.time()*1000),random.randint(1000,1999),random.randint(1000,9999))urls=[]for s in segs['flv']:no='%02x'%int(s['no'])url='http://f.youku.com/player/getFlvPath/sid/%s_%s/st/flv/fileid/%s%s%s?K=%s&ts=%s'%(sid,no,vid[:8],no.upper(),vid[10:],s['k'],s['seconds'])urls.append(url)return urlsdef down2file(urls,filename):f=open(filename,'wb')fileNum=len(urls)count=0for url in urls:count+=1print('downloading file %d/%d'%(count,fileNum))req=urllib.request.Request(url,headers={'Referer':'http://www.youku.com'})data=urllib.request.urlopen(req).read()f.write(data)f.close()print('download '+filename+' OK!')def youkuDown(link):videotitle,videoid=getVideoInfo(link)urls=getTrueLink(videoid)down2file(urls,videotitle+'.flv')if __name__=='__main__':if len(sys.argv)<2:print('Example Usage: python3 youkuDown.py http://v.youku.com/v_show/id_XNjUzNDQwMzIw.html')print('')exit() youkuDown(sys.argv[1])
Simply put, because the real link of a youku video changes dynamically, and the ID of each video remains unchanged, for example, starting with the link seek ('wget' + url. In addition, Youku uses segments for large videos. Therefore, the actual url is included in a List. The download results show the total number of segments and the number of segments downloaded in real time. The following is an example of my use:
Video Playback status saved to the local device:
Enjoy it, friends who want to download Youku videos in linux ~~ Wish you a happy New Year ~~ Hahaha
Reprinted Please note: