-
-
Save goatscrub/227dd779d174d67c2e62088ab17003a4 to your computer and use it in GitHub Desktop.
Downloading segmented video from vimeo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import requests | |
import base64 | |
from tqdm import tqdm | |
import sys | |
# Search for master.json URL in dev tools | |
master_json_url = 'https://178skyfiregce-a.akamaihd.net/exp=1474107106~acl=%2F142089577%2F%2A~hmac=0d9becc441fc5385462d53bf59cf019c0184690862f49b414e9a2f1c5bafbe0d/142089577/video/426274424,426274425,426274423,426274422/master.json?base64_init=1' | |
sp='video/' | |
base_url = master_json_url.split(sp)[0] | |
resp = requests.get(master_json_url) | |
content = resp.json() | |
# retrieve best quality | |
streams = { 'audio': {}, 'video': {} } | |
audios = [(i, a['bitrate']) for (i, a) in enumerate(content['audio'])] | |
heights = [(i, d['height']) for (i, d) in enumerate(content['video'])] | |
best_audio, _ = max(audios, key=lambda x:x[1]) | |
idx, _ = max(heights, key=lambda x:x[1]) | |
# populate streams | |
streams['video']['content'] = content['video'][idx] | |
streams['audio']['content'] = content['audio'][best_audio] | |
streams['video']['base_url'] = base_url + sp + streams['video']['content']['base_url'] | |
streams['audio']['base_url'] = base_url + 'audio/' + streams['audio']['content']['base_url'] | |
streams['video']['filename'] = 'video_{}.mp4'.format(streams['video']['content']['id']) | |
streams['audio']['filename'] = 'audio_{}.mp4'.format(streams['audio']['content']['id']) | |
for stream in ['video', 'audio']: | |
outfile = open(streams[stream]['filename'], 'wb') | |
init_segment = base64.b64decode(streams[stream]['content']['init_segment']) | |
outfile.write(init_segment) | |
print('STREAM: {}'.format(stream)) | |
print('{} URL: {}'.format(stream, streams[stream]['base_url'])) | |
print('OUTFILE {}: {}'.format(stream, streams[stream]['filename'])) | |
for segment in tqdm(streams[stream]['content']['segments']): | |
segment_url = streams[stream]['base_url'] + segment['url'] | |
resp = requests.get(segment_url, stream=True) | |
if resp.status_code != 200: | |
print('not 200!') | |
print(resp) | |
break | |
for chunk in resp: | |
outfile.write(chunk) | |
outfile.flush() | |
outfile.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment