File size: 4,143 Bytes
9e060c3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/bin/bash

INPUT_DIR="/mnt/jonathanDisk/ivritData"
OUTPUT_DIR="/mnt/jonathanDisk/ivritDataOpus"
CACHE_DIR="$HOME/ivritCache"

WORKERS=24

# 1. Capture exact paths to prevent 'Unknown encoder libopus' environment errors
FF_CMD=$(which ffmpeg)
PROBE_CMD=$(which ffprobe)

echo "======================================================"
echo "Starting Master Audio Pipeline"
echo "Workers: $WORKERS"
echo "FFmpeg path: $FF_CMD"
echo "======================================================"

# Ensure all base directories exist
mkdir -p "$OUTPUT_DIR"
mkdir -p "$CACHE_DIR"

echo ""
echo ">>> PHASE 1: Auditing and fixing existing 230GB using SSD Cache..."

find "$OUTPUT_DIR" -type f -name "*.opus" -print0 | xargs -0 -P "$WORKERS" -I {} bash -c '
    file="$1"
    ffmpeg_bin="$2"
    ffprobe_bin="$3"
    cache_base="$4"
    out_base="$5"
    
    # Check if the file is already mono (1 channel)
    channels=$("$ffprobe_bin" -v error -show_entries stream=channels -of default=noprint_wrappers=1:nokey=1 "$file" 2>/dev/null)
    
    # If it is NOT 1 channel, fix it using the SSD cache
    if [ "$channels" != "1" ]; then
        
        # Calculate paths for the cache
        relpath="${file#$out_base/}"
        cache_folder="$cache_base/$(dirname "$relpath")"
        mkdir -p "$cache_folder"
        
        filename=$(basename "$file")
        cache_source="$cache_folder/$filename"
        cache_target="$cache_folder/${filename%.*}.fixed.opus"
        
        # STEP 1: Copy broken Opus from HDD to SSD
        if cp "$file" "$cache_source"; then
            
            # STEP 2: Fix it (Read from SSD, Write to SSD)
            if "$ffmpeg_bin" -y -hide_banner -loglevel error -i "$cache_source" -af "aresample=resample_cutoff=0.98" -ar 16000 -ac 1 -c:a libopus "$cache_target"; then
                
                # STEP 3: Move the fixed file back to the HDD, overwriting the broken one
                mv "$cache_target" "$file"
            else
                echo "Error fixing: $file"
            fi
            
            # STEP 4: Clean up SSD
            rm -f "$cache_source"
        else
            echo "Error copying to SSD cache for fix: $file"
        fi
    fi
' _ {} "$FF_CMD" "$PROBE_CMD" "$CACHE_DIR" "$OUTPUT_DIR"

echo ">>> PHASE 1 COMPLETE."
echo ""
echo ">>> PHASE 2: Processing new unencoded files from Input Directory using SSD Cache..."

find "$INPUT_DIR" -type f \( -iname \*.wav -o -iname \*.mp3 -o -iname \*.flac -o -iname \*.m4a -o -iname \*.ogg \) -print0 | xargs -0 -P "$WORKERS" -I {} bash -c '
    infile="$1"
    in_dir="$2"
    out_dir="$3"
    cache_dir="$4"
    ffmpeg_bin="$5"
    
    # Calculate the relative path to maintain your exact folder hierarchy
    relpath="${infile#$in_dir/}"
    
    # Define target and cache subfolders
    target_folder="$out_dir/$(dirname "$relpath")"
    cache_folder="$cache_dir/$(dirname "$relpath")"
    
    filename=$(basename "$infile")
    outfile="$target_folder/${filename%.*}.opus"
    
    # >>> SKIP IF ALREADY EXISTS <<<
    if [ -f "$outfile" ]; then
        exit 0
    fi
    
    # Create the specific subfolders if they do not exist
    mkdir -p "$target_folder"
    mkdir -p "$cache_folder"
    
    cache_source="$cache_folder/$filename"
    cache_target="$cache_folder/${filename%.*}.opus"
    
    # STEP 1: Copy raw file from HDD to SSD Cache
    if cp "$infile" "$cache_source"; then

        # STEP 2: Encode (Read from SSD, Write to SSD)
        if "$ffmpeg_bin" -y -hide_banner -loglevel error -i "$cache_source" -af "aresample=resample_cutoff=0.98" -ar 16000 -ac 1 -c:a libopus "$cache_target"; then
            
            # STEP 3: Move finished file back to HDD
            mv "$cache_target" "$outfile"
        else
            echo "Error encoding: $infile"
        fi

        # STEP 4: Clean up SSD Cache
        rm -f "$cache_source"

    else
        echo "Error copying to SSD cache: $infile"
    fi
' _ {} "$INPUT_DIR" "$OUTPUT_DIR" "$CACHE_DIR" "$FF_CMD"

echo ">>> PHASE 2 COMPLETE."
echo "======================================================"
echo "Pipeline Finished! All data is strictly 16kHz and Mono."