#!/bin/sh ## Place this in $PATH. ## As it is, make sure the wma file/s have the .wma extension. ## Errors may occur easly if there are super crazy named files within the target folder, ## I usually just rename these type of files, since it's 1 out of every 40 or 50. ## The folder path may have spaces, but crazy characters I'm not sure about. ## I've tried to keep the focus on *.wma files; it should be okay if other ## files/folders exist within the target folder. ## Requires mplayer and lame. $ENCODER can be defined below. ## When I created the option to remove the left behind wmas, I also created" ## many tripwires for saftey's sake. The jobs have to be flawless in order for them to be removed. ## It's a good study lesson for LPI ;) Ongoing functional project. ## Hint, if ya wanna do a tree of folders containing wmas, use something like the line below. ## for i in $(find . -name "*.wma" -exec dirname {} \; | uniq); do wma2mp3 -f "$i"; done usage_ () { echo "" echo "Usage:" echo "wma2mp3 option " echo "options:" echo "-v = verbose" echo "-h --help = for help (this)" echo "-s = single file encodeing" echo "-f = single folder encoding" echo "" echo "VERY SIMPLE (lots of room for error) script for encoding .wma files to mp3s, using mplayer and lame." echo "If single file method is used then the encoded file will be located in the same folder as wma2mp3 was" echo "called from. The left over .wma file will not be removed by wma2mp3; seemed too risky." echo "If single folder method is used then the finished files will be in a directory by the" echo "name of DONE, which will be located where the wmas where/are." echo "PLEASE READ THIS FILE FOR A FULL UNDERSTANDING BEFORE USING. DON'T RUN AS ROOT!!!!" echo "" exit 0 } ## Set lots of varibles here. ERRPERM1="\nYou must own the target folder/file, please return when this is fact, exiting.\n" ERRPERM2="\nThere seems to be a permission error/s with the file/s; must be owner and u=r, exiting.\n" ERRPERM3="\nThere seems to be a permission error with the folder you reside in or have selected; != u=rwx, exiting.\n" red='\e[0;31m' ## Define some colors. NC='\e[0m' ENCODER="lame" ## I believe notlame or ogg could work here. ## Option --quiet is added to processing line, add options there, not here. single_()## File wko. { ## Check if file has .wma extension. if [ "$(echo "${FOLDER}" | grep -o .wma)" = ".wma" ] then : else echo -e $red "\nThe chosen file MUST be a wma audio file [.wma], exiting.\n" $NC; exit 1 fi ## Check ownership of target file. if [ $(stat -c %u "$FOLDER") = $(id -u) ] then ## Check permissions of file. if [ "$($ls_ -l "$FOLDER" | cut -c 1,2)" = "-r" ] then if [ "$(stat -c %A . | cut -c 1,2,3,4)" = "drwx" ] # Check permissions of PWD. then : # Alright to continue. else echo -e $red $ERRPERM3 $NC; exit 1 fi else echo -e $red $ERRPERM2 $NC; exit 1 fi else echo -e $red $ERRPERM1 $NC; exit 1 fi ## Encode the wma to wav to mp3, rename appropriately. ## New mp3 will be in the directory from where this is called from. b=$(basename "$FOLDER" | sed -e 's/.wma//') echo -e $red "\nPROCESSING FILE: "$FOLDER" " $NC if [ "$VERB" = "yes" ] then mplayer "$FOLDER" -ao pcm && mv audiodump.wav "$b" # verbose spot. else mplayer "$FOLDER" -ao pcm &>/dev/null && mv audiodump.wav "$b" fi if [ -e "$b.mp3" ] # Does the file exist in PWD? then echo -e $red "\nERROR: "$b".mp3 already exists in PWD, skipping $ENCODER job." $NC else sleep 5 if [ "$VERB" = "yes" ] then ${ENCODER} "$b" # verbose spot. else ${ENCODER} --quiet "$b" fi fi rm -f "$b" ## Removes stripped of extension filename, comment to keep. echo -e $red "\nDONE PROCESSING; If success, then file should be in PWD.\n" unset b exit 0 } workem_ () ## Single folder workout. { ## Get ls path. ls_=$(which ls) ## Check for mplayer and $ENCODER if which $ENCODER &>/dev/null then : else echo -e $red "\nThe encoder does not exist, or needs to be in \$PATH, exiting.\n" $NC; exit 1 fi if which mplayer &>/dev/null then : else echo -e $red "\nMplayer does not exist, or needs to be in \$PATH, exiting.\n" $NC; exit 1 fi ## File? Folder? ?.wma files. if [ -f "$FOLDER" ] then single_ else if $ls_ "${FOLDER}"/*.wma &>/dev/null then cd "${FOLDER}" &>/dev/null else echo "" echo "${FOLDER}" echo -e $red "\nTarget folder/file may not exist, hold to many spaces or hold no .wma files, check this, exiting.\n" $NC exit 1 fi fi ## Check ownership of target folder. if [ $(stat -c %u .) = $(id -u) ] then ## Check permissions of folder. if [ "$($ls_ -dl . | cut -c 1,2,3,4)" = "drwx" ] then : ## Continue on. else echo -e $red $ERRPERM3 $NC; exit 1 fi else echo -e $red $ERRPERM1 $NC; exit 1 fi ## Ask user what to do with left over wma files. echo "PROCESSING FOLDER: ${FOLDER}" echo -e $red "\nDo we keep the left over wma files? (N|n) can be risky. [Y/n] Timeout in 20sec.\n" $NC read -t 20 WMAR case $WMAR in n|N) KEEP="no";; *) KEEP="yes";; esac ## Check permissions of files. We want to be owner and u=r. for i in *wma do if [ -r "$i" -a -O "$i" ] then : ## Continue on. else echo -e $red $ERRPERM2 $NC; exit 1 fi done ## Encode the wma to wav to mp3, rename appropriately. ## Move the new mp3s to a new folder called DONE. ## Make it easy to trip so we don't delete folders we shouldn't ## if error occurs. for i in *wma do a=`$ls_ "$i" | sed -e 's/.wma//' | tr -d "*"` echo -e $red "\nPROCESSING FILE: $i" $NC if [ "$VERB" = "yes" ] ## Verbose spot. then mplayer "$i" -ao pcm && mv audiodump.wav "$a" else mplayer "$i" -ao pcm &>/dev/null && mv audiodump.wav "$a" fi if [ -e "$a.mp3" ] # Does the file exist outside of DONE? then echo -e $red "\nERROR: $a.mp3 already exists, skipping $ENCODER job." $NC else sleep 5 if [ "$VERB" = "yes" ] ## Verbose spot. then ${ENCODER} "$a" else ${ENCODER} --quiet "$a" fi fi rm -f "$a" ## Removes stripped of extension filename, comment to keep. done mkdir DONE &>/dev/null ## Move the mp3s into folder DONE. for i in *mp3 do if test -e DONE/"$i" then echo -e $red "\nERROR: $i already exists in DONE, skipping the move to folder DONE." $NC OOPS="1" else mv "$i" DONE fi done ## Do something with the wma files. WMAS=$($ls_ -1 *.wma | wc -l) MP3S=$($ls_ -1 DONE/*.mp3 | wc -l) if [ $KEEP = "no" ] then if [ "$WMAS" = "$MP3S" ] then if [ "$OOPS" = "1" ] then echo -e $red "\nCouldn't remove left over wma files; Previous errors.\n" $NC else rm -f *.wma fi else echo -e $red "\nCouldn't remove left over wma files; The number of wmas to DONE/mp3s isn't equal.\n" $NC fi fi echo -e $red "\nDONE PROCESSING; If success, then files should be in folder DONE.\n" $NC unset OOPS unset a exit 0 } ## My new method of catching user input. NO_ARGS=0 if [ $# -eq "$NO_ARGS" ] then usage_ fi while getopts ":hf:s:vm:" Option do case $Option in f) FOLDER=$OPTARG workem_;; s) FOLDER=$OPTARG workem_;; v) VERB="yes";; h) usage_;; -help) usage_;; *) usage_;; -*) usage_;; esac done shift $(($OPTIND - 1)) exit 0