summaryrefslogtreecommitdiff
path: root/tmw-crunch-sfx
blob: 7f96d18e707aa9045135339be0ed451e9fa8e1fa (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/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