-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathflac-alac-converter.sh
More file actions
124 lines (108 loc) · 3.05 KB
/
Copy pathflac-alac-converter.sh
File metadata and controls
124 lines (108 loc) · 3.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/bin/zsh
#
# flac-alac-converter.sh — Convert FLAC albums to Apple Lossless (ALAC/m4a)
#
# Usage:
# ./flac-alac-converter.sh Convert all subdirs containing FLAC files
# ./flac-alac-converter.sh --dry-run Preview what would happen, no files written
# ./flac-alac-converter.sh -n Same as --dry-run
# ./flac-alac-converter.sh file.flac Convert a single FLAC file (output next to source)
# ./flac-alac-converter.sh --dry-run file.flac Dry-run a single file
# ./flac-alac-converter.sh /path/to/album Convert a specific album directory
#
# Requirements:
# ffmpeg (brew install ffmpeg)
#
# For each album directory, a sibling directory is created with " ALA" appended,
# e.g. "AlbumName ALA". FLAC files are converted to ALAC; jpg and cue
# files are copied as-is. Embedded cover art is preserved.
DRY_RUN=0
SINGLE_FILE=""
TARGET_DIR=""
for arg in "$@"; do
case "$arg" in
--dry-run|-n) DRY_RUN=1 ;;
*.flac) SINGLE_FILE="$arg" ;;
*)
if [[ -d "$arg" ]]; then
TARGET_DIR="$arg"
else
echo "Unknown argument: $arg"; exit 1
fi
;;
esac
done
command -v ffmpeg >/dev/null || { echo "ffmpeg not found"; exit 1; }
(( DRY_RUN )) && echo "[dry-run — no files will be written]"
run() {
if (( DRY_RUN )); then
echo " >> $*"
else
"$@"
fi
}
convert_album() {
local dir="$1"
local new_dir="${dir} ALA"
echo ""
echo "==> $dir"
run mkdir -p "$new_dir"
for ext in jpg cue; do
for f in "$dir"/*.$ext; do
[ -f "$f" ] || continue
echo " copy ${f##*/}"
run cp "$f" "$new_dir/"
done
done
local total=0
for flac in "$dir"/*.flac; do
[ -f "$flac" ] && (( total++ )) || true
done
local i=0
for flac in "$dir"/*.flac; do
[ -f "$flac" ] || continue
(( i++ )) || true
local m4a="$new_dir/${flac##*/}"
m4a="${m4a%.flac}.m4a"
echo " [$i/$total] ${flac##*/} -> ${m4a##*/}"
run ffmpeg -loglevel error -i "$flac" -c:a alac -c:v copy -y "$m4a"
done
echo " done ($i track(s))"
}
# Single-file mode
if [[ -n "$SINGLE_FILE" ]]; then
[[ -f "$SINGLE_FILE" ]] || { echo "File not found: $SINGLE_FILE"; exit 1; }
local m4a="${SINGLE_FILE%.flac}.m4a"
echo "Converting: ${SINGLE_FILE##*/} -> ${m4a##*/}"
run ffmpeg -loglevel error -i "$SINGLE_FILE" -c:a alac -c:v copy -y "$m4a"
echo "Done."
exit 0
fi
# Single-directory mode
if [[ -n "$TARGET_DIR" ]]; then
convert_album "$TARGET_DIR"
echo ""
echo "Done."
exit 0
fi
# Batch mode: discover all subdirectories that contain FLAC files
found=0
for dir in */; do
dir="${dir%/}"
# Skip directories we already created
[[ "$dir" == *" ALA" ]] && continue
# Only process dirs that contain at least one FLAC file
local has_flac=0
for f in "$dir"/*.flac; do
[ -f "$f" ] && has_flac=1 && break
done
(( has_flac )) || continue
convert_album "$dir"
(( found++ )) || true
done
if (( found == 0 )); then
echo "No directories with FLAC files found in the current directory."
exit 1
fi
echo ""
echo "All albums converted!"