-
Notifications
You must be signed in to change notification settings - Fork 34
/
split
executable file
·86 lines (70 loc) · 2.68 KB
/
split
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
#!/bin/bash
step=15 ext=
opts_default=( -acodec copy -vn )
[[ -z "$1" || -z "$2" || "$1" = -h || "$1" = --help ]] && {
cmd=$(basename "$0")
echo >&2 "Usage:"
echo >&2 " $cmd file-to-split.mp3 prefix [ chunk-len-in-minutes ] [ ext ] [ ffmpeg opts ]"
echo >&2 " $cmd prefix [ chunk-len-in-minutes ] [ ext ] [ ffmpeg opts ] -- file1.mp3 file2.mp3 ..."
echo >&2 "Example: $cmd some-long-book.mp3 slb"
echo >&2 "Example: $cmd slb -- some-long-book-p1.mp3 some-long-book-p2.mp3 ..."
echo >&2 " Resulting files will be: slb-001.mp3 slb-002.mp3 ..."
echo >&2 " Default length: $step minutes"
echo >&2 " Default ffmpeg opts (only good for audio files!): ${opts_default[@]}"
echo >&2 " Special ffmpeg opts (must be before others, if any, in the same order):"
echo >&2 " -: don't pass default ones"
echo >&2 " -v: -c copy -bsf:a aac_adtstoasc"
echo >&2 "Example (transcode to ogg): $cmd some-file.m4b sf-split 30 ogg -f ogg"
echo >&2 "Example: $cmd sf-split 30 ogg - -new -ffmpeg -opts -f ogg -- some-file-1.m4b ..."
exit 1
}
set -e -o pipefail
arg_opts=() arg_src=() arg_src_flag=
for arg in "$@"; do
[[ "$arg" != -- ]] || { arg_src_flag=t; continue; }
[[ -z "$arg_src_flag" ]]\
&& arg_opts+=( "$arg" ) || arg_src+=( "$arg" )
done
[[ -n "$arg_src_flag" || "${#arg_opts[@]}" -eq 0 ]] || {
arg_src+=( "${arg_opts[0]}" )
arg_opts=( "${arg_opts[@]:1}" )
}
prefix=${arg_opts[0]}
[[ -n "$prefix" ]] || { echo >&2 "ERROR: no prefix specified"; exit 1; }
[[ -z "${arg_opts[1]}" ]] || step=${arg_opts[1]}
[[ -z "${arg_opts[2]}" ]] || ext=${arg_opts[2]}
step=$(( $step * 60 ))
opts=()
[[ -z "${arg_opts[3]}" ]] || {
opts=( "${arg_opts[@]:3}" )
[[ "${opts[0]}" != - ]] || { opts_default=(); opts=( "${opts[@]:1}" ); }
[[ "${opts[0]}" != -v ]] || opts=( -c copy -bsf:a aac_adtstoasc "${opts[@]:1}" )
}
opts=( "${opts_default[@]}" "${opts[@]}" )
src_lens=()
for src in "${arg_src[@]}"; do
len=$( ffprobe "$src" 2>&1 |
awk 'match($0,/Duration: (\S+)\./,a) {split(a[1],b,":");print b[1]*3600+b[2]*60+b[3]}' )
[[ -n "$len" && "$len" -gt 0 ]] || {
echo >&2 "ERROR: Failed to get duration for: $src"; exit 1; }
src_lens+=( "$len" )
done
[[ "${#arg_src[@]}" -eq 1 ]] && n_src= || n_src=0
for src in "${arg_src[@]}"; do
len=${src_lens[${n_src:-0}]}
[[ -n "$ext" ]] || ext=${src##*.}
n=1
ns=$(( ($len / $step) + 1 ))
while [[ $n -le $ns ]]; do
s=$(( ($n-1) * $step ))
e=$(( $n * $step ))
[[ -z "$n_src" ]] && dst= || dst="$(printf '%03d' $(( $n_src + 1 )))."
dst+=$(printf '%03d' $n)
dst="${prefix}-${dst}.${ext}"
echo "---- $dst [$n/$ns, $s-$e]"
ffmpeg -loglevel warning -y -i "$src" "${opts[@]}" -ss $s -to $e "$dst" </dev/null
(( n += 1 ))
done
[[ -z "$n_src" ]] || (( n_src += 1 ))
done
: