make-ffmpeg: Combine with make-ffmpeg-fade

This commit is contained in:
Jannik Beyerstedt 2024-11-02 17:23:10 +01:00
parent 31f01ca386
commit 8933550f31
5 changed files with 26 additions and 334 deletions

View file

@ -8,6 +8,8 @@ alpha = false
prores = false prores = false
;; in and out time format: t for seconds, n for frame number ;; in and out time format: t for seconds, n for frame number
inout_type = t inout_type = t
;; fade-in duration (seconds), leave out or set to zero to disable
;fade_duration = 0.5
;; Some font settings can have defaults, which can be overridden in the ;; Some font settings can have defaults, which can be overridden in the
;; 'title', 'speaker' and 'text' sections below. ;; 'title', 'speaker' and 'text' sections below.

View file

@ -1,327 +1,5 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
# vim: tabstop=4 shiftwidth=4 expandtab # vim: tabstop=4 shiftwidth=4 expandtab
import os print("ERROR: The functionality of this script was added to 'make-ffmpeg.py'!")
import sys print("Specify meta.fade_duration = 0.5 in the config.ini for the same effect.")
import subprocess
import renderlib
import schedulelib
import argparse
import shlex
from PIL import ImageFont
from configparser import ConfigParser
import json
# Parse arguments
parser = argparse.ArgumentParser(
description='C3VOC Intro-Outro-Generator - Variant which renders only using video filters in ffmpeg',
usage="./make-ffmpeg.py yourproject/",
formatter_class=argparse.RawTextHelpFormatter)
parser.add_argument('project', action="store", metavar='Project folder', type=str, help='''
Path to your project folder
''')
parser.add_argument('--debug', action="store_true", default=False, help='''
Run script in debug mode and render with placeholder texts,
not parsing or accessing a schedule.
This argument must not be used together with --id
Usage: ./make-ffmpeg.py yourproject/ --debug
''')
parser.add_argument('--id', dest='ids', nargs='+', action="store", type=int, help='''
Only render the given ID(s) from your projects schedule.
This argument must not be used together with --debug
Usage: ./make-adobe-after-effects.py yourproject/ --id 4711 0815 4223 1337
''')
parser.add_argument('--room', dest='rooms', nargs='+', action="store", type=str, help='''
Only render the given room(s) from your projects schedule.
This argument must not be used together with --debug
Usage: ./make-adobe-after-effects.py yourproject/ --room "HfG_Studio" "ZKM_Vortragssaal"
''')
parser.add_argument('--skip', nargs='+', action="store", type=str, help='''
Skip ID(s) not needed to be rendered.
Usage: ./make-ffmpeg.py yourproject/ --skip 4711 0815 4223 1337
''')
parser.add_argument('--force', action="store_true", default=False, help='''
Force render if file exists.
''')
args = parser.parse_args()
if (args.skip is None):
args.skip = []
def headline(str):
print("##################################################")
print(str)
print("##################################################")
print()
def error(str):
headline(str)
parser.print_help()
sys.exit(1)
cparser = ConfigParser()
cparser.read(os.path.join(os.path.dirname(args.project), 'config.ini'))
template = cparser['default']['template']
alpha = cparser['default']['alpha']
prores = cparser['default']['prores']
fade_duration = 0.5
title_in = float(cparser['title']['in'])
title_out = float(cparser['title']['out'])
title_duration = title_out - title_in
title_font = cparser['title']['font']
title_fontsize = int(cparser['title']['fontsize'])
title_fontcolor = cparser['title']['fontcolor']
title_x = int(cparser['title']['x'])
title_y = int(cparser['title']['y'])
speaker_in = float(cparser['speaker']['in'])
speaker_out = float(cparser['speaker']['out'])
speaker_duration = speaker_out - speaker_in
speaker_font = cparser['speaker']['font']
speaker_fontsize = int(cparser['speaker']['fontsize'])
speaker_fontcolor = cparser['speaker']['fontcolor']
speaker_x = int(cparser['speaker']['x'])
speaker_y = int(cparser['speaker']['y'])
text_in = float(cparser['text']['in'])
text_out = float(cparser['text']['out'])
text_duration = text_out - text_in
text_font = cparser['text']['font']
text_fontsize = int(cparser['text']['fontsize'])
text_fontcolor = cparser['text']['fontcolor']
text_x = int(cparser['text']['x'])
text_y = int(cparser['text']['y'])
text_text = cparser['text']['text']
font_t = os.path.join(os.path.dirname(args.project), title_font)
font_s = os.path.join(os.path.dirname(args.project), speaker_font)
font_tt = os.path.join(os.path.dirname(args.project), text_font)
fileformat = os.path.splitext(template)[1]
infile = os.path.join(os.path.dirname(args.project), template)
schedule = cparser['default']['schedule']
if not (os.path.exists(os.path.join(args.project, template))):
error("Template file {} in Project Path is missing".format(template))
for ffile in (title_font, speaker_font, text_font):
if not (os.path.exists(os.path.join(args.project, ffile))):
error("Font file {} in Project Path is missing".format(ffile))
if not (os.path.exists(os.path.join(args.project, 'config.ini'))):
error("config.ini file in Project Path is missing")
if alpha == 'true' and not fileformat == '.mov':
error("Alpha can only be rendered with .mov source files")
if not args.project:
error("The Project Path is a required argument")
if not args.debug and not schedule:
error("Either specify --debug or supply a schedule in config.ini")
if args.debug:
persons = ['Thomas Roth', 'Dmitry Nedospasov', 'Josh Datko']
events = [{
'id': 'debug',
'title': 'wallet.fail',
'subtitle': 'Hacking the most popular cryptocurrency hardware wallets',
'persons': persons,
'personnames': ', '.join(persons),
'room': 'Borg',
}]
else:
events = list(schedulelib.events(schedule))
def describe_event(event):
return "#{}: {}".format(event['id'], event['title'])
def event_print(event, message):
print("{} {}".format(describe_event(event), message))
def fmt_command(command, **kwargs):
args = {}
for key, value in kwargs.items():
args[key] = shlex.quote(value)
command = command.format(**args)
return shlex.split(command)
def run(command, **kwargs):
return subprocess.check_call(
fmt_command(command, **kwargs),
stderr=subprocess.STDOUT,
stdout=subprocess.DEVNULL)
def fit_text(string: str, frame_width):
split_line = [x.strip() for x in string.split()]
lines = []
w = 0
line_num = 0
line = []
for word in split_line:
new_line = line + [word.rstrip(':')]
w, _ = translation_font.getsize(" ".join(new_line))
print(w, new_line)
if w > frame_width:
print("too wide, breaking", line)
lines.append(' '.join(line))
line = []
line.append(word.rstrip(':'))
if word.endswith(':'):
print(':, breaking', line)
lines.append(' '.join(line))
line = []
if line:
lines.append(' '.join(line))
return lines
def fit_title(string: str):
global translation_font
translation_font = ImageFont.truetype(font_t, size=title_fontsize-10, encoding="unic")
title = fit_text(string, 1080)
return title
def fit_speaker(string: str):
global translation_font
translation_font = ImageFont.truetype(font_s, size=speaker_fontsize-10, encoding="unic")
speaker = fit_text(string, 1080)
return speaker
def enqueue_job(event):
event_id = str(event['id'])
if event_id in args.skip:
event_print(event, "skipping " + str(event['id']))
return
if (os.path.exists(os.path.join(args.project, event_id + '.ts')) or os.path.exists(os.path.join(args.project, event_id + '.mov'))) and not args.force:
event_print(event, "file exist, skipping " + str(event['id']))
return
event_title = str(event['title'])
event_personnames = str(event['personnames'])
event_title = event_title.replace('"', '')
event_title = event_title.replace('\'', '')
event_personnames = event_personnames.replace('"', '')
t = fit_title(event_title)
s = fit_speaker(event_personnames)
print(s)
if args.debug:
print('Title: ', t)
print('Speaker: ', s)
outfile = os.path.join(os.path.dirname(args.project), event_id + '.ts')
videofilter = ""
for idx, line in enumerate(t):
videofilter += "drawtext=fontfile={fontfile}:fontsize={fontsize}:fontcolor={fontcolor}:x={x}:y={y}:text='{text}':".format(
fontfile=font_t,
fontsize=title_fontsize,
fontcolor=title_fontcolor,
x=title_x,
y=title_y + (idx * title_fontsize),
text=line)
videofilter += "alpha='if(lt(t,{fade_in_start_time}),0,if(lt(t,{fade_in_end_time}),(t-{fade_in_start_time})/{fade_duration},if(lt(t,{fade_out_start_time}),1,if(lt(t,{fade_out_end_time}),({fade_duration}-(t-{fade_out_start_time}))/{fade_duration},0))))',".format(
fade_in_start_time=title_in,
fade_in_end_time=title_in + fade_duration,
fade_out_start_time=title_in + fade_duration + title_duration,
fade_out_end_time=title_in + fade_duration + title_duration + fade_duration,
fade_duration=fade_duration
)
for idx, line in enumerate(s):
videofilter += "drawtext=fontfile={fontfile}:fontsize={fontsize}:fontcolor={fontcolor}:x={x}:y={y}:text='{text}':".format(
fontfile=font_s,
fontsize=speaker_fontsize,
fontcolor=speaker_fontcolor,
x=speaker_x,
y=speaker_y + (idx * speaker_fontsize),
text=line)
videofilter += "alpha='if(lt(t,{fade_in_start_time}),0,if(lt(t,{fade_in_end_time}),(t-{fade_in_start_time})/{fade_duration},if(lt(t,{fade_out_start_time}),1,if(lt(t,{fade_out_end_time}),({fade_duration}-(t-{fade_out_start_time}))/{fade_duration},0))))',".format(
fade_in_start_time=speaker_in,
fade_in_end_time=speaker_in + fade_duration,
fade_out_start_time=speaker_in + fade_duration + speaker_duration,
fade_out_end_time=speaker_in + fade_duration + speaker_duration + fade_duration,
fade_duration=fade_duration
)
if fileformat == '.mov':
if alpha == 'true':
if prores == 'true':
cmd = 'ffmpeg -y -i "{0}" -vf "{1}" -vcodec prores_ks -pix_fmt yuva444p10le -profile:v 4444 -shortest -movflags faststart -f mov "{2}"'.format(
infile, videofilter, outfile)
else:
cmd = 'ffmpeg -y -i "{0}" -vf "{1}" -shortest -c:v qtrle -movflags faststart -f mov "{2}"'.format(
infile, videofilter, outfile)
else:
cmd = 'ffmpeg -y -i "{0}" -vf "{1}" -map 0:0 -c:v mpeg2video -q:v 2 -aspect 16:9 -map 0:1 -c:a mp2 -b:a 384k -shortest -f mpegts "{2}"'.format(
infile, videofilter, outfile)
else:
cmd = 'ffmpeg -y -i "{0}" -vf "{1}" -map 0:0 -c:v mpeg2video -pix_fmt:v yuv420p -qscale:v 2 -qmin:v 2 -qmax:v 7 -keyint_min 0 -bf 0 -g 0 -maxrate:0 90M -aspect 16:9 -map 0:1 -c:a mp2 -b:a 384k -shortest -f mpegts "{2}"'.format(
infile, videofilter, outfile)
if args.debug:
print(cmd)
run(cmd)
return event_id
if args.ids:
if len(args.ids) == 1:
print("enqueuing {} job".format(len(args.ids)))
else:
print("enqueuing {} jobs".format(len(args.ids)))
else:
if len(events) == 1:
print("enqueuing {} job".format(len(events)))
else:
print("enqueuing {} jobs".format(len(events)))
for event in events:
if args.ids and event['id'] not in args.ids:
continue
if args.rooms and event['room'] not in args.rooms:
print("skipping room %s (%s)" % (event['room'], event['title']))
continue
event_print(event, "enqueued as " + str(event['id']))
job_id = enqueue_job(event)
if not job_id:
event_print(event, "job was not enqueued successfully, skipping postprocessing")
continue
print('all done')

