Spaces:
Running
Running
Create vertical.py
Browse files- utils/vertical.py +25 -0
utils/vertical.py
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from moviepy.editor import VideoFileClip
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
def vertical_crop(video_path):
|
| 5 |
+
|
| 6 |
+
clip = VideoFileClip(video_path)
|
| 7 |
+
|
| 8 |
+
w, h = clip.size
|
| 9 |
+
target_ratio = 9 / 16
|
| 10 |
+
|
| 11 |
+
new_width = int(h * target_ratio)
|
| 12 |
+
|
| 13 |
+
x_center = w / 2
|
| 14 |
+
|
| 15 |
+
cropped = clip.crop(
|
| 16 |
+
x_center=x_center,
|
| 17 |
+
width=new_width,
|
| 18 |
+
height=h
|
| 19 |
+
)
|
| 20 |
+
|
| 21 |
+
output = video_path.replace(".mp4", "_vertical.mp4")
|
| 22 |
+
|
| 23 |
+
cropped.write_videofile(output, codec="libx264")
|
| 24 |
+
|
| 25 |
+
return output
|