-
Notifications
You must be signed in to change notification settings - Fork 3
/
tomp3
executable file
·80 lines (65 loc) · 1.7 KB
/
tomp3
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
#!/usr/bin/env bash
################################################################################
# tomp3 - converts audio files to VBR mp3s using ffmpeg.
#
# Dependencies: ffmpeg
#
# Version 1
# Matthew Malensek <[email protected]>
################################################################################
quality=0
active_threads=0
max_threads=$( \
(getconf _NPROCESSORS_ONLN || getconf NPROCESSORS_ONLN) 2> /dev/null)
print_usage() {
cat <<EOM
Usage: $(basename "${0}") [-t threads} [-V quality] files_to_convert
$(basename "${0}") converts audio files to VBR mp3s using ffmpeg. Options:
* -t threads Number of threads to use for conversion (default: ${max_threads})
* -V quality The LAME quality level to use (default: ${quality})
EOM
}
convert_audio() {
from="${1}"
ext="${from##*.}"
to="${from[*]/%${ext}/mp3}"
if [[ -e "${to}" ]]; then
echo "File already exists: ${to}"
return 1
fi
ffmpeg \
-i "${from}" \
-loglevel quiet \
-codec:a libmp3lame -qscale:a "${quality}" \
"${to}"
echo "${from} -> ${to}"
kill -s SIGUSR1 $$
}
thread_done() {
(( active_threads-- ))
}
trap thread_done SIGUSR1
if ! ( which ffmpeg &> /dev/null ); then
echo "Couldn't find ffmpeg!"
exit 1
fi
while getopts "t:V:" flag; do
case ${flag} in
t) max_threads=${OPTARG} ;;
V) quality=${OPTARG} ;;
?) print_usage; exit 1 ;;
esac
done
shift $(( OPTIND - 1 ))
if [[ ${#} -lt 1 ]]; then
print_usage
exit 1
fi
for file in "${@}"; do
convert_audio "${file}" &
(( active_threads++ ))
if [[ ${active_threads} -eq ${max_threads} ]]; then
wait
fi
done
until wait; do :; done