]> gitweb.hhaalo.de Git - albentohandy.git/commitdiff
add test for encode cover
authorBastian Dehn <hhaalo@arcor.de>
Fri, 25 Sep 2026 17:21:48 +0000 (19:21 +0200)
committerBastian Dehn <hhaalo@arcor.de>
Fri, 25 Sep 2026 17:23:51 +0000 (19:23 +0200)
src/lib/vorbis.sh
tests/vorbis.bats

index f25fc560dc7e96156f231d3d72b0a407b1ab63f6..4a99d5dfb51f351a4eda92f42cb9f5deebe58b31 100644 (file)
@@ -28,4 +28,53 @@ vorbis_check_programs()
                || return $?
 }
 
+vorbis_write_number_to_byte()
+{
+       local number="$1"
+       local hex_number=
+       hex_number=$(printf "%08x" "$number")
+
+       for i in {0..6..2}; do
+               printf "%b" "\x${hex_number:$i:2}"
+       done
+}
+
+vorbis_cover_to_metadata()
+{
+       local cover="$1"
+       local covermetadata="${cover/.jpg/.txt}"
+       local filesize=$(wc --bytes "$cover")
+       filesize=${filesize/ */}
+
+       {
+               # picture typ (jpg)
+               vorbis_write_number_to_byte "3"
+               # mime type (image/jpeg)
+               vorbis_write_number_to_byte "10"
+               printf "%s" "image/jpeg"
+               # description length (zero)
+               vorbis_write_number_to_byte "0"
+               # width (zero)
+               vorbis_write_number_to_byte "0"
+               # heigth (zero)
+               vorbis_write_number_to_byte "0"
+               # color depth (zero)
+               vorbis_write_number_to_byte "0"
+               # color count (zero)
+               vorbis_write_number_to_byte "0"
+               # file size
+               vorbis_write_number_to_byte "$filesize"
+               cat "$cover"
+       } > "$covermetadata"
+}
+
+vorbis_encode()
+{
+       local work_path="$1"
+       find "$work_path" -name '*.jpg' \
+               | sort \
+               | parallel --will-cite --ungroup \
+               vorbis_cover_to_metadata {}
+}
+
 vorbis_check_programs || return $?
\ No newline at end of file
index 4dc23d1a36a8425234665c6e33c60a6bae6c649c..9073924c4cf8af9cf010143d9784d9a6702adb6f 100755 (executable)
@@ -3,9 +3,14 @@
 setup() {
        bats_load_library 'bats-support'
        bats_load_library 'bats-assert'
+       mkdir --parents "build/tests/output"
        source "src/lib/vorbis.sh"
 }
 
+teardown() {
+       rm --recursive --force "build"
+}
+
 # bats test_tags=vorbis:check_programs
 @test "failed: check programs missing flac" {
        command() {
@@ -64,4 +69,25 @@ setup() {
 
        assert_failure
        assert_line --index 0 "❌ ERROR: command parallel not found"
+}
+
+# bats test_tags=vorbis:covermetadata
+@test "success: cover to metdata file" {
+       echo "testfile" > "build/tests/output/test.jpg"
+       {
+               printf "\x00\x00\x00\x03"
+               printf "\x00\x00\x00\x0a"
+               printf "image/jpeg"
+               printf "\x00\x00\x00\x00"
+               printf "\x00\x00\x00\x00"
+               printf "\x00\x00\x00\x00"
+               printf "\x00\x00\x00\x00"
+               printf "\x00\x00\x00\x00"
+               printf "\x00\x00\x00\x09"
+               printf "testfile\n"
+       } > "build/tests/output/expectedcover.txt"
+
+       run vorbis_cover_to_metadata "build/tests/output/test.jpg"
+
+       diff "build/tests/output/test.txt" "build/tests/output/expectedcover.txt"
 }
\ No newline at end of file