Skip to content

Instantly share code, notes, and snippets.

@paschoaletto
Last active September 13, 2022 13:52
Show Gist options
  • Save paschoaletto/7f65b7e36b76ccde9fe52b74b62ab9df to your computer and use it in GitHub Desktop.
Save paschoaletto/7f65b7e36b76ccde9fe52b74b62ab9df to your computer and use it in GitHub Desktop.
Downloads segmented audio+video from Vimeo and saves as .mp4 - Usage: 'python vimeo_downloader.py http://...master.json?base64_init=1 optional_name' modified from https://gist.github.com/tayiorbeii/d78c7e4b338b031ce8090b30b395a46f that was modified from https://gist.github.com/alexeygrigorev/a1bc540925054b71e1a7268e50ad55cd
import requests
import base64
from tqdm import tqdm
import sys
import subprocess as sp
FFMPEG_BIN = 'ffmpeg.exe'
master_json_url = sys.argv[1]
base_url = master_json_url[:master_json_url.rfind('/', 0, -26) - 5]
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)
video = content['video'][idx]
video_base_url = base_url + 'video/' + video['base_url']
print 'base url:', video_base_url
filename = 'v.mp4'
video_filename = filename
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()
audio = content['audio'][0]
audio_base_url = base_url + audio['base_url'][3:]
print 'base url:', audio_base_url
filename = 'a.mp3'
audio_filename = filename
print 'saving to %s' % filename
audio_file = open(filename, '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']
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()
filename = sys.argv[2] + '.mp4' if sys.argv[2] else 'video.mp4'
command = [ FFMPEG_BIN,
'-y', # (optional) overwrite output file if it exists
'-i', audio_filename,
'-i',video_filename,
'-acodec', 'copy',
'-vcodec', 'h264',
filename ]
sp.call(command, shell=True)
@eMBee
Copy link

eMBee commented Apr 6, 2017

i have merged all gists into a repo here: https://github.com/eMBee/vimeo-download

@aelkz
Copy link

aelkz commented Mar 17, 2018

If you're using Linux, you'll need a few changes:

FFMPEG_BIN = 'ffmpeg.exe'
to:
FFMPEG_BIN = 'ffmpeg'

and

command = [ FFMPEG_BIN,
        '-y', # (optional) overwrite output file if it exists
        '-i', audio_filename,
        '-i',video_filename,
        '-acodec', 'copy',
        '-vcodec', 'h264',
        filename ]

to:

command = [ FFMPEG_BIN,
        '-i', audio_filename,
        '-i',video_filename,
        '-acodec', 'copy',
        '-vcodec', 'h264',
        filename ]

Tested in Fedora 26.

@vaibhavbangwal
Copy link

vaibhavbangwal commented Jan 14, 2019

What if there is no master.json?
http://104.199.144.5:1935/vod/smil:4209201712080900.smil/chunk_ctaudio_ridp0aa0br64000_cs880633_w1255087167_mpd.m4s -- there are several links like this. How do I download these chunks ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment