From 520e6e8586b46ad3d9f7dba5f24a0815f5651e36 Mon Sep 17 00:00:00 2001 From: Willy-JL <49810075+Willy-JL@users.noreply.github.com> Date: Tue, 24 Sep 2024 07:16:07 +0100 Subject: [PATCH] Add preview conversion script --- .utils/previews.py | 34 ++++++++++++++++++++++++++++++++++ Makefile | 12 ++++++++++++ README.md | 5 ++++- requirements.txt | 1 + 4 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 .utils/previews.py diff --git a/.utils/previews.py b/.utils/previews.py new file mode 100644 index 00000000..1861dd33 --- /dev/null +++ b/.utils/previews.py @@ -0,0 +1,34 @@ +#!/usr/bin/env python3 +import ffmpeg +import pathlib + +import common + + +def convert_mp4_previews(pack_set: pathlib.Path) -> None: + previews = pack_set / "preview" + + for preview in previews.glob("*.mp4"): + probe = ffmpeg.probe(preview) + + for stream in probe["streams"]: + if stream["codec_type"] == "video": + break + else: # Did not break, so no video stream + print(f"Preview has no video: {preview.relative_to(common.packs_root)}") + continue + + convert = ffmpeg.input(str(preview)) + + # Personal ease of use: when captured from qFlipper with BlueRecorder, + # this crops to fit the preview window perfectly + if stream["width"] == 862 and stream["height"] == 532: + convert = convert.crop(width=512, height=256, x=66, y=82) + + convert = convert.output(str(preview.with_suffix(".gif"))) + convert.run() + + +if __name__ == "__main__": + for pack_set in common.cli_pack_sets(): + convert_mp4_previews(pack_set) diff --git a/Makefile b/Makefile index 2d648ffb..3f3a1d1b 100644 --- a/Makefile +++ b/Makefile @@ -24,6 +24,18 @@ endif check: venv requirements ./.venv/bin/python3 .utils/check.py $(CHECK_ARGS) +# If the first argument is "previews"... +ifeq (previews,$(firstword $(MAKECMDGOALS))) + # use the rest as arguments for "previews" + PREVIEWS_ARGS := $(wordlist 2,$(words $(MAKECMDGOALS)),$(MAKECMDGOALS)) + # ...and turn them into do-nothing targets + $(eval $(PREVIEWS_ARGS):;@:) +endif + +.PHONY: previews +previews: venv requirements + ./.venv/bin/python3 .utils/previews.py $(PREVIEWS_ARGS) + venv: python3 -m venv .venv diff --git a/README.md b/README.md index e68f59f8..768609bf 100644 --- a/README.md +++ b/README.md @@ -13,8 +13,11 @@ The scope of this repo is keeping the asset packs themselves updated and recompi You can do this by: ```bash make repack [pack-name] + # OR + python .utils/repack.py [pack-name] ``` Currently we don't have a convenient way of generating previews. For now what we do is: - For Icons: use [qFlipper](https://flipperzero.one/update), click 'Save Screenshot' -- For Anims: use [qFlipper](https://flipperzero.one/update), record it (e.g. with [Blue Recorder](https://flathub.org/apps/sa.sy.bluerecorder)), cut it with `ffmpeg -i in.gif -vf "crop=512:256:66:82" out.gif` \ No newline at end of file +- For Anims: use [qFlipper](https://flipperzero.one/update), record it, put `.mp4` in `pack-name/preview` folder, run `make previews [pack-name]` (or `python .utils/previews.py [pack-name]`) to convert to `.gif` + - To make cropping easier, you can use [Blue Recorder](https://flathub.org/apps/sa.sy.bluerecorder)'s Window capture: the above script will notice the right pixel sizes (862x532) and crop to fit qFlipper's preview \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 1ce72ad0..f03dcebf 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ black==24.3.0 heatshrink2==0.13.0 pillow==10.3.0 +ffmpeg-python==0.2.0