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

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

WORKERS=24

echo "Initializing FFmpeg CPU flood with $WORKERS workers..."
echo "Input: $INPUT_DIR"
echo "Output: $OUTPUT_DIR"
echo "SSD Cache: $CACHE_DIR"

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

# 1. Find all audio files
# 2. Pipe into xargs with -P to run $WORKERS in parallel
# 3. Pass infile, input_dir, output_dir, and cache_dir cleanly into the subshell
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"
    
    # 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")"
    
    # Create the specific subfolders if they do not exist
    mkdir -p "$target_folder"
    mkdir -p "$cache_folder"
    
    # Set up filenames for the SSD cache and HDD destination
    filename=$(basename "$infile")
    cache_source="$cache_folder/$filename"
    cache_target="$cache_folder/${filename%.*}.opus"
    outfile="$target_folder/${filename%.*}.opus"
    
    # STEP 1: Copy the source file from the HDD to the SSD
    # We use an `if` statement so we only encode if the copy succeeds 
    # (prevents zero-byte errors if your SSD gets full)
    if cp "$infile" "$cache_source"; then
        
        # STEP 2: Run FFmpeg reading from the SSD and writing to the SSD
        if ffmpeg -y -hide_banner -loglevel error -i "$cache_source" -ar 16000 -c:a libopus "$cache_target"; then
            
            # STEP 3: Move the finished Opus file back to the HDD
            mv "$cache_target" "$outfile"
        else
            echo "Error encoding: $infile"
        fi
        
        # STEP 4: Delete the source copy from the SSD to free up space
        rm -f "$cache_source"
        
    else
        echo "Error copying to SSD cache: $infile"
    fi
' _ {} "$INPUT_DIR" "$OUTPUT_DIR" "$CACHE_DIR"

echo "Pipeline complete! All files converted and moved to HDD."