Skip to content

Commit

Permalink
feat: builtin moviepy and ffmpeg
Browse files Browse the repository at this point in the history
for newer version opencv
see #178 #181
  • Loading branch information
williamfzc committed Aug 13, 2022
1 parent 7b7f9d5 commit 489d8e1
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 28 deletions.
10 changes: 7 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,23 @@
"Programming Language :: Python :: 3.6",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
],
python_requires=">=3.6",
install_requires=[
"opencv-python>=4.1.2.30,<4.5",
"opencv-contrib-python>=4.1.2.30,<4.5",
"opencv-python>=4.1.2.30",
"opencv-contrib-python>=4.1.2.30",
"moviepy>=1.0.3",
"imageio>=2.5.0",
"imageio-ffmpeg>=0.4.7",
"numpy>=0.18.0",
"loguru>=0.2.5",
"scikit-image>=0.16.0",
"scikit-learn>=0.21.0",
"pyecharts>=1.3.1",
"findit>=0.5.8",
"Jinja2>=2.10.1",
"MarkupSafe>=2.0.1",
"MarkupSafe==2.0.1",
"fire>=0.2.1",
"keras>=2.3.1",
"pydantic>=0.32.2",
Expand Down
3 changes: 0 additions & 3 deletions stagesepx/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,3 @@

# config
DEFAULT_THRESHOLD = 0.98

# ext
FFMPEG = r"ffmpeg"
8 changes: 5 additions & 3 deletions stagesepx/toolbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,11 @@ def video_jump(video_cap: cv2.VideoCapture, frame_id: int):
# another -1 for re-read
video_cap.set(cv2.CAP_PROP_POS_FRAMES, frame_id - 1 - 1)
video_cap.read()
logger.debug(
f"previous pointer: {get_current_frame_id(video_cap)}({get_current_frame_time(video_cap)})"
)

# notice: this timestamp may not correct because of resync by moviepy
# logger.debug(
# f"previous pointer: {get_current_frame_id(video_cap)}({get_current_frame_time(video_cap)})"
# )


def compare_ssim(pic1: np.ndarray, pic2: np.ndarray) -> float:
Expand Down
24 changes: 15 additions & 9 deletions stagesepx/video.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import os
import tempfile
import typing

import cv2
import imageio_ffmpeg
import numpy as np
from loguru import logger
import tempfile
import moviepy.editor as mpy

from stagesepx import toolbox
from stagesepx import constants

if typing.TYPE_CHECKING:
from stagesepx.hook import BaseHook
Expand Down Expand Up @@ -97,7 +99,9 @@ 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, constants.FFMPEG)
toolbox.fps_convert(
fps, self.path, video_path, imageio_ffmpeg.get_ffmpeg_exe()
)
self.path = video_path

with toolbox.video_capture(self.path) as cap:
Expand All @@ -118,11 +122,6 @@ def __str__(self):
__repr__ = __str__

def sync_timestamp(self):
try:
import moviepy.editor as mpy
except ImportError:
logger.error(f"import moviepy failed, can not sync timestamp")
return
vid = mpy.VideoFileClip(self.path)

# moviepy start from 0, 0.0
Expand All @@ -132,7 +131,10 @@ def sync_timestamp(self):
if frame_id >= len(self.data):
# ignore the rest
break
self.data[frame_id].timestamp = timestamp
frame_id_real = frame_id + 1
if not self.data[frame_id].timestamp:
logger.debug(f"fix frame {frame_id_real}'s timestamp: {timestamp}")
self.data[frame_id].timestamp = timestamp
logger.info("sync timestamp with moviepy finished")

def add_preload_hook(self, new_hook: "BaseHook"):
Expand Down Expand Up @@ -174,6 +176,10 @@ def load_frames(self, *args, **kwargs):
f"frames loaded. frame count: {self.frame_count}, size: {self.frame_size}, memory cost: {total_cost} bytes"
)

# sync timestamp for some newer versions opencv
# see: #178, #181
self.sync_timestamp()

def _read_from_file(self) -> typing.Generator[VideoFrame, None, None]:
with toolbox.video_capture(self.path) as cap:
success, frame = cap.read()
Expand Down
11 changes: 1 addition & 10 deletions test/test_video.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,6 @@ def test_convert_first():
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


def test_contain_image():
v = VideoObject(VIDEO_PATH)
v.load_frames()
Expand All @@ -73,6 +63,7 @@ def test_preload_with_hook():
v.add_preload_hook(hook)
v.load_frames()


def test_sync_timestamp():
v = VideoObject(VIDEO_PATH)
v.load_frames()
Expand Down

0 comments on commit 489d8e1

Please sign in to comment.