Skip to content

Commit

Permalink
feat(video): use moviepy for re sync timestamp
Browse files Browse the repository at this point in the history
  • Loading branch information
williamfzc committed Aug 11, 2022
1 parent 97d31ee commit 7b7f9d5
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
18 changes: 18 additions & 0 deletions stagesepx/video.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,24 @@ 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
# but stagesepx is 1, 0.0
assert self.data, "load_frames() first"
for frame_id, (timestamp, _) in enumerate(vid.iter_frames(with_times=True)):
if frame_id >= len(self.data):
# ignore the rest
break
self.data[frame_id].timestamp = timestamp
logger.info("sync timestamp with moviepy finished")

def add_preload_hook(self, new_hook: "BaseHook"):
"""this hook only will be executed when preload"""
self._hook_list.append(new_hook)
Expand Down
5 changes: 5 additions & 0 deletions test/test_video.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,8 @@ def test_preload_with_hook():
hook = ExampleHook()
v.add_preload_hook(hook)
v.load_frames()

def test_sync_timestamp():
v = VideoObject(VIDEO_PATH)
v.load_frames()
v.sync_timestamp()

0 comments on commit 7b7f9d5

Please sign in to comment.