From 850a40ed5e8c790793bfcd0018e1b40307177720 Mon Sep 17 00:00:00 2001 From: Tom Burrows Date: Tue, 28 Apr 2020 14:07:22 +0100 Subject: [PATCH] Add temp_audiofile_path parameter in write_videofile (#1144) --- CHANGELOG.md | 3 ++- moviepy/video/VideoClip.py | 19 ++++++++++++++----- tests/test_VideoClip.py | 15 ++++++++++++++- 3 files changed, 30 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7e789d123..b272ce29c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,10 +15,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - If you were previously setting custom locations for FFmpeg or ImageMagick in ``config_defaults.py`` and MoviePy still cannot autodetect the binaries, you will need to switch to the new method using enviroment variables. [#1109] ### Added -- Support for path-like objects as an option wherever filenames are passed in as arguments +- Support for path-like objects as an option wherever filenames are passed in as arguments [#1137] - Autodetect ImageMagick executable on Windows [#1109] - Optionally configure paths to FFmpeg and ImageMagick binaries with environment variables or a ``.env`` file [#1109] - Optional `encoding` parameter in `SubtitlesClip` [#1043] +- Optional `temp_audiofile_path` parameter in `VideoClip.write_videofile()` to specify where the temporary audiofile should be created [#1144] ### Changed diff --git a/moviepy/video/VideoClip.py b/moviepy/video/VideoClip.py index f9615b79b..ab9b6492c 100644 --- a/moviepy/video/VideoClip.py +++ b/moviepy/video/VideoClip.py @@ -23,6 +23,7 @@ outplace, requires_duration, use_clip_fps_by_default, + convert_path_to_string, ) from moviepy.tools import extensions_dict, find_extension, subprocess_call from moviepy.video.io.ffmpeg_writer import ffmpeg_write_video @@ -139,7 +140,7 @@ def save_frame(self, filename, t=0, withmask=True): @requires_duration @use_clip_fps_by_default @convert_masks_to_RGB - @convert_path_to_string("filename") + @convert_path_to_string(["filename", "temp_audiofile", "temp_audiofile_path"]) def write_videofile( self, filename, @@ -154,6 +155,7 @@ def write_videofile( audio_bitrate=None, audio_bufsize=2000, temp_audiofile=None, + temp_audiofile_path="", rewrite_audio=True, remove_temp=True, write_logfile=False, @@ -216,12 +218,16 @@ def write_videofile( If ``audio`` is the name of an audio file, this audio file will be incorporated as a soundtrack in the movie. - audiofps + audio_fps frame rate to use when generating the sound. temp_audiofile - the name of the temporary audiofile to be generated and - incorporated in the the movie, if any. + the name of the temporary audiofile, as a string or path-like object, to be created and + then used to write the complete video, if any. + + temp_audiofile_path + the location that the temporary audiofile is placed, as a + string or path-like object. Defaults to the current working directory. audio_codec Which audio codec should be used. Examples are 'libmp3lame' @@ -304,7 +310,10 @@ def write_videofile( audiofile = temp_audiofile elif make_audio: audio_ext = find_extension(audio_codec) - audiofile = name + Clip._TEMP_FILES_PREFIX + "wvf_snd.%s" % audio_ext + audiofile = os.path.join( + temp_audiofile_path, + name + Clip._TEMP_FILES_PREFIX + "wvf_snd.%s" % audio_ext, + ) # enough cpu for multiprocessing ? USELESS RIGHT NOW, WILL COME AGAIN # enough_cpu = (multiprocessing.cpu_count() > 1) diff --git a/tests/test_VideoClip.py b/tests/test_VideoClip.py index 21d681dcc..99ad4a6c3 100644 --- a/tests/test_VideoClip.py +++ b/tests/test_VideoClip.py @@ -37,6 +37,18 @@ def test_errors_with_redirected_logs(): close_all_clips(locals()) +def test_write_videofiles_with_temp_audiofile_path(): + clip = VideoFileClip("media/big_buck_bunny_432_433.webm").subclip(0.2, 0.5) + location = os.path.join(TMP_DIR, "temp_audiofile_path.webm") + temp_location = "temp_audiofile" + if not os.path.exists(temp_location): + os.mkdir(temp_location) + clip.write_videofile(location, temp_audiofile_path=temp_location, remove_temp=False) + assert os.path.isfile(location) + contents_of_temp_dir = os.listdir(temp_location) + assert any(file.startswith("temp_audiofile_path") for file in contents_of_temp_dir) + + def test_save_frame(): clip = VideoFileClip("media/big_buck_bunny_432_433.webm") location = os.path.join(TMP_DIR, "save_frame.png") @@ -163,4 +175,5 @@ def test_withoutaudio(): if __name__ == "__main__": - pytest.main() + # pytest.main() + test_write_videofiles_with_temp_audiofile_path()