-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathload_video.py
55 lines (39 loc) · 1.24 KB
/
load_video.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import numpy as np
import av
def load_mp4(vid_path):
container = av.open(vid_path)
ims = [frame.to_image() for frame in container.decode(video=0)]
ims_c = np.array([np.array(im) for im in ims])
return ims_c
def load_mp4_ffmpeg(vid_path, grey=1, resolution=None):
import ffmpeg
probe = ffmpeg.probe(vid_path)
video_stream = next(
(stream for stream in probe['streams'] if stream['codec_type'] == 'video'), None)
width = int(video_stream['width'])
height = int(video_stream['height'])
out, _ = (
ffmpeg
.input(vid_path)
.output('pipe:', format='rawvideo', pix_fmt='rgb24')
.global_args('-loglevel', 'error')
.run(capture_stdout=True)
)
ims = (
np
.frombuffer(out, np.uint8)
.reshape([-1, height, width, 3])
)
if resolution is not None or grey:
from PIL import Image
ims = [Image.fromarray(im) for im in ims]
if resolution:
ims = [im.resize(resolution) for im in ims]
if grey:
ims = [im.convert('L') for im in ims]
ims_c = np.array([np.array(im) for im in ims])
else:
ims_c = ims
if grey:
ims_c = np.expand_dims(ims_c, axis=3)
return ims_c