View file

@ -71,10 +71,11 @@ class TextConfig:
return fit_text(text, (FRAME_WIDTH-self.x-100), font) return fit_text(text, (FRAME_WIDTH-self.x-100), font)
def get_ffmpeg_filter(self, inout_type: str, text: list[str]): def get_ffmpeg_filter(self, inout_type: str, fade_time: float, text: list[str]):
if not text: if not text:
return "" return ""
text_duration = self.outpoint - self.inpoint
filter_str = "" filter_str = ""
for idx, line in enumerate(text): for idx, line in enumerate(text):
filter_str += "drawtext=enable='between({},{},{})'".format( filter_str += "drawtext=enable='between({},{},{})'".format(
@ -92,6 +93,14 @@ class TextConfig:
filter_str += ":fontsize={0}:fontcolor={1}:x={2}:y={3}:text={4}".format( filter_str += ":fontsize={0}:fontcolor={1}:x={2}:y={3}:text={4}".format(
self.fontsize, self.fontcolor, self.x, self.y + (idx*self.fontsize), ffmpeg_escape_str(line)) self.fontsize, self.fontcolor, self.x, self.y + (idx*self.fontsize), ffmpeg_escape_str(line))
if fade_time > 0:
filter_str += ":alpha='if(lt(t,{fade_in_start_time}),0,if(lt(t,{fade_in_end_time}),(t-{fade_in_start_time})/{fade_duration},if(lt(t,{fade_out_start_time}),1,if(lt(t,{fade_out_end_time}),({fade_duration}-(t-{fade_out_start_time}))/{fade_duration},0))))'".format(
fade_in_start_time=self.inpoint,
fade_in_end_time=self.inpoint + fade_time,
fade_out_start_time=self.inpoint + fade_time + text_duration,
fade_out_end_time=self.inpoint + fade_time + text_duration + fade_time,
fade_duration=fade_time)
filter_str += "," filter_str += ","
return filter_str[:-1] return filter_str[:-1]
@ -103,6 +112,7 @@ class Config:
alpha: bool = False alpha: bool = False
prores: bool = False prores: bool = False
inout_type: str = "t" # in and out time format: t for seconds, n for frame number inout_type: str = "t" # in and out time format: t for seconds, n for frame number
fade_duration: float = 0 # fade duration in seconds, 0 to disable
fileext: str fileext: str
@ -128,6 +138,7 @@ def parse_config(filename) -> Config:
conf.alpha = meta.getboolean('alpha', conf.alpha) conf.alpha = meta.getboolean('alpha', conf.alpha)
conf.prores = meta.getboolean('prores', conf.prores) conf.prores = meta.getboolean('prores', conf.prores)
conf.inout_type = meta.get('inout_type', conf.inout_type) conf.inout_type = meta.get('inout_type', conf.inout_type)
conf.fade_duration = meta.getfloat('fade_duration', conf.fade_duration)
defaults = cparser['default'] defaults = cparser['default']
default_fontfile = defaults.get('fontfile', None) default_fontfile = defaults.get('fontfile', None)
@ -242,9 +253,10 @@ def enqueue_job(conf: Config, event):
else: else:
ffmpeg_path = 'ffmpeg' ffmpeg_path = 'ffmpeg'
videofilter = conf.title.get_ffmpeg_filter(conf.inout_type, title) + "," videofilter = conf.title.get_ffmpeg_filter(conf.inout_type, conf.fade_duration, title) + ","
videofilter += conf.speaker.get_ffmpeg_filter(conf.inout_type, speakers) + "," videofilter += conf.speaker.get_ffmpeg_filter(conf.inout_type,
videofilter += conf.text.get_ffmpeg_filter(conf.inout_type, extra_text) conf.fade_duration, speakers) + ","
videofilter += conf.text.get_ffmpeg_filter(conf.inout_type, conf.fade_duration, extra_text)
cmd = [ffmpeg_path, '-y', '-i', conf.template_file, '-vf', videofilter] cmd = [ffmpeg_path, '-y', '-i', conf.template_file, '-vf', videofilter]

View file

@ -1,4 +1,4 @@
pillow pillow>=8.0.0
cssutils==1.0.2 cssutils==1.0.2
lxml==4.9.1 lxml==4.9.1
svg.path==4.0.2 svg.path==4.0.2

View file

@ -4,13 +4,13 @@ schedule = http://vcfb.de/2024/schedule.xml
template = intro.mp4 template = intro.mp4
alpha = false alpha = false
prores = false prores = false
fontfile = true inout_type = t
inout = t fade_duration = 0.5
[title] [title]
in = 1 in = 1
out = 9.5 out = 9.5
font = Computerfont.ttf fontfile = Computerfont.ttf
fontsize = 100 fontsize = 100
fontcolor = #ffffff fontcolor = #ffffff
x = 85 x = 85
@ -19,7 +19,7 @@ y = 122
[speaker] [speaker]
in = 2 in = 2
out = 9 out = 9
font = SourceSansPro-Semibold.ttf fontfile = SourceSansPro-Semibold.ttf
fontsize = 65 fontsize = 65
fontcolor = #ffffff fontcolor = #ffffff
x = 85 x = 85
@ -29,7 +29,7 @@ y = 861
[text] [text]
in = 0 in = 0
out = 0 out = 0
font = Computerfont.ttf fontfile = Computerfont.ttf
fontsize = 0 fontsize = 0
fontcolor = #ffffff fontcolor = #ffffff
x = 0 x = 0