Files
cwsvJudo/DieJudoGürtelprüfung/clipper
2021-10-26 19:14:22 +02:00

128 lines
2.6 KiB
Python
Executable File

#! /usr/bin/env python3
import youtube_dl
import json
import ffmpeg
import subprocess
import glob
import sys
import argparse
config = {
'vcodec': "vp9",
'acodec': "libopus"
}
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:
if 'from' in clip and 'to' in clip:
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'],
)
else:
stream = ffmpeg.input(
glob.glob(infoDict['id']+"*")[0],
)
video = stream.video
audio = stream.audio
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)
if 'from' in clip and 'to' in clip:
stream = ffmpeg.input(
glob.glob(infoDict['id']+"*")[0],
ss=clip['from'],
to=clip['to'],
)
else:
stream = ffmpeg.input(
glob.glob(infoDict['id']+"*")[0]
)
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, audio,
clip['target'],
vcodec=config['vcodec'],
**{
# "an":None,
"y":None,
"pass":"2",
"b:v":"512k", "minrate":"375k", "maxrate":"1088k",
"quality":"good",
"acodec": config['acodec'],
}
)
try:
ffmpeg.run(stream)
except:
print(infoDict)
#640x480p @ 24,25,30 512 (LQ), 750 (MQ) 256 (LQ) 375 (MQ) 742 (LQ) 1088 (MQ)