From f40f09d371561ab6c205456d83b5421c652deaa3 Mon Sep 17 00:00:00 2001 From: Jordi Mas Date: Fri, 20 Dec 2024 09:34:42 +0100 Subject: [PATCH] Switch to moviepy 2.1 which fixes several video problems, like not keeping it veritcal --- e2e-tests/cmd_test.py | 2 +- open_dubbing/video_processing.py | 13 ++++++------- requirements.txt | 2 +- tests/video_processing_test.py | 18 ++++++++---------- 4 files changed, 16 insertions(+), 19 deletions(-) diff --git a/e2e-tests/cmd_test.py b/e2e-tests/cmd_test.py index dcbf84d..dd49d5d 100644 --- a/e2e-tests/cmd_test.py +++ b/e2e-tests/cmd_test.py @@ -85,7 +85,7 @@ def test_translations_with_tts(self, tts_engine): ), "Utterance end check failed" assert "- Bon dia. - Bé." == text_array[0], "translated text 0" - assert "El meu nom és Jordi Mas." == text_array[1], "translated text 1" + assert "Em dic Jordi Mas." == text_array[1], "translated text 1" assert "Sóc de Barcelona." == text_array[2], "translated text 2" assert ( "I m'encanta aquesta ciutat." == text_array[3] diff --git a/open_dubbing/video_processing.py b/open_dubbing/video_processing.py index 46fc69c..7eafbd4 100644 --- a/open_dubbing/video_processing.py +++ b/open_dubbing/video_processing.py @@ -17,7 +17,7 @@ from typing import Final -from moviepy.editor import AudioFileClip, VideoFileClip, concatenate_videoclips +from moviepy import AudioFileClip, VideoFileClip, concatenate_videoclips _DEFAULT_FPS: Final[int] = 30 _DEFAULT_DUBBED_VIDEO_FILE: Final[str] = "dubbed_video" @@ -36,13 +36,13 @@ def split_audio_video(*, video_file: str, output_directory: str) -> tuple[str, s warnings.filterwarnings("ignore", category=UserWarning) audio_clip = video_clip.audio audio_output_file = os.path.join(output_directory, filename + "_audio.mp3") - audio_clip.write_audiofile(audio_output_file, verbose=False, logger=None) - video_clip_without_audio = video_clip.set_audio(None) + audio_clip.write_audiofile(audio_output_file, logger=None) + video_clip_without_audio = video_clip.with_audio(None) fps = video_clip.fps or _DEFAULT_FPS video_output_file = os.path.join(output_directory, filename + "_video.mp4") video_clip_without_audio.write_videofile( - video_output_file, codec="libx264", fps=fps, verbose=False, logger=None + video_output_file, codec="libx264", fps=fps, logger=None ) return video_output_file, audio_output_file @@ -69,8 +69,8 @@ def combine_audio_video( ) audio = concatenate_videoclips([audio, silence]) elif duration_difference < 0: - audio = audio.subclip(0, video.duration) - final_clip = video.set_audio(audio) + audio = audio.subclipped(0, video.duration) + final_clip = video.with_audio(audio) target_language_suffix = "_" + target_language.replace("-", "_").lower() dubbed_video_file = os.path.join( output_directory, @@ -84,7 +84,6 @@ def combine_audio_video( audio_codec="aac", temp_audiofile="temp-audio.m4a", remove_temp=True, - verbose=False, logger=None, ) return dubbed_video_file diff --git a/requirements.txt b/requirements.txt index 7e1d57f..faeaa3f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,5 +1,5 @@ numpy >= 1.17.3 -moviepy == 1.0.3 +moviepy == 2.1.1 demucs == 4.0.1 psutil == 6.0.0 torch >= 2.0.0,< 2.5 diff --git a/tests/video_processing_test.py b/tests/video_processing_test.py index a508851..9e5e4ca 100644 --- a/tests/video_processing_test.py +++ b/tests/video_processing_test.py @@ -20,8 +20,8 @@ import numpy as np from moviepy.audio.AudioClip import AudioArrayClip -from moviepy.editor import ColorClip from moviepy.video.compositing.CompositeVideoClip import clips_array +from moviepy.video.VideoClip import ColorClip from open_dubbing.video_processing import VideoProcessing @@ -39,15 +39,15 @@ def _create_mock_video(self, directory: str, video_duration: int = 5) -> str: The full path to the saved video file. """ filename = os.path.join(directory, "mock_video.mp4") - red = ColorClip((256, 200), color=(255, 0, 0)).set_duration(video_duration) - green = ColorClip((256, 200), color=(0, 255, 0)).set_duration(video_duration) - blue = ColorClip((256, 200), color=(0, 0, 255)).set_duration(video_duration) + red = ColorClip((256, 200), color=(255, 0, 0), duration=video_duration) + green = ColorClip((256, 200), color=(0, 255, 0), duration=video_duration) + blue = ColorClip((256, 200), color=(0, 0, 255), duration=video_duration) combined_arrays = clips_array([[red, green, blue]]) combined_arrays.fps = 30 samples = int(44100 * video_duration) audio_data = np.zeros((samples, 2), dtype=np.int16) audio_clip = AudioArrayClip(audio_data, fps=44100) - final_clip = combined_arrays.set_audio(audio_clip) + final_clip = combined_arrays.with_audio(audio_clip) final_clip.write_videofile(filename, logger=None) return filename @@ -80,11 +80,9 @@ def test_combine_audio_video(self): video_duration = 5 directory = temporary_directory video_path = os.path.join(directory, "video.mp4") - red = ColorClip((256, 200), color=(255, 0, 0)).set_duration(video_duration) - green = ColorClip((256, 200), color=(0, 255, 0)).set_duration( - video_duration - ) - blue = ColorClip((256, 200), color=(0, 0, 255)).set_duration(video_duration) + red = ColorClip((256, 200), color=(255, 0, 0), duration=video_duration) + green = ColorClip((256, 200), color=(0, 255, 0), duration=video_duration) + blue = ColorClip((256, 200), color=(0, 0, 255), duration=video_duration) combined_arrays = clips_array([[red, green, blue]]) combined_arrays.fps = 30 combined_arrays.write_videofile(video_path)