moviepy icon indicating copy to clipboard operation
moviepy copied to clipboard

How to modify this script that it can utilize GPU instead of CPU - moviepy > VideoFileClip

Open FurkanGozukara opened this issue 1 year ago • 1 comments

I have the following script to split video into chunks and get certain number of chunks. It works good but uses cpu instead of my RTX 3090 GPU

How can I make it use GPU in the final render? Thank you so much for answers

import math
import moviepy.editor as mp
import subprocess
import sys


# Define the input video file path and desired output duration
input_file = "install_second_part_2x.mp4"
output_duration = 158  # in seconds

# Load the input video file into a moviepy VideoFileClip object
video = mp.VideoFileClip(input_file)

# Calculate the number of 5-second chunks in the video
chunk_duration = 5
num_chunks = math.ceil(video.duration / chunk_duration)

# Create a list to hold the selected chunks
selected_chunks = []

# Calculate the number of chunks to select
output_num_chunks = math.ceil(output_duration / chunk_duration)

# Calculate the stride between adjacent chunks to select
stride = num_chunks // output_num_chunks

# Loop through the chunks and select every stride-th chunk
for i in range(num_chunks):
    if i % stride == 0:
        chunk = video.subclip(i*chunk_duration, (i+1)*chunk_duration)
        selected_chunks.append(chunk)
    progress = i / num_chunks * 100
    sys.stdout.write(f"\rProcessing: {progress:.2f}%")
    sys.stdout.flush()

# Keep adding chunks until the output duration is reached
output = selected_chunks[0]
for chunk in selected_chunks[1:]:
    if output.duration < output_duration:
        output = mp.concatenate_videoclips([output, chunk])
    else:
        break


# Write the output video to a file
output_file = "install_part_cut.mp4"
output.write_videofile(output_file)

FurkanGozukara avatar Apr 08 '23 02:04 FurkanGozukara

if you have a nvidia gpu, you can just run write_videofile(output_file, codec="h264_nvenc") to check your codecs, run ffmpeg -codecs first

COROYO avatar Oct 18 '23 13:10 COROYO