Files
cwsvJudo/onlineTraining/yt-clipper.py
2021-03-14 09:11:36 +01:00

87 lines
1.9 KiB
Python
Executable File

#! /usr/bin/env python3
import youtube_dl
import json
import ffmpeg
import subprocess
import glob
import sys
jsonFileName = sys.argv[1]
#jsonFileName = "***REMOVED***Kniebeuge.json"
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']
)
stream = ffmpeg.filter(stream, "scale", height="480", width="-2" )
stream = ffmpeg.output(stream,
clip['target'],
vcodec="vp9",
**{
"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']
)
stream = ffmpeg.filter(stream, "scale", height="480", width="-2" )
stream = ffmpeg.output(stream,
clip['target'],
vcodec="vp9",
**{
"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)