-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmp3split.sh
More file actions
executable file
·314 lines (245 loc) · 7.8 KB
/
mp3split.sh
File metadata and controls
executable file
·314 lines (245 loc) · 7.8 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#!/usr/bin/env bash
# mp3split.sh
# Based on Reddit comment by https://old.reddit.com/user/bayarookie:
# https://old.reddit.com/r/ffmpeg/comments/jn6sny/split_audio_file_into_smaller_file_based_on/gb0mumn/
set -euo pipefail
shopt -s extglob
usage() {
printf 'Usage: %s TIMESTAMP_FILE MP3_FILE\n' "$(basename "$0")" >&2
}
die() {
printf 'Error: %s\n' "$*" >&2
exit 1
}
require_command() {
command -v "$1" >/dev/null 2>&1 || die "Required command not found: $1"
}
trim_whitespace() {
local value=$1
value=${value##+([[:space:]])}
value=${value%%+([[:space:]])}
printf '%s\n' "$value"
}
is_valid_timestamp() {
local timestamp=$1
local first second third
[[ $timestamp =~ ^[0-9]+:[0-9]{2}(:[0-9]{2})?(\.[0-9]+)?$ ]] || return 1
IFS=':' read -r first second third <<< "$timestamp"
if [[ -n ${third:-} ]]; then
(( 10#$second <= 59 )) || return 1
second=${third%%.*}
else
second=${second%%.*}
fi
(( 10#$second <= 59 ))
}
timestamp_to_milliseconds() {
local timestamp=$1
local first second third
local hours=0
local minutes
local seconds
local whole_seconds
local fraction=
IFS=':' read -r first second third <<< "$timestamp"
if [[ -n ${third:-} ]]; then
hours=$first
minutes=$second
seconds=$third
else
minutes=$first
seconds=$second
fi
whole_seconds=${seconds%%.*}
if [[ $seconds == *.* ]]; then
fraction=${seconds#*.}
fi
fraction=${fraction:0:3}
while ((${#fraction} < 3)); do
fraction="${fraction}0"
done
printf '%s\n' "$(( ((10#$hours * 60 + 10#$minutes) * 60 + 10#$whole_seconds) * 1000 + 10#$fraction ))"
}
sanitize_output_name() {
local title=$1
title=$(printf '%s' "$title" | LC_ALL=C sed -E 's#[/\\]#-#g; s/[[:cntrl:]]//g; s/[<>:"|?*]/_/g; s/[[:space:]]+/ /g')
title=$(trim_whitespace "$title")
while [[ $title == .* ]]; do
title=${title#.}
done
title=$(trim_whitespace "$title")
if [[ -z $title ]]; then
title='track'
fi
printf '%s\n' "$title"
}
output_path_reserved() {
local candidate=$1
local reserved
if ((${#planned_outputs[@]} == 0)); then
return 1
fi
for reserved in "${planned_outputs[@]}"; do
if [[ $reserved == "$candidate" ]]; then
return 0
fi
done
return 1
}
next_output_path() {
local base_dir=$1
local track_number=$2
local stem=$3
local candidate
local suffix=2
printf -v candidate '%s/%02d - %s.mp3' "$base_dir" "$track_number" "$stem"
while [[ -e $candidate ]] || output_path_reserved "$candidate"; do
printf -v candidate '%s/%02d - %s-%d.mp3' "$base_dir" "$track_number" "$stem" "$suffix"
((suffix++))
done
planned_outputs+=("$candidate")
next_output_path_result=$candidate
}
if [[ $# -ne 2 ]]; then
usage
exit 1
fi
timestamp_file=$1
audio_file=$2
[[ -r $timestamp_file ]] || die "Timestamp file is not readable: $timestamp_file"
[[ -r $audio_file ]] || die "Audio file is not readable: $audio_file"
require_command ffmpeg
require_command ffprobe
duration=$(
ffprobe \
-v error \
-sexagesimal \
-show_entries format=duration \
-of default=noprint_wrappers=1:nokey=1 \
-- "$audio_file"
) || die "Unable to read audio duration with ffprobe: $audio_file"
duration=$(trim_whitespace "$duration")
[[ -n $duration ]] || die "ffprobe returned an empty duration for: $audio_file"
is_valid_timestamp "$duration" || die "ffprobe returned an invalid duration: $duration"
attached_picture_stream=$(
ffprobe \
-v error \
-select_streams v \
-show_entries stream=index:stream_disposition=attached_pic \
-of csv=p=0 \
-- "$audio_file" |
awk -F',' '$2 == 1 { print $1; exit }'
) || die "Unable to inspect album art streams with ffprobe: $audio_file"
duration_ms=$(timestamp_to_milliseconds "$duration")
start_times=()
track_titles=()
sanitized_titles=()
output_paths=()
planned_outputs=()
artist_name=
album_title=
year_value=
line_number=0
previous_ms=-1
while IFS= read -r raw_line || [[ -n $raw_line ]]; do
((line_number += 1))
line=$(trim_whitespace "$raw_line")
if [[ -z $line ]]; then
continue
fi
if [[ ${line:0:1} == '#' ]]; then
comment_text=$(trim_whitespace "${line#\#}")
if [[ -n $comment_text ]]; then
if [[ $comment_text =~ ^([^:]+):[[:space:]]*(.+)$ ]]; then
metadata_key=$(trim_whitespace "${BASH_REMATCH[1]}")
metadata_value=$(trim_whitespace "${BASH_REMATCH[2]}")
metadata_key=$(printf '%s' "$metadata_key" | tr '[:lower:]' '[:upper:]')
case "$metadata_key" in
ARTIST)
artist_name=$metadata_value
;;
ALBUM)
album_title=$metadata_value
;;
YEAR)
year_value=$metadata_value
;;
esac
fi
fi
continue
fi
if [[ ! $line =~ ^([^[:space:]]+)[[:space:]]+(.+)$ ]]; then
die "Invalid timestamp entry on line $line_number: $raw_line"
fi
timestamp=${BASH_REMATCH[1]}
title=$(trim_whitespace "${BASH_REMATCH[2]}")
is_valid_timestamp "$timestamp" || die "Invalid timestamp on line $line_number: $timestamp"
[[ -n $title ]] || die "Missing output title on line $line_number"
current_ms=$(timestamp_to_milliseconds "$timestamp")
(( current_ms < duration_ms )) || die "Timestamp on line $line_number is not before the audio duration ($duration): $timestamp"
(( current_ms > previous_ms )) || die "Timestamp on line $line_number is not greater than the previous timestamp: $timestamp"
previous_ms=$current_ms
sanitized_title=$(sanitize_output_name "$title")
start_times+=("$timestamp")
track_titles+=("$title")
sanitized_titles+=("$sanitized_title")
done < "$timestamp_file"
(( ${#start_times[@]} > 0 )) || die "Timestamp file did not contain any valid entries: $timestamp_file"
track_total=${#start_times[@]}
artist_dir=$(sanitize_output_name "${artist_name:-Unknown Artist}")
album_dir=$(sanitize_output_name "${album_title:-Unknown Album}")
if [[ -n ${year_value:-} ]]; then
album_dir="${album_dir} (${year_value})"
fi
output_dir="${artist_dir}/${album_dir}"
mkdir -p -- "$output_dir"
for i in "${!sanitized_titles[@]}"; do
next_output_path "$output_dir" "$((i + 1))" "${sanitized_titles[i]}"
output_paths+=("$next_output_path_result")
done
for i in "${!start_times[@]}"; do
next_index=$((i + 1))
track_number=$((i + 1))
if (( next_index < ${#start_times[@]} )); then
end_time=${start_times[next_index]}
else
end_time=$duration
fi
printf 'Writing %s\n' "${output_paths[i]}"
ffmpeg_cmd=(
ffmpeg
-n
-ss "${start_times[i]}"
-to "$end_time"
-i "$audio_file"
-map 0:a
-map_chapters -1
-c copy
-id3v2_version 3
-write_id3v1 1
-map_metadata 0
-metadata "title=${track_titles[i]}"
-metadata "track=${track_number}/${track_total}"
)
if [[ -n ${attached_picture_stream:-} ]]; then
ffmpeg_cmd+=(
-map "0:${attached_picture_stream}"
-disposition:v:0 attached_pic
)
fi
if [[ -n ${album_title:-} ]]; then
ffmpeg_cmd+=(-metadata "album=$album_title")
fi
if [[ -n ${artist_name:-} ]]; then
ffmpeg_cmd+=(-metadata "artist=$artist_name")
fi
if [[ -n ${year_value:-} ]]; then
ffmpeg_cmd+=(
-metadata "date=$year_value"
-metadata "year=$year_value"
)
fi
ffmpeg_cmd+=("${output_paths[i]}")
"${ffmpeg_cmd[@]}"
done