Skip to content

Commit

Permalink
feat(#74): support portable ffmpeg
Browse files Browse the repository at this point in the history
  • Loading branch information
williamfzc committed Nov 20, 2019
1 parent a80ef82 commit 2c0a5e7
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 4 deletions.
3 changes: 3 additions & 0 deletions stagesepx/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@
# report
BACKGROUND_COLOR = r"#fffaf4"
UNSTABLE_FLAG = r"-1"

# ext
FFMPEG = r"ffmpeg"
16 changes: 14 additions & 2 deletions stagesepx/toolbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,19 @@ def np2b64str(frame: np.ndarray) -> str:
return b64encode(buffer).decode()


def fps_convert(target_fps: int, source_path: str, target_path: str) -> int:
command: typing.List[str] = ["ffmpeg", "-i", source_path, "-r", str(target_fps), target_path]
def fps_convert(
target_fps: int, source_path: str, target_path: str, ffmpeg_exe: str = None
) -> int:
# for portable ffmpeg
if not ffmpeg_exe:
ffmpeg_exe = r"ffmpeg"
command: typing.List[str] = [
ffmpeg_exe,
"-i",
source_path,
"-r",
str(target_fps),
target_path,
]
logger.debug(f"convert video: {command}")
return subprocess.check_call(command)
3 changes: 2 additions & 1 deletion stagesepx/video.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import tempfile

from stagesepx import toolbox
from stagesepx import constants


class VideoFrame(object):
Expand Down Expand Up @@ -79,7 +80,7 @@ def __init__(
if fps:
video_path = os.path.join(tempfile.mkdtemp(), f"tmp_{fps}.mp4")
logger.debug(f"convert video, and bind path to {video_path}")
toolbox.fps_convert(fps, self.path, video_path)
toolbox.fps_convert(fps, self.path, video_path, constants.FFMPEG)
self.path = video_path

with toolbox.video_capture(path) as cap:
Expand Down
4 changes: 3 additions & 1 deletion test/test_toolbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,7 @@ def test_compress():

def test_convert_video():
target_fps: int = 30
ret = toolbox.fps_convert(target_fps, VIDEO_PATH, os.path.join(PROJECT_PATH, f"{target_fps}.mp4"))
ret = toolbox.fps_convert(
target_fps, VIDEO_PATH, os.path.join(PROJECT_PATH, f"{target_fps}.mp4")
)
assert not ret
10 changes: 10 additions & 0 deletions test/test_video.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,13 @@ def test_convert_first():
v = VideoObject(VIDEO_PATH, fps=30)
v.load_frames()
assert len(v.data) == 36


def test_custom_ffmpeg():
from stagesepx import constants

constants.FFMPEG = "unknown"
try:
VideoObject(VIDEO_PATH, fps=30)
except FileNotFoundError:
pass

0 comments on commit 2c0a5e7

Please sign in to comment.