From 41e930c5c55ec5ba7ce83e64b6c3d599403d12a4 Mon Sep 17 00:00:00 2001 From: Aleksey Chubukov Date: Sat, 30 Sep 2023 13:49:58 +0400 Subject: [PATCH] rewrite in python --- Makefile | 14 -------- make.jq | 18 ---------- make.py | 101 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 101 insertions(+), 32 deletions(-) delete mode 100644 Makefile delete mode 100644 make.jq create mode 100644 make.py diff --git a/Makefile b/Makefile deleted file mode 100644 index f2da5cc..0000000 --- a/Makefile +++ /dev/null @@ -1,14 +0,0 @@ -.PHONY: all subrecipes_png subrecipes_webp -makeFileDir := $(dir $(abspath $(lastword $(MAKEFILE_LIST)))) -all: subrecipes_png subrecipes_webp - true -subrecipes_webp: stickers.json $(makeFileDir)/make.jq - mkdir -p ./stickers - jq -r -f make.jq --arg out_format '-f webp -preset:v icon -frames 1 ' --arg out_ext webp stickers.json > src/Makefile.webp.mk - $(MAKE) -C src -f Makefile.webp.mk -subrecipes_png: stickers.json $(makeFileDir)/make.jq - mkdir -p ./stickers - jq -r -f make.jq --arg out_format '-preset:v icon -frames 1' --arg out_ext png stickers.json > src/Makefile.png.mk - $(MAKE) -C src -f Makefile.png.mk -stickers.json: - touch stickers.json diff --git a/make.jq b/make.jq deleted file mode 100644 index 6b85728..0000000 --- a/make.jq +++ /dev/null @@ -1,18 +0,0 @@ -#!/usr/bin/env jq -. as $stickers | [ - "allstickers_"+$out_ext+": " + ( [ $stickers[]| select(.disabled != "true") | "../stickers/sticker_"+.name+"."+$out_ext ] | join(" ")), - ($stickers[] | select(.disabled != "true") | [ - "../stickers/sticker_"+.name+"."+$out_ext + ": "+ ((.in[]| select(.format != "lavfi") | .input )|gsub("(\\s+)"; "\\ ")), - "ffmpeg " - +(if .in.base.format then "-f "+.in.base.format else "" end) +" -i "+ (.in.base.input|@sh) - + " -vf " - +(if .colors and .colors.transparent then "geq=\\''" - +"r=if(lt(alpha(X,Y),128),"+(.colors.transparent[0]|tostring)+",r(X,Y)):" - +"g=if(lt(alpha(X,Y),128),"+(.colors.transparent[1]|tostring)+",g(X,Y)):" - +"b=if(lt(alpha(X,Y),128),"+(.colors.transparent[2]|tostring)+",b(X,Y)):" - +"a=alpha(X,Y)'\\'," else "" end) - +"scale=w=512:h=512:force_original_aspect_ratio=decrease" - + " " - +"-y -lossless 1 "+$out_format+" ../stickers/sticker_"+.name+"."+$out_ext - ] | join("\n\t")) -] | join ("\n") diff --git a/make.py b/make.py new file mode 100644 index 0000000..b44ac93 --- /dev/null +++ b/make.py @@ -0,0 +1,101 @@ +#!/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")