Python3 Automatically download Youku video applet

Source: Internet
Author: User
Tags unique id

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 
  1. Import re
  2. Import Urllib.request
  3. Import JSON
  4. Import time
  5. Import Random
  6. Import Sys
  7. def getvideoinfo (URL):
  8. Ruletitle=re.compile (' <title> (. *) </title> ')
  9. Ruleid=re.compile (' http://v.youku.com/v_show/id_ (. *). html ')
  10. Videotitle=ruletitle.findall (Urllib.request.urlopen (URL). read (). Decode (' UTF8 '))
  11. Videoid=ruleid.findall (URL)
  12. return videotitle[0],videoid[0]
  13. def gettruelink (videoid):
  14. Data=urllib.request.urlopen (' http://v.youku.com/player/getPlayList/VideoIDS/' +videoid)
  15. Info=json.loads (Data.read (). Decode (' UTF8 '))
  16. segs=info[' data ' [0][' segs ']
  17. Types=segs.keys ()
  18. seed=info[' data ' [0][' seed ']
  19. Source=list ("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz/\\:._-1234567890")
  20. mixed="
  21. While Source:
  22. seed= (seed*211+30031) &0xFFFF
  23. Index=seed*len (source) >>
  24. C=source.pop (Index)
  25. Mixed+=c
  26. ids=info[' data ' [0][' streamfileids '] [' flv '].split (' * ') [:-1]
  27. Vid=". Join (Mixed[int (i)] for i in IDs)
  28. sid='%s%s%s '% (int (time.time () *), Random.randint (1999), Random.randint (9999 ))  
  29. Urls=[]
  30. For s in segs[' flv ']:
  31. no='%02x '%int (s[' no ')
  32. 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 '])
  33. Urls.append (URL)
  34. return URLs
  35. def down2file (urls,filename):
  36. F=open (filename,' WB ')
  37. Filenum=len (URLs)
  38. count=0
  39. For URL in URLs:
  40. count+=1
  41. print (' downloading file%d/%d '% (count,filenum))
  42. Req=urllib.request.request (url,headers={' Referer ':' http://www.youku.com ')
  43. Data=urllib.request.urlopen (req). Read ()
  44. F.write (data)
  45. F.close ()
  46. print (' Download ' +filename+' ok! ')
  47. def youkudown (link):
  48. Videotitle,videoid=getvideoinfo (link)
  49. Urls=gettruelink (VideoID)
  50. Down2file (urls,videotitle+'. flv ')
  51. If __name__==' __main__ ':
  52. If Len (SYS.ARGV) <2:
  53. print (' Example usage:python3 youkudown.py http://v.youku.com/v_show/id_XNjUzNDQwMzIw.html ')
  54. print (")
  55. Exit ()
  56. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.