File size: 925 Bytes
3ccde9c |
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 |
import os
from moviepy.editor import VideoFileClip
# Define the folder and duration
input_folder = "examples"
output_folder = "examples/trimmed"
trim_duration = 3 # seconds
# Create output folder if it doesn't exist
os.makedirs(output_folder, exist_ok=True)
# Process each .mp4 file
for filename in os.listdir(input_folder):
if filename.lower().endswith(".mp4"):
input_path = os.path.join(input_folder, filename)
output_path = os.path.join(output_folder, filename)
print(f"Trimming: {input_path} -> {output_path}")
try:
clip = VideoFileClip(input_path).subclip(0, trim_duration)
clip.write_videofile(
output_path,
codec="libx264",
audio_codec="aac",
verbose=False,
logger=None,
)
except Exception as e:
print(f"Failed to process {filename}: {e}")
|