blob: 7af0f430316f7f4efe4285e262533d3be5a6ab7b (
plain) (
blame)
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
|
#!/bin/sh
# See LICENSE file for copyright and license details.
set -e
usage () {
printf 'usage %s: [-Dry] in-file [out-file]\n' "$0" >&2
exit 1
}
recursive=no
delete_in_file=no
ffmpeg_flag_y=
status=0
while getopts Dry flag; do
case "$flag" in
D)
delete_in_file=yes;;
r)
recursive=yes;;
y)
ffmpeg_flag_y=-y;;
?)
usage;;
esac
done
shift $(( ${OPTIND} - 1 ))
if test ! $# = 1 && test ! $# = 2; then
usage
fi
in_file="$1"
if test -z "${in_file}"; then
usage
fi
out_file=""
if test $# = 2; then
out_file="$2"
if test -z "${out_file}"; then
usage
fi
if test "${out_file}" = -; then
printf '%s: - is not supported for out-file\n' "$0" >&2
exit 1
fi
else
if test "${in_file}" = -; then
printf '%s: out-file must be specified if in-file is -\n' "$0" >&2
exit 1
fi
fi
getext () {
probed_file="$1"
if test ! "${probed_file}" -; then
probed_file="file:${probed_file}"
fi
ffprobe -v error -select_streams a:0 -show_entries stream=codec_name -of default=noprint_wrappers=1:nokey=1 -- "${probed_file}"
}
removeext () {
printf '%s\0' "$1" | tr '\0\n' '\n\0' | sed 's/\.[^.]*$//' | tr '\0' '\n'
}
extract () {
printf '\033[1m%s\033[m\n' "Extracting audio track from ${1}" >&2
printf '\033[1m%s\033[m\n' "and storing as ${2}" >&2
in_file_prefixed="${1}"
if test ! "${in_file_prefixed}" -; then
in_file_prefixed="file:${in_file_prefixed}"
fi
if ffmpeg -i "${in_file_prefixed}" -vn -acodec copy ${ffmpeg_flag_y} -- "file:${2}"; then
if test ${delete_in_file} = yes; then
unlink -- "${1}"
fi
fi
}
if test ${recursive} = no || test ! -d "${in_file}"; then
if test -z "${out_file}"; then
out_file="$(removeext "${in_file}").$(getext "${in_file}")"
fi
extract "${in_file}" "${out_file}"
exit $?
fi
recursive () {
for in_file in "${1}/"*; do
out_file="$2/$(basename -- "${in_file}")"
if test -d "${in_file}"; then
mkdir -p -- "${out_file}"
recursive "${in_file}" "${out_file}"
else
out_file="$(removeext "${out_file}").$(getext "${in_file}")"
if ! extract "${in_file}" "${out_file}"; then
status=1
fi
fi
done
if test ${delete_in_file} = yes; then
rmdir -- "${1}" 2>/dev/null || :
fi
}
if test -z "${out_file}"; then
out_file="${in_file}"
else
mkdir -p -- "${out_file}"
fi
recursive "${in_file}" "${out_file}"
exit $status
|