updated on Thu Jan 26 00:18:00 UTC 2012
[aur-mirror.git] / many2flac / many2flac
blob09817a3ff8a7415eebf384fd497cc3d8b8172c3d
1 #!/bin/bash
2 # ALAC/APE to FLAC converter script
3 # License: GPLv3
4 # Author: m0rph
5 # Email: m0rph.mailbox@gmail.com
8 check_program()
10 type -P "$1" &>/dev/null
11 if [ $? != 0 ]; then
12 echo "error: unable to find $1, please install required package and try again"
13 exit 1
18 print_header()
20 echo "many2flac converter version 1.14, (c) m0rph, 2011"
21 echo "email: m0rph.mailbox@gmail.com"
22 echo "supported input formats: ALAC, APE"
23 echo ""
27 print_help()
29 echo "usage: $(basename "${0}") [options] <input file(s)>"
30 echo "options:"
31 echo " -h, --help display this help and exit"
32 echo " -e, --erase erase input file(s) after success conversion"
33 echo " -f, --force force overwrite output file(s)"
34 echo " -n, --native-flac use native flac encoder instead of ffmpeg"
35 echo " -r, --replay-gain calculate ReplayGain and store in FLAC tags"
36 echo " -0, --fast use compression level 0"
37 echo " -1 use compression level 1"
38 echo " -2 use compression level 2"
39 echo " -3 use compression level 3"
40 echo " -4 use compression level 4"
41 echo " -5 use compression level 5"
42 echo " -6 use compression level 6"
43 echo " -7 use compression level 7"
44 echo " -8, --best use compression level 8"
45 echo ""
49 check_format()
51 format=$(ffmpeg -i "$1" 2>&1 | grep "Audio: " | cut -d ',' -f 1 | cut -d ' ' -f 8)
52 if [[ $format != "alac" && $format != "ape" ]]; then
53 return 1
58 process_file()
60 ffmpeg -y -i "$1" -aq $compression "$2" &> /dev/null
62 if [ $replay_gain_flag == 1 ]; then
63 flac --force --replay-gain "$2" &> /dev/null
68 process_file_native()
70 title=$(ffmpeg -i "$1" 2>&1 | grep -i " title :" | cut -d ':' -f 2 | sed 's/^ *//')
71 artist=$(ffmpeg -i "$1" 2>&1 | grep -i " artist :" | cut -d ':' -f 2 | sed 's/^ *//')
72 album=$(ffmpeg -i "$1" 2>&1 | grep -i " album :" | cut -d ':' -f 2 | sed 's/^ *//')
73 year=$(ffmpeg -i "$1" 2>&1 | grep -i " date :" | cut -d ':' -f 2 | sed 's/^ *//')
74 track=$(ffmpeg -i "$1" 2>&1 | grep -i " track :" | cut -d ':' -f 2 | sed 's/^ *//')
75 genre=$(ffmpeg -i "$1" 2>&1 | grep -i " genre :" | cut -d ':' -f 2 | sed 's/^ *//')
77 pipe=`mktemp -u -t many2flac.pipe.XXXXXX`
78 mkfifo "$pipe"
79 mplayer -ao pcm -ao pcm:file="$pipe" "$1" -noconsolecontrols &> /dev/null &
80 if [ $replay_gain_flag == 1 ]; then
81 flac --force "$pipe" --replay-gain -$compression \
82 -T title="$title" -T artist="$artist" -T album="$album" \
83 -T date="$year" -T tracknumber="$track" -T genre="$genre" \
84 -o "$2" &> /dev/null
85 else
86 flac --force "$pipe" -$compression \
87 -T title="$title" -T artist="$artist" -T album="$album" \
88 -T date="$year" -T tracknumber="$track" -T genre="$genre" \
89 -o "$2" &> /dev/null
94 print_header
96 compression=5
97 erase_flag=0
98 force_flag=0
99 native_flag=0
100 replay_gain_flag=0
101 files=()
103 while [[ $# > 0 ]]; do
104 case "$1" in
105 -h|--help)
106 print_help
107 exit 0
109 -e|--erase)
110 erase_flag=1
112 -f|--force)
113 force_flag=1
115 -n|--native-flac)
116 native_flag=1
118 -r|--replay-gain)
119 replay_gain_flag=1
121 -0|--fast)
122 compression=0
125 compression=1
128 compression=2
131 compression=3
134 compression=4
137 compression=5
140 compression=6
143 compression=7
145 -8|--best)
146 compression=8
148 *.ape|*.m4a)
149 if [ ! -f "$1" ]; then
150 echo "error: unable to find \"$1\" file"
151 echo ""
152 exit 1
154 files=("${files[@]}" "$1")
156 -?|--*)
157 echo "error: invalid option $1"
158 echo ""
159 exit 1
162 echo "error: invalid argument: \"$1\""
163 echo ""
164 exit 1
166 esac
168 shift
169 done
172 if [[ ${#files[@]} == 0 ]]; then
173 echo "error: no input files"
174 echo ""
175 exit 1
178 check_program ffmpeg
179 check_program flac
180 check_program mplayer
182 for input_file in "${files[@]}"; do
183 check_format "$input_file"
184 if [ $? != 0 ]; then
185 echo "$input_file: unsupported format"
186 continue
189 echo -n "converting $input_file"
190 output_file=${input_file%.*}.flac
191 if [[ -f "$output_file" && $force_flag == 0 ]]; then
192 echo " - failure (file exists)"
193 continue
196 if [ $native_flag == 1 ]; then
197 process_file_native "$input_file" "$output_file"
198 else
199 process_file "$input_file" "$output_file"
202 if [ $? == 0 ]; then
203 echo " - done"
204 if [ $erase_flag == 1 ]; then
205 rm "$input_file"
207 else
208 echo " - failure"
211 done