summaryrefslogblamecommitdiff
path: root/tmw-crunch-sfx
blob: 7f96d18e707aa9045135339be0ed451e9fa8e1fa (plain) (tree)






































































































                                                                                                                                                                                                                                                                                                                  
#!/bin/bash


##Ledmitz (2022)
##For use with TMW Classic (AKA Legacy); Mass compression of SFX ogg files, recursively. Defaults to 64 kbps if already in mono and list stereo SFX
#and be prompted for each one after monos are compressed.

#Required apps check
REQUIRED_APPS='awk dirname ffprobe ffmpeg grep sed'
    for APP in $REQUIRED_APPS; do
        REQ_APP_CHECK=$(find '/bin' '/usr/bin' -name "$APP")
            if [[ "$REQ_APP_CHECK" == '' ]]; then
                echo -e "$APP must be installed in order for toob to work\nRequired Apps: $REQUIRED_APPS" >&2
                exit 0
            fi
    done
    
#Audio sample rate (Default: 44100)
SAMPRATE='44100'
#Audio bitrate in kbps (Default: 64)
BITRATE='64'
#Audio bitrate in kbps (Default: 160)
STEREO_BITRATE='160'
#Number of channels (Default: 1 (mono) - do not change. for possible future changes)
CHANNELS='1'

while :; do
    echo "Warning: Make backups before running this script. The changes to the original source files are permanent."
    echo ''
    grep '^TYPE_COMPRESSED' "$0"
    grep '^SAMPRATE' "$0"
    grep '^BITRATE' "$0"
    grep '^CHANNELS' "$0"
        read -r -p "This will convert all mono ogg vorbis audio over $BITRATE kbps in \"$PWD\", recursively, to a $SAMPRATE Hz / $BITRATE kbps / $CHANNELS channel ogg and prompt for action when stereo files are encountered. Are you sure you wish to continue?(\"yes\" to continue. Others answers quit)" ANS1
            if [[ "$ANS1" == 'yes' ]]; then
                break
            else
                exit 0
            fi
done

#Find oggs and convert them if criteria is met
#find . -type f -iname "*.ogg" |
#while IFS= read -r FILE; do
for FILE in $(find . -type f -iname "*.ogg"); do
    NAME=${FILE//.ogg/}
    SRC_BITRATE=$(ffprobe -hide_banner "$FILE" 2>&1 | grep 'Audio: vorbis' | grep -Eo '[0-9]+ kb/s' | sed 's/ kb\/s//g')
    SRC_SAMPRATE=$(ffprobe -hide_banner "$FILE" 2>&1 | grep 'Audio: vorbis' | grep -Eo '[0-9]+ Hz' | sed 's/ Hz//g')
    SRC_CHANNELS=$(ffprobe -hide_banner "$FILE" 2>&1 | grep 'Audio: vorbis' | awk '{print $7}' | sed 's/,$//')
            #Bitrate and samplerate check
            if [[ "$SRC_BITRATE" -gt "$BITRATE" ]] && [[ "$SRC_SAMPRATE" -ge "$SAMPRATE" ]]; then
                #Mono check
                if [[ "$SRC_CHANNELS" == 'mono' ]]; then
                    ffmpeg -hide_banner -loglevel 'error' -stats -nostdin -i "$FILE" -vn -ar "$SAMPRATE" -ac "$CHANNELS" -b:a "$BITRATE"'k' "$NAME"'_tmp.ogg'
                    mv "$NAME"'_tmp.ogg' "$NAME"'.ogg'
                elif [[ "$SRC_CHANNELS" == 'stereo' ]]; then
                    ffprobe -hide_banner "$FILE" 2>&1
                        while :; do
                            read -r -p "Stereo file: $NAME.ogg. Do you wish to proceed with conversion to mono?" STEREO_ANS1
                                case $STEREO_ANS1 in
                                    [Yy]* ) ffmpeg -hide_banner -loglevel 'error' -stats -nostdin -i "$FILE" -vn -ar "$SAMPRATE" -ac "$CHANNELS" -b:a "$BITRATE"'k' "$NAME"'_tmp.ogg'
                                            mv "$NAME"'_tmp.ogg' "$NAME"'.ogg'
                                            break
                                            ;;
                                    [Nn]* ) if [[ "$SRC_BITRATE" -gt "$STEREO_BITRATE" ]]; then
                                                ffmpeg -hide_banner -loglevel 'error' -stats -nostdin -i "$FILE" -vn -ar "$SAMPRATE" -ac '2' -b:a "$STEREO_BITRATE"'k' "$NAME"'_tmp.ogg'
                                                mv "$NAME"'_tmp.ogg' "$NAME"'.ogg'
                                            fi
                                            break
                                            ;;
                                        * ) echo "Invalid response: Y/y(anything) for yes or N/n(anything) for no."
                                            continue
                                            ;;
                                esac
                        done
                fi
            elif [[ "$SRC_BITRATE" -gt "$BITRATE" ]] && [[ "$SRC_SAMPRATE" -lt "$SAMPRATE" ]] && [[ "$SRC_CHANNELS" == 'mono' ]]; then
                #convert with src samplerate if lower than target samplerate
                ffmpeg -hide_banner -loglevel 'error' -stats -nostdin -i "$FILE" -vn -ar "$SRC_SAMPRATE" -ac "$CHANNELS" -b:a "$BITRATE"'k' "$NAME"'_tmp.ogg'
                mv "$NAME"'_tmp.ogg' "$NAME"'.ogg'
            elif [[ "$SRC_BITRATE" -gt "$BITRATE" ]] && [[ "$SRC_SAMPRATE" -lt "$SAMPRATE" ]] && [[ "$SRC_CHANNELS" == 'stereo' ]]; then
                ffprobe -hide_banner "$FILE" 2>&1
                    while :; do
                        read -r -p "Stereo File with low samplerate: $NAME.ogg. Do you wish to proceed with conversion to mono at $SRC_SAMPRATE Hz?" STEREO_ANS1
                            case $STEREO_ANS1 in
                                [Yy]* ) ffmpeg -hide_banner -loglevel 'error' -stats -nostdin -i "$FILE" -vn -ar "$SRC_SAMPRATE" -ac "$CHANNELS" -b:a "$BITRATE"'k' "$NAME"'_tmp.ogg'
                                        mv "$NAME"'_tmp.ogg' "$NAME"'.ogg'
                                        break
                                        ;;
                                [Nn]* ) if [[ "$SRC_BITRATE" -gt "$STEREO_BITRATE" ]]; then
                                            ffmpeg -hide_banner -loglevel 'error' -stats -nostdin -i "$FILE" -vn -ar "$SAMPRATE" -ac '2' -b:a "$STEREO_BITRATE"'k' "$NAME"'_tmp.ogg'
                                            mv "$NAME"'_tmp.ogg' "$NAME"'.ogg'
                                        fi
                                        break
                                        ;;
                                    * ) echo "Invalid response: Y/y(anything) for yes or N/n(anything) for no."
                                        continue
                                        ;;
                            esac
                    done
            fi
done