From 615079070a5521c26b5864b4f5560f02c7cd63a3 Mon Sep 17 00:00:00 2001 From: Elias Schriefer Date: Fri, 12 Apr 2024 18:04:43 +0200 Subject: [PATCH] Add script and config --- ffmpeg-mka-thumbnailer | 102 +++++++++++++++++++++++++++++++++++++++++ ffmpeg-mka.thumbnailer | 4 ++ 2 files changed, 106 insertions(+) create mode 100755 ffmpeg-mka-thumbnailer create mode 100644 ffmpeg-mka.thumbnailer diff --git a/ffmpeg-mka-thumbnailer b/ffmpeg-mka-thumbnailer new file mode 100755 index 0000000..2bec0bb --- /dev/null +++ b/ffmpeg-mka-thumbnailer @@ -0,0 +1,102 @@ +#!/usr/bin/zsh + +help_args=(-h --help) +if [[ "${@:*help_args}" != "" ]] { + cat <<-END + ffmpeg-mka-thumbnailer + Extract album cover art from Matroska audio files (.mka) + + Usage: + ffmpeg-mka-thumbnailer [OPTIONS] -i -o + + Options: + -h, --help Show this help text + -i Matroska audio file to read + -o Where to write the output + -s Not implemented yet + END + exit 1 +} + +required_args=(-i -o) +if [[ "${==${(o)@:*required_args}}" != "-i -o" ]] { + supplied_args=("${@:*required_args}") + for p ("${(@)required_args:|supplied_args}") { + echo "Missing argument $p" >&2 + } + exit 1 +} + +input= +output= + +while [[ $# -gt 0 ]] { + case "$1" { + -i) + input="$2" + shift + ;; + -o) + output="$2" + shift + ;; + -s) + echo "-s is not implemented yet" >&2 + exit 1 + ;; + *) + echo "Unrecognized option $1" >&2 + exit 1 + ;; + } + + shift +} + +if [[ ! (-r "$input" && -f "$input") ]] { + echo "Input $input is not a readable file" >&2 + exit 1 +} + +dir="$(dirname "$output")" +if [[ ! (-d "$dir" && -w "$dir") ]] { + echo "Output $output is not under a writable directory" >&2 + exit 1 +} + +attachment_streams="$(ffprobe -of json -v error -select_streams t -show_streams "$input" | jq -c)" + +if [[ "$(echo -n "$attachment_streams" | jq ".streams | length")" -lt 1 ]] { + echo "Input does not have any attachment streams" >&2 + exit 1 +} + +attachment_filename="$(echo -n "$attachment_streams" | jq -r '[.streams[] | select((.tags.filename | (startswith("cover") or startswith("small_cover"))) and (.tags.mimetype | startswith("image/")))] | sort_by(.index) | first | .tags.filename | strings')" + +if [[ "$attachment_filename" = "" ]] { + echo "No suitable attachment found" >&2 + exit 1 +} + +prev_pwd="$(pwd)" +tmpdir="$(mktemp -d)" +tmpdir_success=$? +if [[ $tmpdir_success -ne 0 ]] { + echo "Could not create temporary directory" >&2 + exit 1 +} else { + cd "$tmpdir" +} + +return_code=0 +repeat 1 { + ffmpeg -v error -dump_attachment:t "$attachment_filename" -f matroska -i "$input" -t 0 -f null null || { return_code=$?; break } + if [[ "${attachment_filename##*.}" = "${output##*.}" || ("${attachment_filename##*.}" =~ ^jpe?g$ && "${output##*.}" =~ ^jpe?g$) ]] { + mv "$attachment_filename" "$output" + } else { + convert "$attachment_filename" "$output" || { return_code=$?; break } + } +} +cd "$prev_pwd" +rm -rf "$tmpdir" +exit $return_code diff --git a/ffmpeg-mka.thumbnailer b/ffmpeg-mka.thumbnailer new file mode 100644 index 0000000..82924f8 --- /dev/null +++ b/ffmpeg-mka.thumbnailer @@ -0,0 +1,4 @@ +[Thumbnailer Entry] +TryExec=ffmpeg-mka-thumbnailer +Exec=ffmpeg-mka-thumbnailer -i %i -o %o +MimeType=audio/x-matroska;