Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stabilize Video - New functionality #580

Merged
merged 19 commits into from
May 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- 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]
- Added new `ffmpeg_stabilize_video()` function in `ffmpeg_tools`
- Optional `temp_audiofile_path` parameter in `VideoClip.write_videofile()` to specify where the temporary audiofile should be created [#1144]

### Changed <!-- for changes in existing functionality -->
Expand Down
37 changes: 37 additions & 0 deletions moviepy/video/io/ffmpeg_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,40 @@ def ffmpeg_resize(video, output, size):
]

subprocess_call(cmd)

tburrows13 marked this conversation as resolved.
Show resolved Hide resolved

@convert_path_to_string(("inputfile", "outputfile", "output_dir"))
def ffmpeg_stabilize_video(
inputfile, outputfile=None, output_dir="", overwrite_file=True
):
"""
Stabilizes ``filename`` and write the result to ``output``.

Parameters
-----------

inputfile
The name of the shaky video

tburrows13 marked this conversation as resolved.
Show resolved Hide resolved
outputfile
The name of new stabilized video
Optional: defaults to appending '_stabilized' to the input file name

output_dir
The directory to place the output video in
Optional: defaults to the current working directory

overwrite_file
If ``outputfile`` already exists in ``output_dir``, then overwrite ``outputfile``
Optional: defaults to True
"""
if not outputfile:
without_dir = os.path.basename(inputfile)
name, ext = os.path.splitext(without_dir)
outputfile = f"{name}_stabilized{ext}"

outputfile = os.path.join(output_dir, outputfile)
cmd = [FFMPEG_BINARY, "-i", inputfile, "-vf", "deshake", outputfile]
if overwrite_file:
cmd.append("-y")
subprocess_call(cmd)
9 changes: 9 additions & 0 deletions tests/test_ffmpeg_tools.py
Original file line number Diff line number Diff line change
@@ -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"))