stickerpack_assembler/stickergen/actions.py

77 lines
3.8 KiB
Python

#!/usr/bin/env python
# -*- coding: utf-8 -*-
from yaml import safe_load
from pathlib import Path
import logging
path_stickers_root = Path('.')
path_stickers_output_dir = path_stickers_root / 'stickers'
path_stickers_output_dir.mkdir(exist_ok=True)
path_stickers_source_dir = path_stickers_root / 'src'
path_stickers_data_dir = path_stickers_root / 'data'
logging.basicConfig(
filename=str(path_stickers_root / 'make.log'),
level=logging.DEBUG)
log = logging.getLogger('stickers.actions')
log.setLevel(logging.DEBUG)
log.debug('path_stickers_root = %s', path_stickers_root)
log.debug('path_stickers_data_dir = %s', path_stickers_data_dir)
def mktask(out_format, out_ext, ffmpeg_opts):
def task_stickers():
log = logging.getLogger('stickers.actions.task_'+out_format)
log.setLevel(logging.DEBUG)
log.debug(f'{list(path_stickers_data_dir.glob("*.yaml"))}')
for sticker_description_file in path_stickers_data_dir.glob('*.yaml'):
ffmpeg_action = ['ffmpeg']
file_dep = [ sticker_description_file ]
log.debug('input description file: %s', sticker_description_file)
with open(sticker_description_file) as yaml_file:
sticker_description = safe_load(yaml_file)
if sticker_description.get('disabled', False):
continue
log.debug('stuff %s', sticker_description)
out_filename = f'{sticker_description["name"]}.{out_ext}'
target_filename = path_stickers_output_dir / out_filename
if sticker_description['input']['base'].get('format','') not in ('lavfi', 'image2'):
file_dep.append(f'{path_stickers_source_dir/sticker_description["input"]["base"]["input"]}')
if sticker_description['input']['base'].get('format'):
ffmpeg_action.append('-f')
ffmpeg_action.append(sticker_description['input']['base']['format'])
if sticker_description['input']['base'].get('framerate'):
ffmpeg_action.append('-framerate')
ffmpeg_action.append(sticker_description['input']['base']['framerate'])
ffmpeg_action.append('-i')
ffmpeg_action.append(path_stickers_source_dir/sticker_description['input']['base']['input'])
ffmpeg_action.append('-vf')
filterspec = ''
if sticker_description['input'].get('colors',{}).get('transparent'):
transparent = sticker_description['input']['colors']['transparent']
filterspec += 'geq=\''
filterspec += f'r=if(lt(alpha(X,Y),128),{transparent[0]},r(X,Y)):'
filterspec += f'g=if(lt(alpha(X,Y),128),{transparent[1]},g(X,Y)):'
filterspec += f'b=if(lt(alpha(X,Y),128),{transparent[2]},b(X,Y)):'
filterspec += f'a=alpha(X,Y)\','
out_width = sticker_description.get('output', {}).get('width', 512)
out_height = sticker_description.get('output', {}).get('height', 512)
filterspec += f'scale=w={out_width}:h={out_height}:force_original_aspect_ratio=decrease'
if sticker_description.get('output', {}).get('speed'):
filterspec += f',setpts={sticker_description['output']['speed']}'
ffmpeg_action.append(filterspec)
ffmpeg_action.append('-y')
ffmpeg_action += ffmpeg_opts
ffmpeg_action.append(target_filename)
yield {
'actions': [ffmpeg_action],
'name': out_filename,
'targets': [target_filename],
'file_dep': file_dep
}
return task_stickers
task_png = mktask('png', 'png', ['-preset:v', 'icon', '-frames', '1'])
task_webm = mktask('webm', 'webm', ['-codec:v', 'libvpx-vp9', '-map', 'v', '-pix_fmt', 'yuva420p'])
task_webp = mktask('webp', 'webp', ['-lossless', '1'])