Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
587 views
in Technique[技术] by (71.8m points)

Python - download video from indirect url

I have a link like this

https://r9---sn-4g57knle.googlevideo.com/videoplayback?id=10bc30daeba89d81&itag=22&source=picasa&begin=0&requiressl=yes&mm=30&mn=sn-4g57knle&ms=nxu&mv=m&nh=IgpwcjA0LmZyYTE2KgkxMjcuMC4wLjE&pl=19&sc=yes&mime=video/mp4&lmt=1439597374686662&mt=1474140191&ip=84.56.35.53&ipbits=8&expire=1474169270&sparams=ip,ipbits,expire,id,itag,source,requiressl,mm,mn,ms,mv,nh,pl,sc,mime,lmt&signature=6EF8ABF841EA789F5314FC52C3C3EA8698A587C9.9297433E91BB6CBCBAE29548978D35CDE30C19EC&key=ck2

which is a temporary generated redirect from (a link like) this link: https://2.bp.blogspot.com/bO0q678cHRVZqTDclb33qGUXve_X1CRTgHMVz9NUgA=m22

(so the first link won't work in a couple of hours)

How can I download the video from the googlevideo site with Python? I already tried youtube-dl because of this answer, but it isn't working for me.

The direct URL would already help me a lot!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can use pycurl

#!/bin/usr/env python
import sys
import pycurl

c = pycurl.Curl()
c.setopt(c.FOLLOWLOCATION, 1)
c.setopt(c.URL, sys.argv[1])
with open(sys.argv[2], 'w') as f:
    c.setopt(c.WRITEFUNCTION, f.write)
    c.perform()

Usage:

$ chmod +x a.py 
$ ./a.py "https://2.bp.blogspot.com/bO0q678cHRVZqTDclb33qGUXve_X1CRTgHMVz9NUgA=m22" output.mp4
$ file output.mp4
output.mp4: ISO Media, MP4 v2 [ISO 14496-14]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...