40

用 python 下载 vimeo 视频

 4 years ago
source link: https://www.tuicool.com/articles/mUVf6vA
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

现在vimeo上的视频尝试过使用在线的第三方网站进行下载,发现有些链接根本不支持,例如这种链接 https://vimeo.com/asrm/review/366781261/67afcdc0a4 ,直接提示链接无效。

y2Izemi.png!web

要相信程序员总是有办法的,决定自己动手,丰衣足食。

打开Chrome开启调试,发现视频是分片段下发的。

bQRRnq6.png!web

剩下的问题就是如何把这些视频碎片合并成一个完整的视频了。这里教大家一个方法,就是先找到master.json 这个文件

Jbiy22A.png!web

这里面有该视频的完整地址

完整代码:

import requests
import base64
from tqdm import tqdm


master_json_url = 'https://164skyfiregce-vimeo.akamaized.net/exp=1571835428~acl=%2F366916363%2F%2A~hmac=a00b4aed3a3ebf0324f76552b57008a68d09264b41f63a9a0e802042b3790c39/366916363/sep/video/1515464456,1515464455,1515464454,1515464451,1515464450/master.json?base64_init=1'
base_url = master_json_url[:master_json_url.rfind('/', 0, -26) + 1]

# 如果不能访问就尝试用代理
proxies = {
  'http': 'socks5://127.0.0.1:1081',
  'https': 'socks5://127.0.0.1:1081',
}

resp = requests.get(master_json_url, proxies=proxies)
content = resp.json()

# 找到分辨率最高的资源
heights = [(i, d['height']) for (i, d) in enumerate(content['video'])]
idx, _ = max(heights, key=lambda h: h[1])
video = content['video'][idx]

# 拼成完整的视频地址
video_base_url = base_url + video['base_url']
print('base url:', video_base_url)

filename = 'ASRM2019MembersMeeting%s.mp4' % video['id']
print('saving to %s' % filename)

video_file = open(filename, 'wb')

init_segment = base64.b64decode(video['init_segment'])
video_file.write(init_segment)

for segment in tqdm(video['segments']):
    segment_url = video_base_url + segment['url']
    resp = requests.get(segment_url, stream=True)
    if resp.status_code != 200:
        print('not 200!')
        print(resp)
        print(segment_url)
        break
    for chunk in resp:
        video_file.write(chunk)

video_file.flush()
video_file.close()

开始下载:

IbU3Yvz.png!web

有问题可以扫描二维码和我交流

关注公众号「Python之禅」,回复「1024」免费获取Python资源

vQremuy.jpg!web

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK