File size: 1,398 Bytes
2e8e758
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e489e2c
2e8e758
 
 
e489e2c
2e8e758
 
e489e2c
2e8e758
 
 
 
 
 
 
8010ada
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
#!/bin/bash

# Read arguments
video_file=$1
shift  # Remove first argument (video_file)
gpu_indices=($@)  # Remaining arguments are GPU indices
n_gpus=${#gpu_indices[@]}  # Derive n_gpus from the array length
n_frames=$(python -c "import sys; from vre import FFmpegVideo; print(len(FFmpegVideo(sys.argv[1])))" $video_file)
fps=$(python -c "import sys; from vre import FFmpegVideo; print(FFmpegVideo(sys.argv[1]).fps)" $video_file)
frames_per_gpu=$((n_frames / n_gpus))

# Function to clean up child processes on script termination
cleanup() {
    echo "Terminating subprocesses..."
    pkill -P $$  # Kill all child processes of this script
    exit 1
}

trap cleanup SIGINT SIGTERM

for ((i=0; i<n_gpus; i++)); do
    start_frame=$((i * frames_per_gpu))
    end_frame=$((start_frame + frames_per_gpu))
    
    # Ensure the last partition captures all remaining frames
    if [[ $i -eq $((n_gpus - 1)) ]]; then
        end_frame=$n_frames
    fi
    
    echo "CUDA_VISIBLE_DEVICES=${gpu_indices[$i]} ./wip.py $video_file --frames $start_frame..$end_frame &"
    CUDA_VISIBLE_DEVICES=${gpu_indices[$i]} ./wip.py $video_file --frames $start_frame..$end_frame &
done

# Wait for all background processes to complete
wait

# Combine output frames into a video using ffmpeg
ffmpeg -framerate $fps -i out_"$video_file"/collage/%d.jpg -c:v libx265 -pix_fmt yuv420p out_"$video_file"/collage/collage.mp4