python – How to Bulk Speed UP and Crop Videos FFMPEG

I have videos in (input) folder that is located on the root of the script, I also have output folder on the root, The input folder has several videos that i want to speed up in bulk by 1.1 percent, I also want to cropt the videos by 90 percent (Meaning 90 Percent of original video is visible).

I have a code that does not function well. Here is the code I have

 import os
import subprocess

# Define input and output directories
input_folder = "input"
output_folder = "output"

# Create the output directory if it doesn't exist
if not os.path.exists(output_folder):
    os.makedirs(output_folder)

# List all video files in the input directory
input_files = [f for f in os.listdir(input_folder) if f.endswith(('.mp4', '.avi', '.mkv'))]

# Speed up and crop each video
for input_file in input_files:
    input_path = os.path.join(input_folder, input_file)
    output_file = os.path.splitext(input_file)[0] + "_speed_crop.mp4"
    output_path = os.path.join(output_folder, output_file)

    # FFmpeg command to speed up video by 1.1x and crop to 90%
    ffmpeg_command = [
        "ffmpeg",
        "-i", input_path,
        "-vf", "setpts=1.1*PTS,crop=in_w*0.9:in_h*0.9",
        "-c:v", "libx264",
        "-crf", "20",
        "-c:a", "aac",
        "-strict", "experimental",
        output_path
    ]

    # Run FFmpeg command
    subprocess.run(ffmpeg_command)

print("Conversion complete.")

Read more here: Source link