Files
cwsvJudo/onlineTraining/yt-clipper.py
2021-02-28 17:05:03 +01:00

69 lines
1.5 KiB
Python
Executable File

#! /usr/bin/env python3
import youtube_dl
import json
import ffmpeg
import subprocess
import glob
jsonFileName = "clips.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'],
)
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'],
)
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)