Skip to content

Commit 31fad34

Browse files
stephenyan1231fmassa
authored andcommitted
[video reader] inception commit (#1303)
* [video reader] inception commit * add method save_metadata to class VideoClips in video_utils.py * add load_metadata() method to VideoClips class * add Exception to not catch unexpected events such as memory erros, interrupt * fix bugs in video_plus.py * [video reader]remove logging. update setup.py * remove time measurement in test_video_reader.py * Remove glog and try making ffmpeg finding more robust * Add ffmpeg to conda build * Add ffmpeg to conda build [again] * Make library path finding more robust * Missing import * One more missing fix for import * Py2 compatibility and change package to av to avoid version conflict with ffmpeg * Fix for python2 * [video reader] support to decode one stream only (e.g. video/audio stream) * remove argument _precomputed_metadata_filepath * remove save_metadata method * add get_metadata method * expose _precomputed_metadata and frame_rate arguments in video dataset __init__ method * remove ssize_t * remove size_t to pass CI check on Windows * add PyInit__video_reader function to pass CI check on Windows * minor fix to define PyInit_video_reader symbol * Make c++ video reader optional * Temporarily revert changes to test_io * Revert changes to python files * Rename files to make it private * Fix python lint * Fix C++ lint * add a functor object EnumClassHash to make Enum class instances usable as key type of std::unordered_map * fix cpp format check
1 parent a6a926b commit 31fad34

36 files changed

+3646
-4
lines changed

packaging/torchvision/meta.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ requirements:
1212
host:
1313
- python
1414
- setuptools
15+
- av
1516
{{ environ.get('CONDA_PYTORCH_BUILD_CONSTRAINT') }}
1617
{{ environ.get('CONDA_CUDATOOLKIT_CONSTRAINT') }}
1718
{{ environ.get('CONDA_CPUONLY_FEATURE') }}
@@ -21,6 +22,7 @@ requirements:
2122
- pillow >=4.1.1
2223
- numpy >=1.11
2324
- six
25+
- av
2426
{{ environ.get('CONDA_PYTORCH_CONSTRAINT') }}
2527
{{ environ.get('CONDA_CUDATOOLKIT_CONSTRAINT') }}
2628

setup.py

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@
77
from pkg_resources import get_distribution, DistributionNotFound
88
import subprocess
99
import distutils.command.clean
10+
import distutils.spawn
1011
import glob
1112
import shutil
1213

1314
import torch
14-
from torch.utils.cpp_extension import CppExtension, CUDAExtension, CUDA_HOME
15+
from torch.utils.cpp_extension import BuildExtension, CppExtension, CUDAExtension, CUDA_HOME
1516

1617

1718
def read(*names, **kwargs):
@@ -124,6 +125,17 @@ def get_extensions():
124125
include_dirs = [extensions_dir]
125126
tests_include_dirs = [test_dir, models_dir]
126127

128+
ffmpeg_exe = distutils.spawn.find_executable('ffmpeg')
129+
has_ffmpeg = ffmpeg_exe is not None
130+
if has_ffmpeg:
131+
ffmpeg_bin = os.path.dirname(ffmpeg_exe)
132+
ffmpeg_root = os.path.dirname(ffmpeg_bin)
133+
ffmpeg_include_dir = os.path.join(ffmpeg_root, 'include')
134+
135+
# TorchVision video reader
136+
video_reader_src_dir = os.path.join(this_dir, 'torchvision', 'csrc', 'cpu', 'video_reader')
137+
video_reader_src = glob.glob(os.path.join(video_reader_src_dir, "*.cpp"))
138+
127139
ext_modules = [
128140
extension(
129141
'torchvision._C',
@@ -140,6 +152,27 @@ def get_extensions():
140152
extra_compile_args=extra_compile_args,
141153
),
142154
]
155+
if has_ffmpeg:
156+
ext_modules.append(
157+
CppExtension(
158+
'torchvision.video_reader',
159+
video_reader_src,
160+
include_dirs=[
161+
video_reader_src_dir,
162+
ffmpeg_include_dir,
163+
extensions_dir,
164+
],
165+
libraries=[
166+
'avcodec',
167+
'avformat',
168+
'avutil',
169+
'swresample',
170+
'swscale',
171+
],
172+
extra_compile_args=["-std=c++14"],
173+
extra_link_args=["-std=c++14"],
174+
)
175+
)
143176

144177
return ext_modules
145178

@@ -179,6 +212,8 @@ def run(self):
179212
"scipy": ["scipy"],
180213
},
181214
ext_modules=get_extensions(),
182-
cmdclass={'build_ext': torch.utils.cpp_extension.BuildExtension,
183-
'clean': clean}
215+
cmdclass={
216+
'build_ext': BuildExtension.with_options(no_python_abi_suffix=True),
217+
'clean': clean,
218+
}
184219
)

test/assets/videos/R6llTwEh07w.mp4

843 KB
Binary file not shown.
Binary file not shown.

test/assets/videos/README

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
Video meta-information Notation
2+
3+
Video File Name
4+
video: codec, fps
5+
audio: codec, bits per sample, sample rate
6+
7+
Test videos are listed below.
8+
--------------------------------
9+
10+
- RATRACE_wave_f_nm_np1_fr_goo_37.avi
11+
- source: hmdb51
12+
- video: DivX MPEG-4
13+
- fps: 30
14+
- audio: N/A
15+
16+
- SchoolRulesHowTheyHelpUs_wave_f_nm_np1_ba_med_0.avi
17+
- source: hmdb51
18+
- video: DivX MPEG-4
19+
- fps: 30
20+
- audio: N/A
21+
22+
- TrumanShow_wave_f_nm_np1_fr_med_26.avi
23+
- source: hmdb51
24+
- video: DivX MPEG-4
25+
- fps: 30
26+
- audio: N/A
27+
28+
- v_SoccerJuggling_g23_c01.avi
29+
- source: ucf101
30+
- video: Xvid MPEG-4
31+
- fps: 29.97
32+
- audio: N/A
33+
34+
- v_SoccerJuggling_g24_c01.avi
35+
- source: ucf101
36+
- video: Xvid MPEG-4
37+
- fps: 29.97
38+
- audio: N/A
39+
40+
- R6llTwEh07w.mp4
41+
- source: kinetics-400
42+
- video: H-264 - MPEG-4 AVC (part 10) (avc1)
43+
- fps: 30
44+
- audio: MPEG AAC audio (mp4a)
45+
- sample rate: 44.1K Hz
46+
47+
- SOX5yA1l24A.mp4
48+
- source: kinetics-400
49+
- video: H-264 - MPEG-4 AVC (part 10) (avc1)
50+
- fps: 29.97
51+
- audio: MPEG AAC audio (mp4a)
52+
- sample rate: 48K Hz
53+
54+
- WUzgd7C1pWA.mp4
55+
- source: kinetics-400
56+
- video: H-264 - MPEG-4 AVC (part 10) (avc1)
57+
- fps: 29.97
58+
- audio: MPEG AAC audio (mp4a)
59+
- sample rate: 48K Hz

test/assets/videos/SOX5yA1l24A.mp4

547 KB
Binary file not shown.
Binary file not shown.
Binary file not shown.

test/assets/videos/WUzgd7C1pWA.mp4

869 KB
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)