File size: 3,180 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
#!/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 in Output Directory..."

find "$OUTPUT_DIR" -type f -name "*.opus" -print0 | xargs -0 -P "$WORKERS" -I {} bash -c '
    file="$1"
    ffmpeg_bin="$2"
    ffprobe_bin="$3"
    
    # 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 in-place
    if [ "$channels" != "1" ]; then
        if "$ffmpeg_bin" -y -hide_banner -loglevel error -i "$file" -af "aresample=resample_cutoff=0.98" -ar 16000 -ac 1 -c:a libopus "${file}.tmp.opus"; then
            mv "${file}.tmp.opus" "$file"
        else
            echo "Error fixing: $file"
        fi
    fi
' _ {} "$FF_CMD" "$PROBE_CMD"

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

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"
    
    # Copy to SSD Cache
    if cp "$infile" "$cache_source"; then

        # Encode reading from SSD and writing 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
            # Move back to HDD
            mv "$cache_target" "$outfile"
        else
            echo "Error encoding: $infile"
        fi

        # 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."