]> gitweb.hhaalo.de Git - albentohandy.git/commitdiff
add picture to flac
authorBastian Dehn <hhaalo@arcor.de>
Fri, 25 Sep 2026 15:58:44 +0000 (17:58 +0200)
committerBastian Dehn <hhaalo@arcor.de>
Fri, 25 Sep 2026 15:58:44 +0000 (17:58 +0200)
src/lib/flac.sh
tests/flac.bats

index 308f35845aaf2ecfb6875e2579bc91af74742bce..3acf7c6cc368da17eec02efcb38513f29f8c26b8 100644 (file)
@@ -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
index d5a03b6b9f9ba97411b123ba25fb47d59a6b9720..41fd6b50d015be5b2cbd51d49633255a32e0a86d 100755 (executable)
@@ -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