102 lines
4.5 KiB
Python
102 lines
4.5 KiB
Python
#!/usr/bin/env python3
|
|
from yaml import safe_load
|
|
from pathlib import Path
|
|
from sys import argv
|
|
import logging
|
|
from shlex import quote
|
|
|
|
path_stickers_root = Path(argv[1])
|
|
path_stickers_output_dir = path_stickers_root / 'stickers'
|
|
path_stickers_source_dir = path_stickers_root / 'src'
|
|
path_makefile_gen = path_stickers_root / 'makefile.gen'
|
|
path_stickers_data_dir = path_stickers_root / 'data'
|
|
log = logging.getLogger("stickers.make")
|
|
logging.basicConfig(
|
|
filename=str(path_stickers_root / 'make.log'),
|
|
level=logging.DEBUG
|
|
)
|
|
output_ext = 'static'
|
|
out_formats = {
|
|
'static': {
|
|
'extension': 'webp',
|
|
'options': '-lossless 1 -f webp'
|
|
},
|
|
'video': {
|
|
'extension': 'webm',
|
|
'options': '-codec:v libvpx-vp9 -map v -pix_fmt yuva420p'
|
|
},
|
|
'png': {
|
|
'extension': 'png',
|
|
'options': '-preset:v icon -frames 1'
|
|
}
|
|
}
|
|
|
|
with open(path_makefile_gen, 'w') as makefile:
|
|
targets_list = list()
|
|
# makefile: skeleton rules
|
|
makefile.write(".PHONY: all clean stickers/\n")
|
|
makefile.write('stickers:\n\tmkdir stickers\n')
|
|
makefile.write('clean:\n\trm -vrf stickers\n')
|
|
for sticker_description_file in path_stickers_data_dir.glob('*.yaml'):
|
|
log.debug('input description file: %s', sticker_description_file)
|
|
with open(sticker_description_file) as yaml_file:
|
|
sticker_description = safe_load(yaml_file)
|
|
log.debug("stuff %s", sticker_description)
|
|
target_filename = path_stickers_output_dir \
|
|
/ ( sticker_description['name'] \
|
|
+ '.' \
|
|
+ out_formats[output_ext]['extension'] )
|
|
if sticker_description['input']['base'].get('format','') \
|
|
not in ('lavfi', 'image2'):
|
|
prerequisite_filename = str(path_stickers_source_dir \
|
|
/ sticker_description['input']['base']['input'])
|
|
else:
|
|
prerequisite_filename = ''
|
|
targets_list.append(f"{target_filename}")
|
|
# makefile: rule declaration
|
|
escaped_prereq = prerequisite_filename.replace(' ', '\\ ')
|
|
makefile.write(f"{target_filename}: {escaped_prereq} | stickers\n")
|
|
# makefile: recipe ffmpeg base
|
|
makefile.write('\tffmpeg ')
|
|
# makefile: specify format if present
|
|
if sticker_description['input']['base'].get('format'):
|
|
makefile.write(f"-f {sticker_description['input']['base']['format']} ")
|
|
# makefile: force input framerate if present
|
|
if sticker_description['input']['base'].get('framerate'):
|
|
makefile.write(f"-framerate {sticker_description['input']['base']['framerate']} ")
|
|
#makefile: input base file
|
|
input_file = quote(str(path_stickers_source_dir \
|
|
/ sticker_description['input']['base']['input']
|
|
)
|
|
)
|
|
makefile.write(f"-i {input_file} ")
|
|
# makefile: video filter: base
|
|
makefile.write('-vf ')
|
|
# makefile: video filter: change transparent color
|
|
if sticker_description['input'].get('colors',{}).get('transparent'):
|
|
makefile.write("geq=\\''")
|
|
transparent = sticker_description['input']['colors']['transparent']
|
|
makefile.write(f"r=if(lt(alpha(X,Y),128),{transparent[0]},r(X,Y)):"
|
|
f"g=if(lt(alpha(X,Y),128),{transparent[1]},g(X,Y)):"
|
|
f"b=if(lt(alpha(X,Y),128),{transparent[2]},b(X,Y)):"
|
|
"a=alpha(X,Y)'\\',"
|
|
)
|
|
# makefile: video filter: scale
|
|
out_width = sticker_description.get('output', {}).get('width', 512)
|
|
out_height = sticker_description.get('output', {}).get('height', 512)
|
|
makefile.write(f"scale=w={out_width}:h={out_height}:force_original_aspect_ratio=decrease")
|
|
# makefile: output file speed
|
|
if sticker_description.get('output', {}).get('speed'):
|
|
makefile.write(f",setpts={sticker_description['output']['speed']}")
|
|
# makefile: video filter: finalize
|
|
makefile.write(' ')
|
|
# makefile: output file options
|
|
makefile.write(f"{out_formats[output_ext]['options']} ")
|
|
# makefile: replace output without confirmation
|
|
makefile.write('-y ')
|
|
# makefile: output file
|
|
makefile.write(quote(str(target_filename)))
|
|
# makefile: finalize recipe
|
|
makefile.write('\n')
|
|
makefile.write(f"all: {' '.join(targets_list)} | stickers\n")
|