#!/bin/bash # -*-shell-script-*- # Converts MPEG files to WAV using mpg123. # Copyright (C) 2001 Ernest N. Mamikonyan # # This program is free software; you can redistribute it and/or # modify it under the terms of the GNU General Public License # as published by the Free Software Foundation; either version 2 # of the License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. # some defaults readonly mpg123=mpg321 # mpg123 clone to use force=false # don't overwrite files without prompting quiet=false # produce progress output usage() { echo "\ Usage: ${0##*/} [OPTIONS] [-] [FILE ...] Convert MPEG files to WAV using the ${mpg123##*/} MPEG decoder. -f never prompt before overwriting existent files -h display this help message and exit -n number the output files numerically, eg. 1.wav, 2.wav, ... -o name of the output file or directory -q suppress all diagnostic output and errors -V display version information and exit " >&2 exit $1 } rm() { local error=$(command rm -rf -- "$1" 2>&1) [[ -e "$1" ]] && { echo -e "Can't delete \`$1': $error"; exit 1; } } to_clobber() { [[ -e "$1" ]] || return 0 if $force; then echo "$1 exists - overwriting!"; rm "$1" else read -n 1 -p "$1 exists - overwrite? " < /dev/tty if [[ "$REPLY" != [Yy] ]]; then if [[ "$REPLY" ]]; then builtin echo -e \\bNo. >&2 else builtin echo -e No. >&2 fi return 1 fi builtin echo -e \\bYes! >&2 rm "$1" fi return 0 } convert() { local error to_clobber "$2" || return 1 echo -en "$1 ==> $2:\e[70G" if [[ "$1" == '' ]]; then if [[ "$2" == '' ]]; then "$mpg123" -qs - else error=$("$mpg123" -qw "$2" - 2>&1) fi else error=$("$mpg123" -qw "$2" -- "$1" 2>&1) fi if [[ $? != 0 || "$error" || ("$2" != '' && ! -f "$2") ]]; then echo -e ' \e[1;31mfailed\e[0;39m\e[K' [[ "$error" ]] && echo -e "$error" return 1 else echo -e ' \e[1;32mdone\e[0;39m\e[K'; return 0 fi } rename_n_convert() { local mpeg wav for mpeg; do if (($number)); then wav="$dir$((number++)).wav" else wav="${mpeg##*/}"; wav="$dir${wav%.*}.wav" fi convert "$mpeg" "$wav" done } while getopts :fhno:qV option; do case $option in f) force=true;; h) usage 0;; n) typeset -i number=1;; o) output="$OPTARG";; q) quiet=true;; V) echo $'\ mpg2wav version 1.0, Copyright \251 2001 Ernest N. Mamikonyan. mpg2wav comes with ABSOLUTELY NO WARRANTY. This is free software, and you are welcome to redistribute it under certain conditions; see the source for details. ' exit 0 ;; :) echo "The \`-$OPTARG' option requires an argument!" >&2; usage 1;; *) echo "\`-$OPTARG' is not a valid option!" >&2; usage 1;; esac done shift $((OPTIND-1)) $quiet && echo() { :; } || echo() { builtin echo "$@" >&2; } if (($# == 0)); then if [[ "$output" ]]; then convert '' "$output" else convert '' '' fi exit elif [[ "$output" ]]; then if [[ $# > 1 || -d "$1" || -d "$output" ]]; then dir="${output%/}/" if [[ ! -d "$dir" ]]; then to_clobber "$dir" || exit 1 error=$(mkdir -p -- "$dir" 2>&1) if [[ ! -d "$dir" ]]; then echo -e "Can't make directory \`$dir': $error"; exit 1 fi fi else convert "$1" "$output"; exit fi fi for arg; do if [[ "$arg" == - ]]; then convert '' "$dir" elif [[ -d "$arg" ]]; then OIFS="$IFS" IFS=: rename_n_convert $(find "$arg" -type f -print0 | tr \\0 :) IFS="$OIFS" else rename_n_convert "$arg" fi done