Skip to content

Update video_mode.py #368

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

Merged
merged 3 commits into from
Nov 8, 2023
Merged
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
21 changes: 18 additions & 3 deletions src/video_mode.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,29 @@
def open_path_as_images(path, maybe_depthvideo=False):
"""Takes the filepath, returns (fps, frames). Every frame is a Pillow Image object"""
suffix = pathlib.Path(path).suffix
if suffix == '.gif':
if suffix.lower() == '.gif':
frames = []
img = Image.open(path)
for i in range(img.n_frames):
img.seek(i)
frames.append(img.convert('RGB'))
return 1000 / img.info['duration'], frames
if suffix in ['.avi'] and maybe_depthvideo:
if suffix.lower() == '.mts':
import imageio_ffmpeg
import av
container = av.open(path)
frames = []
for packet in container.demux(video=0):
for frame in packet.decode():
# Convert the frame to a NumPy array
numpy_frame = frame.to_ndarray(format='rgb24')
# Convert the NumPy array to a Pillow Image
image = Image.fromarray(numpy_frame)
frames.append(image)
fps = float(container.streams.video[0].average_rate)
container.close()
return fps, frames
if suffix.lower() in ['.avi'] and maybe_depthvideo:
try:
import imageio_ffmpeg
# Suppose there are in fact 16 bits per pixel
Expand All @@ -40,7 +55,7 @@ def open_path_as_images(path, maybe_depthvideo=False):
finally:
if 'gen' in locals():
gen.close()
if suffix in ['.webm', '.mp4', '.avi']:
if suffix.lower() in ['.webm', '.mp4', '.avi']:
from moviepy.video.io.VideoFileClip import VideoFileClip
clip = VideoFileClip(path)
frames = [Image.fromarray(x) for x in list(clip.iter_frames())]
Expand Down