From 7a5c4192975dfcc5e0faf5764debd272c6173757 Mon Sep 17 00:00:00 2001 From: Grant Sanderson Date: Tue, 29 Jan 2019 23:52:56 -0800 Subject: [PATCH] Changed what the gain argument of Scene.play means to instead actually apply gain to the passed in sound, not the background --- manimlib/scene/scene.py | 4 ++-- manimlib/scene/scene_file_writer.py | 30 ++++++++++++++++++++--------- 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/manimlib/scene/scene.py b/manimlib/scene/scene.py index d7977a5a..1f7f9bc1 100644 --- a/manimlib/scene/scene.py +++ b/manimlib/scene/scene.py @@ -572,9 +572,9 @@ class Scene(Container): for frame in frames: self.file_writer.write_frame(frame) - def add_sound(self, sound_file, time_offset=0, gain=0): + def add_sound(self, sound_file, time_offset=0, gain=None, **kwargs): time = self.get_time() + time_offset - self.file_writer.add_sound(sound_file, time, gain) + self.file_writer.add_sound(sound_file, time, gain, **kwargs) def show_frame(self): self.update_frame(ignore_skipping=True) diff --git a/manimlib/scene/scene_file_writer.py b/manimlib/scene/scene_file_writer.py index 9ca5664f..cd098afc 100644 --- a/manimlib/scene/scene_file_writer.py +++ b/manimlib/scene/scene_file_writer.py @@ -119,7 +119,9 @@ class SceneFileWriter(object): def create_audio_segment(self): self.audio_segment = AudioSegment.silent() - def add_audio_segment(self, new_segment, time=None, gain=0): + def add_audio_segment(self, new_segment, + time=None, + gain_to_background=None): if not self.includes_sound: self.includes_sound = True self.create_audio_segment() @@ -140,13 +142,15 @@ class SceneFileWriter(object): self.audio_segment = segment.overlay( new_segment, position=int(1000 * time), - gain_during_overlay=gain, + gain_during_overlay=gain_to_background, ) - def add_sound(self, sound_file, time, gain=0): + def add_sound(self, sound_file, time=None, gain=None, **kwargs): file_path = get_full_sound_file_path(sound_file) new_segment = AudioSegment.from_file(file_path) - self.add_audio_segment(new_segment, time, gain) + if gain: + new_segment = new_segment.apply_gain(gain) + self.add_audio_segment(new_segment, time, **kwargs) # Writers def begin_animation(self, allow_write=False): @@ -307,17 +311,25 @@ class SceneFileWriter(object): ) # Makes sure sound file length will match video file self.add_audio_segment(AudioSegment.silent(0)) - self.audio_segment.export(sound_file_path) + self.audio_segment.export( + sound_file_path, + bitrate='312k', + ) temp_file_path = movie_file_path.replace(".", "_temp.") - commands = commands = [ + commands = [ "ffmpeg", "-i", movie_file_path, "-i", sound_file_path, '-y', # overwrite output file if it exists - "-c:v", "copy", "-c:a", "aac", + "-c:v", "copy", + "-c:a", "aac", + "-b:a", "320k", + # select video stream from first file + "-map", "0:v:0", + # select audio stream from second file + "-map", "1:a:0", '-loglevel', 'error', - "-shortest", - "-strict", "experimental", + # "-shortest", temp_file_path, ] subprocess.call(commands)