13
13
def open_path_as_images (path , maybe_depthvideo = False ):
14
14
"""Takes the filepath, returns (fps, frames). Every frame is a Pillow Image object"""
15
15
suffix = pathlib .Path (path ).suffix
16
- if suffix == '.gif' :
16
+ if suffix . lower () == '.gif' :
17
17
frames = []
18
18
img = Image .open (path )
19
19
for i in range (img .n_frames ):
20
20
img .seek (i )
21
21
frames .append (img .convert ('RGB' ))
22
22
return 1000 / img .info ['duration' ], frames
23
- if suffix in ['.avi' ] and maybe_depthvideo :
23
+ if suffix .lower () == '.mts' :
24
+ import imageio_ffmpeg
25
+ import av
26
+ container = av .open (path )
27
+ frames = []
28
+ for packet in container .demux (video = 0 ):
29
+ for frame in packet .decode ():
30
+ # Convert the frame to a NumPy array
31
+ numpy_frame = frame .to_ndarray (format = 'rgb24' )
32
+ # Convert the NumPy array to a Pillow Image
33
+ image = Image .fromarray (numpy_frame )
34
+ frames .append (image )
35
+ fps = float (container .streams .video [0 ].average_rate )
36
+ container .close ()
37
+ return fps , frames
38
+ if suffix .lower () in ['.avi' ] and maybe_depthvideo :
24
39
try :
25
40
import imageio_ffmpeg
26
41
# Suppose there are in fact 16 bits per pixel
@@ -40,7 +55,7 @@ def open_path_as_images(path, maybe_depthvideo=False):
40
55
finally :
41
56
if 'gen' in locals ():
42
57
gen .close ()
43
- if suffix in ['.webm' , '.mp4' , '.avi' ]:
58
+ if suffix . lower () in ['.webm' , '.mp4' , '.avi' ]:
44
59
from moviepy .video .io .VideoFileClip import VideoFileClip
45
60
clip = VideoFileClip (path )
46
61
frames = [Image .fromarray (x ) for x in list (clip .iter_frames ())]
0 commit comments