Added thumbnails for 8.Kyu

This commit is contained in:
marko
2022-02-19 10:33:14 +01:00
parent acf6c09b0d
commit 975c88dee6
2 changed files with 100 additions and 46 deletions

View File

@@ -9,16 +9,41 @@ import sys
import argparse
import os
def generate_thumbnail(in_filename, out_filename, time, height):
try:
(
ffmpeg
.input(in_filename, ss=time)
.filter('scale', -2, height)
.output(out_filename, vframes=1)
.overwrite_output()
.run(capture_stdout=True, capture_stderr=True)
)
except ffmpeg.Error as e:
print(e.stderr.decode(), file=sys.stderr)
sys.exit(1)
# setting width and height from hardcoded defaults -> configured defaults -> clip
def getDimensions(config, clip):
h = config['height'] if 'height' in config else 480
w = config['width'] if 'width' in config else -2
if 'scale' in clip:
h = clip['scale']['h'] if 'h' in clip['scale'] else h
w = clip['scale']['w'] if 'w' in clip['scale'] else w
return h, w
config = {
'vcodec': "vp9",
'acodec': "libopus"
'acodec': "libopus",
'height': 480,
'quality': "best",
}
argParser = argparse.ArgumentParser()
jsonFileName = sys.argv[1]
clipDict = {}
@@ -28,7 +53,7 @@ with open(jsonFileName) as jf:
ydl_opts = {"outtmpl": "%(id)s"}
for clip in clipDict:
# create the directories so ffmped doesn't complain
# create the directories so ffmpeg doesn't complain
try:
outputDir = os.path.dirname(clip['target'])
os.makedirs(outputDir)
@@ -41,20 +66,22 @@ for clip in clipDict:
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],
)
# @todo This is a very bad hack because the outtmpl options doesn't seem to be working if the file gets reencoded
inputFilename = glob.glob(infoDict['id']+"*")[0]
video = stream.video
audio = stream.audio
# generate preview image for the video
if 'poster' in clip:
generate_thumbnail(inputFilename, os.path.splitext(clip['target'])[0]+".jpg", clip['poster']['timeIndex'], h )
kwArgs = {}
if 'from' in clip:
kwArgs['ss'] = clip['from']
if 'to' in clip:
kwArgs['to'] = clip['to']
stream = ffmpeg.input( inputFilename, **kwArgs)
video, audio = stream.video, stream.audio
if 'crop' in clip:
stream = ffmpeg.filter(stream,
@@ -65,12 +92,7 @@ for clip in clipDict:
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 )
h, w = getDimensions(config, clip)
stream = ffmpeg.output(stream,
clip['target'],
@@ -82,7 +104,7 @@ for clip in clipDict:
# "b:v":"276k", "minrate":"138k", "maxrate":"400k", # x360
"b:v":"512k", "minrate":"256k", "maxrate":"742k", # x480 LQ
# "b:v":"750k", "minrate":"375k", "maxrate":"1088k", # x480 MQ
"quality":"good",
"quality": config['quality'] if 'quality' in config else "best",
}
)
try:
@@ -110,12 +132,7 @@ for clip in clipDict:
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 )
h, w = getDimensions(config, clip)
stream = ffmpeg.output(stream, audio,
clip['target'],
@@ -124,8 +141,10 @@ for clip in clipDict:
# "an":None,
"y":None,
"pass":"2",
"b:v":"512k", "minrate":"375k", "maxrate":"1088k",
"quality":"good",
# "b:v":"276k", "minrate":"138k", "maxrate":"400k", # x360
"b:v":"512k", "minrate":"256k", "maxrate":"742k", # x480 LQ
# "b:v":"750k", "minrate":"375k", "maxrate":"1088k", # x480 MQ
"quality": config['quality'] if 'quality' in config else "best",
"acodec": config['acodec'],
}
)
@@ -133,6 +152,5 @@ for clip in clipDict:
ffmpeg.run(stream)
except:
print(infoDict)
#640x480p @ 24,25,30 512 (LQ), 750 (MQ) 256 (LQ) 375 (MQ) 742 (LQ) 1088 (MQ)
exit(-1)