Last active
July 21, 2023 10:38
-
-
Save jonatasleon/8e80592cfee9e769236089a4d40beaeb to your computer and use it in GitHub Desktop.
Vimeo video downloader with Python 3
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
#!/bin/env python3 | |
import argparse | |
import base64 | |
import os | |
import re | |
import subprocess | |
import sys | |
from tempfile import mkstemp | |
import requests | |
from tqdm import tqdm | |
if len(sys.argv) > 1: | |
master_json_url = sys.argv[1] | |
output_file = sys.argv[2] | |
else: | |
print('master.json must be passed as argument', file=sys.stderr) | |
exit(1) | |
base_url = master_json_url[:master_json_url.rfind('/', 0, -26) + 1] | |
resp = requests.get(master_json_url) | |
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) | |
filenameVideo = mkstemp(prefix='.mp4')[1] | |
print ('saving to {}'.format(filenameVideo)) | |
video_file = open(filenameVideo, '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() | |
# Audio download here | |
bitrate = [(i, d['bitrate']) for (i, d) in enumerate(content['audio'])] | |
print('Bitrate', bitrate) | |
idx, _ = max(bitrate, key=lambda x: x[1]) | |
audio = content['audio'][idx] | |
audio_base_url = base_url + audio['base_url'] | |
print('Base url:', audio_base_url) | |
filenameAudio = mkstemp(prefix='.mp4')[1] | |
print('Saving AUDIO to %s' % filenameAudio) | |
audio_file = open(filenameAudio, 'wb') | |
init_segment = base64.b64decode(audio['init_segment']) | |
audio_file.write(init_segment) | |
for segment in tqdm(audio['segments']): | |
segment_url = audio_base_url + segment['url'] | |
segment_url = re.sub(r'/[a-zA-Z0-9_-]*/\.\./',r'/',segment_url.rstrip()) | |
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: | |
audio_file.write(chunk) | |
audio_file.flush() | |
audio_file.close() | |
print('Combining video and audio...') | |
cmd = 'ffmpeg -y -i ' | |
cmd += filenameAudio | |
cmd += ' -i ' | |
cmd += filenameVideo | |
cmd += ' ' + output_file | |
subprocess.call(cmd, shell=True) | |
print('Mixing Done!') | |
# Delete the remaining single audio and video files | |
os.remove(filenameAudio) | |
os.remove(filenameVideo) | |
print("Temporary files removed!") | |
print("Done!") |
Also add:
cmd += ' -c:v copy -c:a copy '
after cmd += filenameVideo
to avoid re-encoding
Where can i get master.json url for example for this video https://player.vimeo.com/video/819183591
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you for script!
Unfortunately I was unable to get video because of /video/ and /audio/ part in master.json url:
So I've added line
before line 29:
And video urls became correct.
UPDATE: mkstemp argument 'prefix' is wrong - it leads to temp files like ''.mp4q3inl_k0".
mkstemp(suffix='.mp4')[1] might be better :)