Commit | Line | Data |
---|---|---|
21c4e167 H |
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 | |
7 | ||
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" | |
13 | ||
14 | lamelog='/dev/null' | |
15 | [ "$cpus" -eq 1 ] && lamelog='/dev/stdout' | |
16 | ||
17 | [ -z "$1" ] && { | |
18 | echo -e "lame-recursive v$version (Harvie 2o1o) | |
19 | ||
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 | |
28 | ||
29 | Usage: $0 [preset] [cpu-cores] | |
30 | Example: cd ~/music; $0 standart | |
31 | Example: cd ~/spoken; $0 voice | |
32 | ||
9ee7b8d9 | 33 | presets: |
21c4e167 H |
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 | |
37 | ||
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) | |
9ee7b8d9 H |
42 | |
43 | tips & tricks: | |
44 | - to quickly visualy check if all files were recoded: | |
45 | ls -R1 . | sort | less | |
21c4e167 H |
46 | " |
47 | exit; | |
48 | } | |
49 | ||
50 | encode() { | |
51 | tsign() { echo -ne "[$$]\t"; echo "$@"; } | |
52 | temp=$(mktemp) | |
53 | cleanup() { | |
54 | last=$(cat "$temp"); | |
55 | echo -ne "\n[$$] Terminated. Deleting incomplete file:\n\t" | |
56 | echo "$last" | |
57 | rm -f "$last"; | |
58 | rm -rf "$temp"; | |
59 | exit; | |
60 | } | |
61 | trap cleanup SIGINT SIGTERM SIGHUP SIGPIPE SIGQUIT SIGKILL; | |
62 | ||
63 | ||
64 | infile="$1" | |
65 | cd "${infile%/*}"; | |
66 | outdir="${outdir}${quality}" | |
67 | [[ -d "$outdir" ]] || { | |
68 | echo "==> Creating directory: $(pwd)/$outdir"; | |
69 | mkdir -p "$outdir"; | |
70 | } | |
71 | infile="${infile##*/}"; | |
72 | outfile="$outdir/${infile##*/}"; | |
73 | ||
74 | [ "$outfile" -nt "$infile" ] && { | |
75 | tsign "Output file is newer than input file: $(pwd)/$outfile"; | |
76 | exit; | |
77 | } | |
78 | ||
79 | echo "$(pwd)/$outfile" > "$temp" | |
80 | tsign "Encoding: '$infile'"; | |
81 | lame --preset "$quality" "$infile" "$outfile" >"$lamelog" 2>&1 | |
82 | tsign " Done: '$infile'; retval=$?"; | |
83 | } | |
84 | ||
85 | echo "==> I will use $cpus CPU cores"; | |
86 | export -f encode; export quality outdir lamelog | |
87 | find -regextype posix-egrep -iregex '.*'"$filetypes" | grep -v /"$outdir" | tr '\r\n' '\0\0' | xargs -0 -n 1 -P "$cpus" bash -c 'encode "$@"' -- | |
88 | echo ' | |
89 | ||
90 | ==> All files were processed. | |
91 | ==> But you should rather check them before deleting the originals... | |
92 | ==> Thank you for using lame-recursive by Harvie ( http://blog.harvie.cz/ )' |