Imageio python包应做您想要的。这是使用此软件包的python代码段:
import pylabimport imageiofilename = '/tmp/file.mp4'vid = imageio.get_reader(filename, 'ffmpeg')nums = [10, 287]for num in nums: image = vid.get_data(num) fig = pylab.figure() fig.suptitle('image #{}'.format(num), fontsize=20) pylab.imshow(image)pylab.show()
for i, im in enumerate(vid): print('Mean of frame %i is %1.1f' % (i, im.mean()))
要安装imageio,可以使用pip:
pip install imageio
另一种解决方案是使用moviepy(它使用类似的代码来读取视频),但是我认为imageio更轻便并且可以完成工作。
对第一条评论的回应
为了检查整个文件中的标称帧速率是否相同,您可以计算迭代器中的帧数:
count = 0try: for _ in vid: count += 1except RuntimeError: print('something went wront in iterating, maybee wrong fps number')finally: print('number of frames counted {}, number of frames in metada {}'.format(count, vid.get_meta_data()['nframes']))In [10]: something went wront in iterating, maybee wrong fps number number of frames counted 454, number of frames in metada 461
为了显示每帧的时间戳:
try: for num, image in enumerate(vid.iter_data()): if num % int(vid._meta['fps']): continue else: fig = pylab.figure() pylab.imshow(image) timestamp = float(num)/ vid.get_meta_data()['fps'] print(timestamp) fig.suptitle('image #{}, timestamp={}'.format(num, timestamp), fontsize=20) pylab.show()except RuntimeError: print('something went wrong')
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)