diff --git a/flac-opus-compressing.sh b/flac-opus-compressing.sh index d775d42..2c63255 100755 --- a/flac-opus-compressing.sh +++ b/flac-opus-compressing.sh @@ -2,6 +2,7 @@ # Media Library FLAC to Opus Conversion Script # Processes multiple albums in a media library, converting all FLAC files to Opus 448kbps +# Also copies cover art files to maintain album artwork # Check if ffmpeg is installed if ! command -v ffmpeg &> /dev/null; then @@ -27,6 +28,7 @@ show_usage() { echo " -t, --test Test mode - show what would be processed" echo " -f, --force Force re-conversion of existing Opus files" echo " -b, --bitrate RATE Opus bitrate in kbps (default: 448)" + echo " --no-covers Skip copying cover art files" echo " -h, --help Show this help message" echo "" echo "Examples:" @@ -35,6 +37,7 @@ show_usage() { echo " $0 --test /path/to/music/library" echo " $0 --replace /path/to/music/library" echo " $0 -b 384 /path/to/music/library" + echo " $0 --no-covers /path/to/music/library" } # Default options @@ -43,10 +46,21 @@ REPLACE_MODE=false PRESERVE_MODE=false TEST_MODE=false FORCE_MODE=false +COPY_COVERS=true OPUS_BITRATE=192 PROCESSED_COUNT=0 SKIPPED_COUNT=0 ERROR_COUNT=0 +COVERS_COPIED=0 +COVERS_SKIPPED=0 + +# Common cover art filenames to look for +COVER_FILENAMES=("cover.jpg" "Cover.jpg" "COVER.jpg" "folder.jpg" "Folder.jpg" "FOLDER.jpg" + "album.jpg" "Album.jpg" "ALBUM.jpg" "front.jpg" "Front.jpg" "FRONT.jpg" + "cover.jpeg" "Cover.jpeg" "COVER.jpeg" "folder.jpeg" "Folder.jpeg" "FOLDER.jpeg" + "album.jpeg" "Album.jpeg" "ALBUM.jpeg" "front.jpeg" "Front.jpeg" "FRONT.jpeg" + "cover.png" "Cover.png" "COVER.png" "folder.png" "Folder.png" "FOLDER.png" + "album.png" "Album.png" "ALBUM.png" "front.png" "Front.png" "FRONT.png") # Parse command line arguments while [[ $# -gt 0 ]]; do @@ -75,6 +89,10 @@ while [[ $# -gt 0 ]]; do OPUS_BITRATE="$2" shift 2 ;; + --no-covers) + COPY_COVERS=false + shift + ;; -h|--help) show_usage exit 0 @@ -139,6 +157,57 @@ needs_conversion() { return 0 # Needs conversion } +# Function to copy cover art files +copy_cover_art() { + local source_dir="$1" + local dest_dir="$2" + local relative_path="$3" + + # Skip if covers are disabled + if [ "$COPY_COVERS" = false ]; then + return + fi + + # Skip if in replace or preserve mode (covers stay in original location) + if [ "$REPLACE_MODE" = true ] || [ "$PRESERVE_MODE" = true ]; then + return + fi + + # Look for cover art files + local found_cover=false + for cover_name in "${COVER_FILENAMES[@]}"; do + local cover_file="$source_dir/$cover_name" + if [ -f "$cover_file" ]; then + local dest_cover="$dest_dir/$cover_name" + + # Check if we need to copy (force mode or destination doesn't exist/is older) + if [ "$FORCE_MODE" = true ] || [ ! -f "$dest_cover" ] || [ "$cover_file" -nt "$dest_cover" ]; then + if [ "$TEST_MODE" = true ]; then + echo " WOULD COPY COVER: $relative_path/$cover_name" + else + echo " COPYING COVER: $cover_name" + if cp "$cover_file" "$dest_cover" 2>/dev/null; then + ((COVERS_COPIED++)) + else + echo " WARNING: Failed to copy $cover_name" + fi + fi + found_cover=true + else + echo " SKIP COVER: $cover_name (already exists and is newer)" + ((COVERS_SKIPPED++)) + found_cover=true + fi + # Only copy the first cover file found to avoid duplicates + break + fi + done + + if [ "$found_cover" = false ] && [ "$TEST_MODE" = false ]; then + echo " NO COVER: No cover art found in $(basename "$source_dir")" + fi +} + # Function to process a single FLAC file process_flac_file() { local input_file="$1" @@ -231,6 +300,7 @@ process_flac_file() { process_album() { local album_dir="$1" local album_name=$(basename "$album_dir") + local relative_album_path="${album_dir#$MEDIA_LIBRARY/}" echo "Processing album: $album_name" @@ -251,6 +321,12 @@ process_album() { process_flac_file "$flac_file" "$relative_path" done + # Copy cover art if needed (only for output directory mode) + if [ "$REPLACE_MODE" = false ] && [ "$PRESERVE_MODE" = false ]; then + local output_album_dir="$OUTPUT_DIR/$relative_album_path" + copy_cover_art "$album_dir" "$output_album_dir" "$relative_album_path" + fi + echo "" } @@ -274,6 +350,12 @@ if [ "$FORCE_MODE" = true ]; then echo "Force: Enabled (will re-convert existing Opus files)" fi +if [ "$COPY_COVERS" = true ]; then + echo "Covers: Enabled (will copy album artwork)" +else + echo "Covers: Disabled" +fi + echo "" # Find all directories that contain FLAC files (potential albums) @@ -304,6 +386,11 @@ echo "Converted: $PROCESSED_COUNT files" echo "Skipped: $SKIPPED_COUNT files (already converted or newer)" echo "Errors: $ERROR_COUNT files" +if [ "$COPY_COVERS" = true ]; then + echo "Covers copied: $COVERS_COPIED files" + echo "Covers skipped: $COVERS_SKIPPED files (already exist or newer)" +fi + if [ "$ERROR_COUNT" -gt 0 ]; then exit 1 fi