Skip to content

Video editing with Python

Add basic unit test

Tom Burrowsa55d2ccc2114
diff --git a/moviepy/video/io/ffmpeg_tools.py b/moviepy/video/io/ffmpeg_tools.py
index 9cba8cd9..e640782e 100644
--- a/moviepy/video/io/ffmpeg_tools.py
+++ b/moviepy/video/io/ffmpeg_tools.py
@@ -127,7 +127,7 @@ def ffmpeg_resize(video, output, size):
subprocess_call(cmd)
-def ffmpeg_stabilize_video(filename, output=None):
+def ffmpeg_stabilize_video(filename, output=None, output_dir="", overwrite_file=False):
"""
Stabilizes ``filename`` and write the result to ``output``.
@@ -140,11 +140,22 @@ def ffmpeg_stabilize_video(filename, output=None):
output
The name of new stabilized video
Optional: defaults to appending '_stabilized' to the input file name
- """
- name, ext = os.path.splitext(filename)
+ output_dir
+ The directory to place the output video in
+ Optional: defaults to the current working directory
+
+ overwrite_file
+ If ``output`` already exists in ``output_dir``, then overwrite ``output``
+ Optional: defaults to True
+ """
if not output:
- output = f"{name}_stabilized{ext}"
+ without_dir = os.path.basename(filename)
+ name, ext = os.path.splitext(without_dir)
+ output = f"{name}_stabilized{ext}"
+ output = os.path.join(output_dir, output)
cmd = [get_setting("FFMPEG_BINARY"), "-i", filename, "-vf", "deshake", output]
+ if overwrite_file:
+ cmd.append("-y")
subprocess_call(cmd)
diff --git a/tests/test_ffmpeg_tools.py b/tests/test_ffmpeg_tools.py
new file mode 100644
index 00000000..8baf6e7d
--- /dev/null
+++ b/tests/test_ffmpeg_tools.py
@@ -0,0 +1,9 @@
+import os
+
+from moviepy.video.io.ffmpeg_tools import ffmpeg_stabilize_video
+from tests.test_helper import TMP_DIR
+
+
+def test_stabilize_video():
+ ffmpeg_stabilize_video("media/fire2.mp4", output_dir=TMP_DIR, overwrite_file=True)
+ assert os.path.exists(os.path.join(TMP_DIR, "fire2_stabilized.mp4"))