File size: 2,477 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
#!/bin/bash

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

WORKERS=24

FF_CMD=$(which ffmpeg)
PROBE_CMD=$(which ffprobe)

echo "======================================================"
echo "Starting Distributed Audio Pipeline (v6)"
echo "Workers: $WORKERS"
echo "FFmpeg path: $FF_CMD"
echo "======================================================"

mkdir -p "$OUTPUT_DIR"
mkdir -p "$CACHE_DIR"

# [Phase 1 code remains unchanged here. Assuming we are focusing on Phase 2 scaling]

echo ">>> PHASE 2: Distributed Processing of new files..."

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"
    
    relpath="${infile#$in_dir/}"
    target_folder="$out_dir/$(dirname "$relpath")"
    cache_folder="$cache_dir/$(dirname "$relpath")"
    
    filename=$(basename "$infile")
    outfile="$target_folder/${filename%.*}.opus"
    lock_dir="${outfile}.lock"
    
    # 1. IS IT DONE? (Check if output exists)
    if [ -f "$outfile" ]; then
        exit 0
    fi
    
    # Create the specific subfolders if they do not exist
    mkdir -p "$target_folder" 2>/dev/null
    mkdir -p "$cache_folder" 2>/dev/null
    
    # 2. IS IT PROCESSING? (Atomic Lock)
    # If mkdir fails, another worker/laptop already claimed this file. Skip it.
    if ! mkdir "$lock_dir" 2>/dev/null; then
        exit 0
    fi
    
    # If we made it here, this specific worker owns the file.
    
    cache_source="$cache_folder/$filename"
    cache_target="$cache_folder/${filename%.*}.opus"
    
    # Process the file
    if cp "$infile" "$cache_source"; then
        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 finished file 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
    
    # 3. RELEASE THE LOCK
    # Clean up the lock directory so we do not leave orphaned locks if something failed
    rm -rf "$lock_dir"

' _ {} "$INPUT_DIR" "$OUTPUT_DIR" "$CACHE_DIR" "$FF_CMD"

echo ">>> PHASE 2 COMPLETE."