Skip to content

Commit e14a3bf

Browse files
committed
Upgrade moviepy to v2.2.1 and refactor for API changes
- Update `requirements.txt` to include `moviepy==2.2.1`, add explicit version constraints for `numpy`, `pytest`, `librosa`, and `soundfile`. - Replace deprecated methods (`resize`, `subclip`, `crop`, etc.) with updated alternatives (`resized`, `subclipped`, `cropped`, etc.) across `shorts.py`. - Adjust `iter_frames` to use the `logger` parameter instead of `progress_bar`. - Update imports to use the `moviepy` namespace directly instead of submodules to align with the new `moviepy` API changes. - Refactor tests in `test_shorts.py` to accommodate library updates.
1 parent 60be23a commit e14a3bf

File tree

3 files changed

+23
-32
lines changed

3 files changed

+23
-32
lines changed

requirements.txt

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
gtts==2.5.3
2-
moviepy==1.0.3
2+
librosa==0.11.0
3+
moviepy==2.2.1
4+
numpy==1.26.4
35
opencv-python==4.10.0.84
6+
pytest==8.4.2
47
python-dotenv==1.0.1
58
random2==1.0.1
69
scenedetect==0.6.2
710
scipy==1.11.4
8-
numpy>=1.21
9-
pytest>=7.0
10-
11-
librosa
12-
soundfile
11+
soundfile==0.13.1

shorts.py

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@
2222

2323
import numpy as np
2424
from dotenv import load_dotenv
25-
from moviepy.editor import CompositeVideoClip, VideoFileClip
26-
import moviepy.video.fx.crop as crop_vid
25+
from moviepy import CompositeVideoClip, VideoFileClip
2726
from scipy.ndimage import gaussian_filter
2827
from scenedetect import SceneManager, open_video
2928
from scenedetect.detectors import ContentDetector
@@ -209,12 +208,7 @@ def compute_video_action_profile(
209208

210209
prev_gray: np.ndarray | None = None
211210

212-
# Sequential frame reading is much cheaper than random access get_frame(t)
213-
# Compatibility with different MoviePy versions: 'progress_bar' arg may not exist.
214-
try:
215-
frame_iter = clip.iter_frames(fps=eff_fps, dtype="uint8", progress_bar=False)
216-
except TypeError:
217-
frame_iter = clip.iter_frames(fps=eff_fps, dtype="uint8")
211+
frame_iter = clip.iter_frames(fps=eff_fps, dtype="uint8", logger="bar")
218212

219213
for idx, frame in enumerate(frame_iter):
220214
t = idx / eff_fps
@@ -455,17 +449,15 @@ def crop_clip(
455449

456450
if current_ratio > target_ratio:
457451
new_width = round(height * ratio_w / ratio_h)
458-
return crop_vid.crop(
459-
clip,
452+
return clip.cropped(
460453
width=new_width,
461454
height=height,
462455
x_center=width * x_center,
463456
y_center=height * y_center,
464457
)
465458

466459
new_height = round(width / ratio_w * ratio_h)
467-
return crop_vid.crop(
468-
clip,
460+
return clip.cropped(
469461
width=width,
470462
height=new_height,
471463
x_center=width * x_center,
@@ -542,7 +534,7 @@ def get_final_clip(
542534
) -> VideoFileClip:
543535
"""Prepare a clip ready for rendering."""
544536

545-
result_clip = clip.subclip(start_point, start_point + final_clip_length)
537+
result_clip = clip.subclipped(start_point, start_point + final_clip_length)
546538

547539
width, height = result_clip.size
548540
target_ratio = config.target_ratio_w / config.target_ratio_h
@@ -557,22 +549,22 @@ def get_final_clip(
557549

558550
width, height = result_clip.size
559551
bg_w, bg_h = select_background_resolution(width)
560-
result_clip = result_clip.resize(width=bg_w)
552+
result_clip = result_clip.resized(width=bg_w)
561553

562554
if width >= height:
563-
background_clip = clip.subclip(start_point, start_point + final_clip_length)
555+
background_clip = clip.subclipped(start_point, start_point + final_clip_length)
564556
background_clip = crop_clip(background_clip, 1, 1, config.x_center, config.y_center)
565-
background_clip = background_clip.resize(width=720, height=720)
566-
background_clip = background_clip.fl_image(blur)
567-
background_clip = background_clip.resize(width=bg_w, height=bg_w)
568-
result_clip = CompositeVideoClip([background_clip, result_clip.set_position("center")])
557+
background_clip = background_clip.resized(width=720, height=720)
558+
background_clip = background_clip.image_transform(blur)
559+
background_clip = background_clip.resized(width=bg_w, height=bg_w)
560+
result_clip = CompositeVideoClip([background_clip, result_clip.with_position("center")])
569561
elif width / 9 < height / 16:
570-
background_clip = clip.subclip(start_point, start_point + final_clip_length)
562+
background_clip = clip.subclipped(start_point, start_point + final_clip_length)
571563
background_clip = crop_clip(background_clip, 9, 16, config.x_center, config.y_center)
572-
background_clip = background_clip.resize(width=720, height=1280)
573-
background_clip = background_clip.fl_image(blur)
574-
background_clip = background_clip.resize(width=bg_w, height=bg_h)
575-
result_clip = CompositeVideoClip([background_clip, result_clip.set_position("center")])
564+
background_clip = background_clip.resized(width=720, height=1280)
565+
background_clip = background_clip.image_transform(blur)
566+
background_clip = background_clip.resized(width=bg_w, height=bg_h)
567+
result_clip = CompositeVideoClip([background_clip, result_clip.with_position("center")])
576568

577569
return result_clip
578570

tests/test_shorts.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from pathlib import Path
44
from unittest.mock import MagicMock
55

6-
from moviepy.editor import ColorClip
6+
from moviepy import ColorClip
77

88
# Ensure the project root is on the import path.
99
import sys
@@ -263,7 +263,7 @@ class VideoStub:
263263
def __init__(self, *_args, **_kwargs):
264264
self.duration = 4.0
265265
self.fps = 30 # source fps
266-
def iter_frames(self, fps=2.0, dtype="uint8", progress_bar=False):
266+
def iter_frames(self, fps=2.0, dtype="uint8", logger=None):
267267
# Yield exactly int(duration*fps) frames
268268
n = int(self.duration * fps)
269269
for i in range(n):

0 commit comments

Comments
 (0)