Skip to content

Make the new video API available through torchvision.io #2773

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

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
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
12 changes: 6 additions & 6 deletions test/test_video.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

import torch
import torchvision
from torchvision.io import _HAS_VIDEO_OPT
from torchvision.io import _HAS_VIDEO_OPT, Video

try:
import av
Expand Down Expand Up @@ -289,7 +289,7 @@ def test_read_video_tensor(self):
tv_result, _, _ = torchvision.io.read_video(full_path, pts_unit="sec")
tv_result = tv_result.permute(0, 3, 1, 2)
# pass 2: decode all frames using new api
reader = torch.classes.torchvision.Video(full_path, "video")
reader = Video(full_path, "video")
frames = []
t, _ = reader.next()
while t.numel() > 0:
Expand All @@ -310,7 +310,7 @@ def test_read_video_tensor(self):
# s = min(r)
# e = max(r)

# reader = torch.classes.torchvision.Video(full_path, "video")
# reader = Video(full_path, "video")
# results = _template_read_video(reader, s, e)
# tv_video, tv_audio, info = torchvision.io.read_video(
# full_path, start_pts=s, end_pts=e, pts_unit="sec"
Expand All @@ -329,7 +329,7 @@ def test_read_video_tensor(self):
# full_path, pts_unit="sec"
# )
# # pass 2: decode all frames using new api
# reader = torch.classes.torchvision.Video(full_path, "video")
# reader = Video(full_path, "video")
# pts = []
# t, p = reader.next()
# while t.numel() > 0:
Expand All @@ -353,7 +353,7 @@ def test_metadata(self):
torchvision.set_video_backend("pyav")
for test_video, config in test_videos.items():
full_path = os.path.join(VIDEO_DIR, test_video)
reader = torch.classes.torchvision.Video(full_path, "video")
reader = Video(full_path, "video")
reader_md = reader.get_metadata()
self.assertAlmostEqual(
config.video_fps, reader_md["video"]["fps"][0], delta=0.0001
Expand All @@ -372,7 +372,7 @@ def test_video_reading_fn(self):

ref_result = _decode_frames_by_av_module(full_path)

reader = torch.classes.torchvision.Video(full_path, "video")
reader = Video(full_path, "video")
newapi_result = _template_read_video(reader)

# First we check if the frames are approximately the same
Expand Down
23 changes: 16 additions & 7 deletions torchvision/io/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import torch
import warnings

from ._video_opt import (
Timebase,
VideoMetaData,
Expand All @@ -20,9 +23,14 @@
encode_jpeg,
write_jpeg,
encode_png,
write_png
write_png,
)

if _HAS_VIDEO_OPT:
Video = torch.classes.torchvision.Video
else:
Video = None


__all__ = [
"write_video",
Expand All @@ -39,10 +47,11 @@
"_read_video_meta_data",
"VideoMetaData",
"Timebase",
'read_image',
'decode_image',
'encode_jpeg',
'write_jpeg',
'encode_png',
'write_png',
"read_image",
"decode_image",
"encode_jpeg",
"write_jpeg",
"encode_png",
"write_png",
"Video",
]
4 changes: 3 additions & 1 deletion torchvision/io/_video_opt.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
torch.ops.load_library(ext_specs.origin)
_HAS_VIDEO_OPT = True
except (ImportError, OSError):
pass
warnings.warn("Could not import video_reader bakcend."
+ " Make sure ffmpeg is installed properly"
+ " and rebuild torchvision.")


default_timebase = Fraction(0, 1)
Expand Down