#!/bin/tcsh -f #H# This program transcodes various sound formats into the 3 formats that I use #H# for different purposes. It can make mp3 or ogg files at 3 different quality #H# levels each and it can make flac files. #H# The script also does peak normilization on files before encoding them. #H# #H# Syntax (order of params matters): #H# % tcode [-k] #H# is the format to encode to. Choices are: flac, mp3, ogg, m4a, mpc, or spx #H# is the encoding quality. Choices are: high, medium, or low #H# is the existing audio file to be transcoded #H# if "all" is used in place of a filename all files in the current directory #H# with the following extensions will be transcoded: #H# wav, dts, ac3, mpa, mp2, mpc, aac #H# This option probably shouldn't be used with -k #H# -k is an optional parameter to tell the script to keep both the original #H# file and the unencoded .wav file #H# #H# example: #H# % tcode ogg medium file.ac3 #H# This will turn file.ac3 into file.ogg at medium quality with no leftovers. #H# #H# Note that transcoding flac>flac will simply normalize and ensure max compression. #H# However, the script will not allow an ogg>ogg transcode. #H# # print usage info if needed if ($1 == "-h" || $1 == "--help" || $1 == "help" || $1 == "") exec sed -n -e '/^\#H\#/s/^....//p' $0 # set defaults and failsafes set NewFormat="error" set Quality="error" set Keep="N" # interpret cli params set NewFormatParam=$1 set QualityParam=$2 set OldFileName="$3" if ("$4" == "-k") set Keep="Y" if ("$OldFileName" == "all") then foreach File (*.wav *.dts *.ac3 *.mpa *.mp2 *.mpc) time $0 $1 $2 "$File" $4 $5 end exit endif # determine which encoder to use and what cli params based to the tcode params if ($NewFormatParam == "f" || $NewFormatParam == "flac") then set NewFormat="flac" set Quality="high" set EncoderParams="--replay-gain -8" endif if ($NewFormatParam == "m" || $NewFormatParam == "mp3") then set NewFormat="mp3" if ($QualityParam == "h" || $QualityParam == "high") then set Quality="high" set EncoderParams="--preset standard -V1 -q 1 -b 32 -B 320" endif if ($QualityParam == "m" || $QualityParam == "medium" || $QualityParam == "tv") then set Quality="medium" set EncoderParams="--preset medium --ns-sfb21 4 -q 1 -b 32 -B 320" endif if ($QualityParam == "l" || $QualityParam == "low" || $QualityParam == "lq") then set Quality="low" set EncoderParams="--preset medium --ns-sfb21 4 -Y -q 1 -b 32 -B 192" endif endif if ($NewFormatParam == "m4a") then set NewFormat="m4a" if ($QualityParam == "h" || $QualityParam == "high") then set Quality="high" set EncoderParams="-q 130" endif if ($QualityParam == "m" || $QualityParam == "medium" || $QualityParam == "tv") then set Quality="medium" set EncoderParams="-q 100" endif if ($QualityParam == "l" || $QualityParam == "low" || $QualityParam == "lq") then set Quality="low" set EncoderParams="-q 80" endif endif if ($NewFormatParam == "aac") then set NewFormat="aac" if ($QualityParam == "h" || $QualityParam == "high") then set Quality="high" set EncoderParams="-q 130" endif if ($QualityParam == "m" || $QualityParam == "medium" || $QualityParam == "tv") then set Quality="medium" set EncoderParams="-q 100" endif if ($QualityParam == "l" || $QualityParam == "low" || $QualityParam == "lq") then set Quality="low" set EncoderParams="-q 80" endif endif if ($NewFormatParam == "spx") then set NewFormat="spx" if ($QualityParam == "h" || $QualityParam == "high") then set Quality="high" set EncoderParams="--vbr --quality 8" endif if ($QualityParam == "m" || $QualityParam == "medium" || $QualityParam == "tv") then set Quality="medium" set EncoderParams="--vbr --quality 5" endif if ($QualityParam == "l" || $QualityParam == "low" || $QualityParam == "lq") then set Quality="low" set EncoderParams="--vbr --quality 3" endif endif if ($NewFormatParam == "mpc") then set NewFormat="mpc" if ($QualityParam == "h" || $QualityParam == "high") then set Quality="high" set EncoderParams="--standard" endif if ($QualityParam == "m" || $QualityParam == "medium" || $QualityParam == "tv") then set Quality="medium" set EncoderParams="--radio" endif if ($QualityParam == "l" || $QualityParam == "low" || $QualityParam == "lq") then set Quality="low" set EncoderParams="--thumb" endif endif if ($NewFormatParam == "o" || $NewFormatParam == "ogg") then set NewFormat="ogg" if ($QualityParam == "h" || $QualityParam == "high") then set Quality="high" # Note that -q 5 might be enough here but I think -q 6 is better. # I pretty much only use this setting on music videos so I want high quality audio. set EncoderParams="-q 6" endif if ($QualityParam == "m" || $QualityParam == "medium" || $QualityParam == "tv") then set Quality="medium" set EncoderParams="-q 4" endif if ($QualityParam == "l" || $QualityParam == "low" || $QualityParam == "lq") then set Quality="low" set EncoderParams="-q 3" endif endif if ($NewFormatParam == "wav") then set NewFormat="wav" set Quality="high" set EncoderParams="" endif # make sure the CLI params were good if ($NewFormat == "error" || $Quality == "error") exec $0 --help # figure out what format we are starting with set OldFormat=`echo "$OldFileName" | awk -F. '{print $NF}'` # Grab the file name without the extention so we can change it later. set FN=`basename "$OldFileName" .${OldFormat}` # Decode compressed audio formats into .wav files so they can be re-encoded. # Delete the original after the decode is done. set Decoded="N" switch ($OldFormat) case "wav": # don't have to decode .wav files set Decoded="Y" breaksw case "ac3": # use a52dec to decode ac3 files to wav files a52dec -o wav "${FN}.ac3" > "${FN}.wav" && set Decoded="Y" breaksw case "dts": # use dtsdec from libdts to decode dts files to wav files dtsdec -o wav "${FN}.dts" > "${FN}.wav" && set Decoded="Y" breaksw case "mpa": # use madplay to decode mpeg audio to wav # mpg123 or mpg321 can also be used but madplay is better madplay "${FN}.mpa" -o "wave:${FN}.wav" && set Decoded="Y" breaksw case "mpc": # use mppdec from musepack-tools to decode mpc files to wav mppdec "${FN}.mpc" "${FN}.wav" && set Decoded="Y" breaksw case "ogg": if ($NewFormat == "ogg") exec echo "ERROR: File is already an ogg." # use oggdec from vorbis-tools to decode ogg to wav oggdec "${FN}.ogg" && set Decoded="Y" breaksw case "mp2": # use madplay to decode mpeg audio to wav # mpg123 or mpg321 can also be used but madplay is better madplay "${FN}.mp2" -o "wave:${FN}.wav" && set Decoded="Y" breaksw case "flac": # use flac to decode flac files to wav flac -d "${FN}.flac" && set Decoded="Y" breaksw case "aac": # use faad to decode AAC files to wav... faad -d -w "${FN}.aac" > "${FN}.wav" && set Decoded="Y" breaksw case "mpa": # use faad to decode MPA files to wav... faad -d -w "${FN}.mpa" > "${FN}.wav" && set Decoded="Y" breaksw case "m4a": # use faad to decode M4A files to wav... faad -d -w "${FN}.m4a" > "${FN}.wav" && set Decoded="Y" breaksw case "spx": # use speexdec to decode speex files to wav... speexdec -d -w "${FN}.spx" -o "${FN}.wav" && set Decoded="Y" breaksw endsw # make sure that the file was actually decoded and blow up if not. if ($Decoded == "N") exec echo "ERROR: File was not decoded." # delete the original file unless it was specificly requested that it be kept. if ($Keep == "N") if ($OldFormat != "wav") rm -v "${FN}.$OldFormat" # Use normalize for scaling instead of sox. It modifies the .wav file directly # which means I can use it to make other formats like flac. normalize --peak "${FN}.wav" || exec echo "Something went wrong in the normalizing." # Actually encode the wav file into an mp3 file and delete it when done. if ($NewFormat == "flac") flac $EncoderParams -o "${FN}.flac" "${FN}.wav" || exec echo "Problem encoding." if ($NewFormat == "mp3") lame $EncoderParams "${FN}.wav" "${FN}.mp3" || exec echo "Problem encoding." if ($NewFormat == "ogg") oggenc $EncoderParams -o "${FN}.ogg" "${FN}.wav" || exec echo "Problem encoding." if ($NewFormat == "m4a") faac $EncoderParams -o "${FN}.m4a" "${FN}.wav" || exec echo "Problem encoding." if ($NewFormat == "aac") faac $EncoderParams -o "${FN}.aac" "${FN}.wav" || exec echo "Problem encoding." if ($NewFormat == "mpc") mppenc $EncoderParams "${FN}.wav" "${FN}.mpc" || exec echo "Problem encoding." if ($NewFormat == "spx") speexenc $EncoderParams "${FN}.wav" "${FN}.spx" || exec echo "Problem encoding." if ($NewFormat == "wav") exec echo "No need to encode a wav file." # delete the .wav file unless it was specificly requested that it be kept. if ($Keep == "N") rm -v "${FN}.wav" # make all my mp3 files have the same ownership and permissions chown kmk:users "${FN}.$NewFormat" >& /dev/null chmod 644 "${FN}.$NewFormat" >& /dev/null