From c4e99cd3b95461fa72cbd73fd10b36326c390661 Mon Sep 17 00:00:00 2001 From: Bastian Dehn Date: Fri, 25 Sep 2026 17:58:44 +0200 Subject: [PATCH] add picture to flac --- src/lib/flac.sh | 22 +++++++++++++++++++++- tests/flac.bats | 31 +++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/src/lib/flac.sh b/src/lib/flac.sh index 308f358..3acf7c6 100644 --- a/src/lib/flac.sh +++ b/src/lib/flac.sh @@ -9,6 +9,26 @@ flac_errlog() return $lastexit } +flac_add_picture() +{ + local work_path="$1" + local flacs= + mapfile -t flacs < <(find "$work_path" -name '*.flac' | sort) + + local output= + local filepath= + local flac= + for flac in "${flacs[@]}"; do + output=${flac/.flac/-out.flac} + filepath=${flac%/*} + metaflac \ + --import-picture-from="3|image/jpeg|||$filepath/cover.jpg" \ + --output-name="$output" \ + "$flac" + mv "$output" "$flac" + done +} + flac_check_programs() { command -v metaflac > /dev/null \ @@ -19,10 +39,10 @@ flac_check_programs() flac_encode() { local work_path="$1" - [ -n "$work_path" ] \ || flac_errlog "missing work path" \ || return $? + flac_add_picture "$work_path" } flac_check_programs || return $? \ No newline at end of file diff --git a/tests/flac.bats b/tests/flac.bats index d5a03b6..41fd6b5 100755 --- a/tests/flac.bats +++ b/tests/flac.bats @@ -3,9 +3,17 @@ setup() { bats_load_library 'bats-support' bats_load_library 'bats-assert' + mkdir --parents "build/tests/output" + export METAFLAC_LOG="build/metaflac.log" source "src/lib/flac.sh" } +teardown() { + rm --recursive --force "build" + rm --force "$METAFLAC_LOG" + unset METAFLAG_LOG +} + # bats test_tags=flac:check_programs @test "failed: check programs missing metaflac" { command() { @@ -24,4 +32,27 @@ setup() { assert_failure assert_line --index 0 "❌ ERROR: missing work path" +} + +# bats test_tags=flac:addpicture +@test "success: add picture to flac" { + echo "testfile" > "build/tests/output/file.flac" + metaflac() { + { + printf "%s\n" "${#@}" + printf "%s\n" "${@}" + } > "$METAFLAC_LOG" + local file="$3" + cp "$file" "${file/.flac/-out.flac}" + } + + run flac_encode "build/tests/output" + + assert_success + assert_equal "$(awk 'NR==1 {print; exit}' "$METAFLAC_LOG")" "3" + assert_equal "$(awk 'NR==2 {print; exit}' "$METAFLAC_LOG")" "--import-picture-from=3|image/jpeg|||build/tests/output/cover.jpg" + assert_equal "$(awk 'NR==3 {print; exit}' "$METAFLAC_LOG")" "--output-name=build/tests/output/file-out.flac" + assert_equal "$(awk 'NR==4 {print; exit}' "$METAFLAC_LOG")" "build/tests/output/file.flac" + [ ! -f "build/tests/output/file-out.flac" ] + [ -f "build/tests/output/file.flac" ] } \ No newline at end of file -- 2.47.3