updated on Tue Jan 10 04:01:21 UTC 2012
[aur-mirror.git] / lame-recursive / lamer.bash
bloba2b6150acc85fcd96f5276b96699f53b854e0a47
1 #!/bin/bash
2 #Copylefted by: Harvie 2o1o
3 version='1.1';
4 filetypes='(mp3|wav|wma)$'; #regex
5 quality="$1"; #passed to lame as --preset argument
6 outdir="_lame-encoded-preset-"; #preset name will be attached - needs to be regex compatible string
8 #guess count of CPU cores
9 cpus=1;
10 cpusguess=$(grep 'processor.:' /proc/cpuinfo 2>/dev/null | wc -l);
11 [ "$cpusguess" -ge 1 ] 2>/dev/null && cpus="$cpusguess"
12 [ "$2" -ge 1 ] 2>/dev/null && cpus="$2"
14 lamelog='/dev/null'
15 [ "$cpus" -eq 1 ] && lamelog='/dev/stdout'
17 [ -z "$1" ] && {
18 echo -e "lame-recursive v$version (Harvie 2o1o)
20 (Re-)Encode all '$filetypes' files in current directory and (sub-directories)
21 - This will NOT touch the original files.
22 - This will only create new files in ${outdir}PRESET directory in each
23 sub-directory, where PRESET will be substitued by selected lame preset.
24 - Files in such directories will be ignored
25 - Once encoded file will be overwriten only if it's older than original file
26 - Requires working lame binary
27 - Supports multiple CPUs/cores
29 Usage: $0 [preset] [cpu-cores]
30 Example: cd ~/music; $0 standart
31 Example: cd ~/spoken; $0 voice
33 presets:
34 VBR: voice, medium, standart, extreme, insane (= 320kbps CBR)
35 ABR: 8, 16, 32, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320
36 For more info: lame --preset help
38 cpu-cores:
39 You probably have $cpusguess CPU cores, i will use this value by default
40 Anyway... You can try any value higher than 0
41 (does not affect sound quality, probably you don't need to change this)
43 exit;
46 encode() {
47 tsign() { echo -ne "[$$]\t"; echo "$@"; }
48 temp=$(mktemp)
49 cleanup() {
50 last=$(cat "$temp");
51 echo -ne "\n[$$] Terminated. Deleting incomplete file:\n\t"
52 echo "$last"
53 rm -f "$last";
54 rm -rf "$temp";
55 exit;
57 trap cleanup SIGINT SIGTERM SIGHUP SIGPIPE SIGQUIT SIGKILL;
60 infile="$1"
61 cd "${infile%/*}";
62 outdir="${outdir}${quality}"
63 [[ -d "$outdir" ]] || {
64 echo "==> Creating directory: $(pwd)/$outdir";
65 mkdir -p "$outdir";
67 infile="${infile##*/}";
68 outfile="$outdir/${infile##*/}";
70 [ "$outfile" -nt "$infile" ] && {
71 tsign "Output file is newer than input file: $(pwd)/$outfile";
72 exit;
75 echo "$(pwd)/$outfile" > "$temp"
76 tsign "Encoding: '$infile'";
77 lame --preset "$quality" "$infile" "$outfile" >"$lamelog" 2>&1
78 tsign " Done: '$infile'; retval=$?";
81 echo "==> I will use $cpus CPU cores";
82 export -f encode; export quality outdir lamelog
83 find -regextype posix-egrep -iregex '.*'"$filetypes" | grep -v /"$outdir" | tr '\r\n' '\0\0' | xargs -0 -n 1 -P "$cpus" bash -c 'encode "$@"' --
84 echo '
86 ==> All files were processed.
87 ==> But you should rather check them before deleting the originals...
88 ==> Thank you for using lame-recursive by Harvie ( http://blog.harvie.cz/ )'