-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathm4atomp3.sh
More file actions
executable file
·104 lines (104 loc) · 2.77 KB
/
m4atomp3.sh
File metadata and controls
executable file
·104 lines (104 loc) · 2.77 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
#!/bin/bash
#
# $Id: aac2mp3,v 1.2 03/30/2008 10:00 Daniel Tavares (dantavares@gmail.com) -
# Based on Script - rali Exp $
#
#
# Convert one or more AAC/M4A files to MP3. Based on a script example
# I found at: http://gimpel.gi.funpic.de/Howtos/convert_aac/index.html
#
ME=`basename ${0}`
FFMPEG="/usr/local/bin/ffmpeg"
EXT="m4a"
BITRATE="192"
do_usage() { # explanatory text
echo "usage: ${ME} [-b nnn] [-e ext] [-f] [-c] [-r] [-v] [-h] [file list]"
echo " Convert music from AAC format to MP3"
echo " -m /path/app Specify the location of ffmpeg(1)"
echo " -b nnn bitrate for mp3 encoder to use"
echo " -e ext Use .ext rather than .m4a extension"
echo " -f Force overwrite of existing file"
echo " -c Delete original AAC|M4A file(s)"
echo " -v Verbose output"
echo " -h This information"
echo ""
echo "For recursive directory, use: find -name '*.${EXT}' -exec ${ME} "{}" [args] \;"
exit 0
}
do_error() {
echo "$*"
exit 1
}
file_overwrite_check() {
if [ "$FORCE" != "yes" ]
then
test -f "${1}" && do_error "${1} already exists."
else
test -f "${1}" && echo " ${1} is being overwritten."
fi
}
create_mp3() { # use ffmpeg(1) to convert from AAC to MP3
file_overwrite_check "${2}"
test $VERBOSE && echo -n "Converting file: ${1}"
avconv -i "${1}" "${2}";
if [ $? -ne 0 ]
then
echo ""
echo "Error!"
do_cleanup
do_error "Exiting"
fi
test $VERBOSE && echo ". OK"
}
do_cleanup() { # Delete intermediate and (optionally) original file(s)
test ${RMM4A} && rm -f "${1}"
test $VERBOSE && echo ". OK"
}
do_set_bitrate() {
test $VERBOSE && echo -n "Setting bitrate to: $1 kbps"
BITRATE=$1
test $VERBOSE && echo ". OK"
}
GETOPT=`getopt -o l:m:b:e:cfhrv -n ${ME} -- "$@"`
if [ $? -ne 0 ]
then
do_usage
fi
eval set -- "$GETOPT"
while true
do
case "$1" in
-m) FFMPEG=$2 ; shift ; shift ;;
-b) do_set_bitrate $2 ; shift ; shift ;;
-e) EXT=$2 ; shift ; shift ;;
-f) FORCE="yes" ; shift ;;
-c) RMM4A="yes" ; shift ;;
-v) VERBOSE="yes" ; shift ;;
-h) do_usage ;;
--) shift ; break ;;
*) do_usage ;;
esac
done
test -f $FFMPEG || do_error "$FFMPEG not found. Use \"-m\" switch."
if [ $# -eq 0 ]
then # Convert all files in current directory
for IFILE in *.${EXT}
do
if [ "${IFILE}" == "*.${EXT}" ]
then
do_error "Not found ${EXT} in this folder."
fi
OUT=`echo "${IFILE}" | sed -e "s/\.${EXT}//g"`
create_mp3 "${IFILE}" "${OUT}.mp3"
do_cleanup "${IFILE}"
done
else # Convert listed files
for IFILE in "$*"
do
test -f "${IFILE}" || do_error "${IFILE} not found."
OUT=`echo "${IFILE}" | sed -e "s/\.${EXT}//g"`
create_mp3 "${IFILE}" "${OUT}.mp3"
do_cleanup "${IFILE}"
done
fi
exit 0