Skip to content

Commit

Permalink
Switch to moviepy 2.1 which fixes several video problems, like not ke…
Browse files Browse the repository at this point in the history
…eping it veritcal
  • Loading branch information
jordimas committed Dec 20, 2024
1 parent 6ceb30b commit f40f09d
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 19 deletions.
2 changes: 1 addition & 1 deletion e2e-tests/cmd_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
13 changes: 6 additions & 7 deletions open_dubbing/video_processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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

Expand All @@ -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,
Expand All @@ -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
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -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
Expand Down
18 changes: 8 additions & 10 deletions tests/video_processing_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit f40f09d

Please sign in to comment.