We are generally in Youku to see some fun video, and sometimes see the wonderful download to the local save it as a souvenir, in the win can be used to download the software, such as Victoria, but suffering the children with Linux. Although some of Chrome and Firefox plug-ins can be downloaded, but sometimes download it is segmented video, but also to manually use FFmpeg to synthesize a video is too cumbersome, and plug-in download to click a few times the mouse, too does not conform to Linuxer's character ha ~ So I wrote a youkudown.py with Python3, the code is as follows:
[Python]View PlainCopy
- Import re
- Import Urllib.request
- Import JSON
- Import time
- Import Random
- Import Sys
- def 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) &0xFFFF
- Index=seed*len (source) >>
- C=source.pop (Index)
- Mixed+=c
- ids=info[' data ' [0][' streamfileids '] [' flv '].split (' * ') [:-1]
- Vid=". Join (Mixed[int (i)] for i in IDs)
- sid='%s%s%s '% (int (time.time () *), Random.randint (1999), Random.randint (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 URLs
- def down2file (urls,filename):
- F=open (filename,' WB ')
- Filenum=len (URLs)
- count=0
- For URL in URLs:
- count+=1
- print (' 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])
Briefly, because the true link of Youku video is dynamic, and the ID of each video is constant, for example, in link http://v.youku.com/v_show/id_XNjUzNDQwMzIw.html, "Xnjuzndqwmziw" Is the unique ID identifier of the video, through which the ID can request a new link generated by the "http://v.youku.com/player/getPlayList/VideoIDS/" +id, and get the video information in JSON format. Then assembled into a video of the real link, this real link is also time-sensitive, and to this real link to initiate the request must be the head of the Referer set to www.youku.com fooled the server to download, so I did not like before with Os.system (' wget ' + URL). Also, Youku on the big video is segmented, so get the real URL I have a list inside, download the time will show a total of a few paragraphs and real-time download how many paragraphs. Here are some examples of my use:
To save to a local video playback situation:
In Linux want to download Youku video of the pot friends enjoy it~~ at the same time I wish you a happy New Year ~ ha haha
Python3 Automatically download Youku video applet