#! /usr/bin/env python3 import youtube_dl import json import ffmpeg import subprocess import glob import sys import argparse config = {} config['vcodec'] = "vp9" argParser = argparse.ArgumentParser() jsonFileName = sys.argv[1] clipDict = {} with open(jsonFileName) as jf: clipDict = json.load(jf) ydl_opts = {"outtmpl": "%(id)s"} for clip in clipDict: infoDict = None with youtube_dl.YoutubeDL(ydl_opts) as ydl: infoDict = ydl.extract_info(clip['source'], download=False) ydl.download([clip['source']]) if infoDict is not None: stream = ffmpeg.input( # @todo This is a very bad hack because the outtmpl options doesn't seem to be working if the file gets reencoded glob.glob(infoDict['id']+"*")[0], ss=clip['from'], to=clip['to'], ) if 'crop' in clip: stream = ffmpeg.filter(stream, "crop", x=clip['crop']['x'], y=clip['crop']['y'], w=clip['crop']['w'], h=clip['crop']['h'] ) h = 480 w = -2 if 'scale' in clip: h = clip['scale']['h'] if 'h' in clip['scale'] else 480 w = clip['scale']['w'] if 'w' in clip['scale'] else -2 stream = ffmpeg.filter(stream, "scale", height=h, width=w ) stream = ffmpeg.output(stream, clip['target'], vcodec=config['vcodec'], **{ "an":None, "y":None, "pass":"1", "b:v":"512k", "minrate":"375k", "maxrate":"1088k", "quality":"good", } ) try: ffmpeg.run(stream) except: print(infoDict) stream = ffmpeg.input( glob.glob(infoDict['id']+"*")[0], ss=clip['from'], to=clip['to'], ) if 'crop' in clip: stream = ffmpeg.filter(stream, "crop", x=clip['crop']['x'], y=clip['crop']['y'], w=clip['crop']['w'], h=clip['crop']['h'] ) h = 480 w = -2 if 'scale' in clip: h = clip['scale']['h'] if 'h' in clip['scale'] else 480 w = clip['scale']['w'] if 'w' in clip['scale'] else -2 stream = ffmpeg.filter(stream, "scale", height=h, width=w ) stream = ffmpeg.output(stream, clip['target'], vcodec=config['vcodec'], **{ "an":None, "y":None, "pass":"2", "b:v":"512k", "minrate":"375k", "maxrate":"1088k", "quality":"good", } ) try: ffmpeg.run(stream) except: print(infoDict) #640x480p @ 24,25,30 512 (LQ), 750 (MQ) 256 (LQ) 375 (MQ) 742 (LQ) 1088 (MQ)