Add script and config

This commit is contained in:
Elias Schriefer 2024-04-12 18:04:43 +02:00
parent 5df0f6515d
commit 615079070a
2 changed files with 106 additions and 0 deletions

102
ffmpeg-mka-thumbnailer Executable file
View File

@ -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 <INPUT FILE> -o <OUTPUT FILE>
Options:
-h, --help Show this help text
-i <INPUT FILE> Matroska audio file to read
-o <OUTPUT FILE> Where to write the output
-s <SIZE> 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

4
ffmpeg-mka.thumbnailer Normal file
View File

@ -0,0 +1,4 @@
[Thumbnailer Entry]
TryExec=ffmpeg-mka-thumbnailer
Exec=ffmpeg-mka-thumbnailer -i %i -o %o
MimeType=audio/x-matroska;