Files
python_scripts/youtube_short.py
T

43 lines
1.3 KiB
Python

import sys
from moviepy.editor import VideoFileClip
def convert_to_short(input_path, output_path="youtube_short.mp4"):
# Load the video file
clip = VideoFileClip(input_path)
# 1. Check video duration
if clip.duration >= 60:
print(f"❌ Error: Video is {clip.duration:.2f}s long. It must be under 60 seconds.")
clip.close()
return False
print(f"✅ Video length check passed ({clip.duration:.2f}s). Processing...")
# 2. Calculate the target width for a 9:16 aspect ratio based on height
target_height = clip.h
target_width = int(target_height * (9 / 16))
# 3. Crop the center of the video
# x_center centers the crop horizontally
short_clip = clip.crop(width=target_width, height=target_height, x_center=clip.w / 2, y_center=clip.h / 2)
# Optional: Resize to standard YouTube Short resolution (1080x1920)
if short_clip.h != 1920:
short_clip = short_clip.resize(newsize=(1080, 1920))
# 4. Export the final video file
short_clip.write_videofile(
output_path,
codec="libx264",
audio_codec="aac",
fps=clip.fps
)
# Clean up files
clip.close()
short_clip.close()
print(f"🎉 Success! Short saved as: {output_path}")
return True
if __name__ == "__main__":
convert_to_short()