| 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 | |
| 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 |
| 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) |
| 42 | " |
| 43 | exit; |
| 44 | } |
| 45 | |
| 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; |
| 56 | } |
| 57 | trap cleanup SIGINT SIGTERM SIGHUP SIGPIPE SIGQUIT SIGKILL; |
| 58 | |
| 59 | |
| 60 | infile="$1" |
| 61 | cd "${infile%/*}"; |
| 62 | outdir="${outdir}${quality}" |
| 63 | [[ -d "$outdir" ]] || { |
| 64 | echo "==> Creating directory: $(pwd)/$outdir"; |
| 65 | mkdir -p "$outdir"; |
| 66 | } |
| 67 | infile="${infile##*/}"; |
| 68 | outfile="$outdir/${infile##*/}"; |
| 69 | |
| 70 | [ "$outfile" -nt "$infile" ] && { |
| 71 | tsign "Output file is newer than input file: $(pwd)/$outfile"; |
| 72 | exit; |
| 73 | } |
| 74 | |
| 75 | echo "$(pwd)/$outfile" > "$temp" |
| 76 | tsign "Encoding: '$infile'"; |
| 77 | lame --preset "$quality" "$infile" "$outfile" >"$lamelog" 2>&1 |
| 78 | tsign " Done: '$infile'; retval=$?"; |
| 79 | } |
| 80 | |
| 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 ' |
| 85 | |
| 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/ )' |