Skip to content

Commit 07af2ed

Browse files
simplify FFmpeg wrapper API
1 parent cded2a0 commit 07af2ed

File tree

2 files changed

+15
-32
lines changed

2 files changed

+15
-32
lines changed

surfer/utils.py

Lines changed: 13 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -648,11 +648,19 @@ def has_ffmpeg():
648648
ffmpeg_exists = rcode == 0
649649
return ffmpeg_exists
650650

651+
652+
def assert_ffmpeg_is_available():
653+
"Raise a RuntimeError if FFmpeg is not in the PATH"
654+
if not has_ffmpeg():
655+
err = ("FFmpeg is not in the path and is needed for saving "
656+
"movies. Install FFmpeg and try again. It can be "
657+
"downlaoded from http://ffmpeg.org/download.html.")
658+
raise RuntimeError(err)
659+
651660
requires_ffmpeg = np.testing.dec.skipif(not has_ffmpeg(), 'Requires FFmpeg')
652661

653662

654-
def ffmpeg(dst, frame_path, framerate=10, codec='mpeg4', opt="", inopt="",
655-
outopt=""):
663+
def ffmpeg(dst, frame_path, framerate=25, codec='mpeg4'):
656664
"""Run FFmpeg in a subprocess to convert an image sequence into a movie
657665
658666
Parameters
@@ -663,25 +671,17 @@ def ffmpeg(dst, frame_path, framerate=10, codec='mpeg4', opt="", inopt="",
663671
frame_path : str
664672
Path to the source frames (with a frame number field like '%04d').
665673
framerate : float
666-
Framerate of the movie (frames per second).
674+
Framerate of the movie (frames per second, default 25).
667675
codec : str
668676
Codec to use (default 'mpeg4').
669-
opt, inopt, outopt : str
670-
FFmpeg options, infile options and outfile options (e.g., "-o1 value
671-
-o2 value", see FFmpeg help for possible options).
672677
673678
Notes
674679
-----
675680
Requires FFmpeg to be in the path. FFmpeg can be downlaoded from `here
676681
<http://ffmpeg.org/download.html>`_. Stdout and stderr are written to the
677682
logger. If the movie file is not created, a RuntimeError is raised.
678683
"""
679-
# make sure FFmpeg is available
680-
if not has_ffmpeg():
681-
err = ("FFmpeg is not in the path and is needed for saving "
682-
"movies. Install FFmpeg and try again. It can be "
683-
"downlaoded from http://ffmpeg.org/download.html.")
684-
raise RuntimeError(err)
684+
assert_ffmpeg_is_available()
685685

686686
# find target path
687687
dst = os.path.expanduser(dst)
@@ -699,20 +699,7 @@ def ffmpeg(dst, frame_path, framerate=10, codec='mpeg4', opt="", inopt="",
699699
frame_dir, frame_fmt = os.path.split(frame_path)
700700

701701
# make the movie
702-
cmd = ['ffmpeg']
703-
if opt:
704-
cmd.extend(opt.split())
705-
if inopt:
706-
cmd.extend(inopt.split())
707-
cmd.extend(('-i', frame_fmt))
708-
if outopt:
709-
cmd.extend(outopt.split())
710-
if '-r ' not in outopt:
711-
cmd.extend(('-r', str(framerate)))
712-
if not ('-c' in cmd or '-codec' in cmd):
713-
cmd.extend(('-c', codec))
714-
cmd.append(dst)
715-
702+
cmd = ['ffmpeg', '-i', frame_fmt, '-r', str(framerate), '-c', codec, dst]
716703
logger.info("Running FFmpeg with command: %s", ' '.join(cmd))
717704
sp = subprocess.Popen(cmd, cwd=frame_dir, stdout=subprocess.PIPE,
718705
stderr=subprocess.PIPE)

surfer/viz.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
from . import utils, io
1919
from .config import config
2020
from .utils import (Surface, verbose, create_color_lut, _get_subjects_dir,
21-
string_types, has_ffmpeg, ffmpeg)
21+
string_types, assert_ffmpeg_is_available, ffmpeg)
2222

2323

2424
import logging
@@ -2129,11 +2129,7 @@ def save_movie(self, fname, time_dilation=4., tmin=None, tmax=None,
21292129
is free and can be obtained from `here
21302130
<http://ffmpeg.org/download.html>`_.
21312131
"""
2132-
if not has_ffmpeg():
2133-
err = ("FFmpeg is needed for saving movies and was not found in "
2134-
"the path. Install FFmpeg and try again. It can be "
2135-
"downlaoded from http://ffmpeg.org/download.html.")
2136-
raise RuntimeError(err)
2132+
assert_ffmpeg_is_available()
21372133

21382134
if tmin is None:
21392135
tmin = self._times[0]

0 commit comments

Comments
 (0)