python保存视频代码_我的python代码没有将视频帧保存为图像

I'm trying to grab specific frames (e.g. frame 0, 10, 20, ...) within a video and save them as images using Python and CV2. For some reasons, my code only saves the first frame. All other frames are created, but with size 0 (they are corrupt).

How can I fix the problem?

import cv2

from numpy import integer

number = 10;

filename = "18s.mp4";

def uniform():

cap = cv2.VideoCapture(filename);

frame_count= int(cap.get(cv2.CAP_PROP_FRAME_COUNT));

print(frame_count)

for x in range(0, number):

frame_no = 1*(x/number)

frame_no_int=int(frame_no*frame_count)

cap.set(2,frame_no);

ret, frame = cap.read()

cv2.imwrite(filename+'_frame_'+str(frame_no_int)+'.jpg', frame);

# When everything done, release the capture

cap.release()

if __name__ == '__main__':

uniform()

解决方案

Apparently, CAP_PROP_POS_AVI_RATIO (constant 2, which you were using in cap.set()) doesn't work well. Take a look at the output of your modified script:

import cv2

from numpy import integer

number = 10

filename = 'chaplin.mp4'

def uniform():

cap = cv2.VideoCapture(filename)

frame_count= int(cap.get(cv2.CAP_PROP_FRAME_COUNT))

print(frame_count)

for x in range(0, number):

frame_pos_ratio = 1.0*x/number

frame_no_int=int(frame_pos_ratio*frame_count)

cap.set(cv2.CAP_PROP_POS_FRAMES, frame_no_int)

print (frame_no_int, cap.get(cv2.CAP_PROP_POS_AVI_RATIO))

ret, frame = cap.read()

cv2.imwrite('_frame_'+str(frame_no_int)+'.jpg', frame)

# Attempt to go the end of film

cap.set(cv2.CAP_PROP_POS_AVI_RATIO, 1)

print (cap.get(cv2.CAP_PROP_POS_FRAMES))

# When everything done, release the capture

cap.release()

if __name__ == '__main__':

uniform()

Output:

172

(0, 6.510416666666667e-05)

(17, 6.510416666666667e-05)

(34, 6.510416666666667e-05)

(51, 6.510416666666667e-05)

(68, 6.510416666666667e-05)

(86, 6.510416666666667e-05)

(103, 6.510416666666667e-05)

(120, 6.510416666666667e-05)

(137, 6.510416666666667e-05)

(154, 6.510416666666667e-05)

150.0

As you can see, cap.get(cv2.CAP_PROP_POS_AVI_RATIO) inside the cycle just returns a constant 6.51e-05.

And even though there are 174 frames, cap.set(cv2.CAP_PROP_POS_AVI_RATIO, 1) takes you only to frame 150, which is definitely a bug.

This behavior is in line with this question.

P.S. Interestingly, even cv2.CAP_PROP_FRAME_COUNT doesn't work properly. Apparently, my video file contained only 150 frames, but they were numbered from 22 to 171, as evidenced by ffprobe -show_frames chaplin.mp4 | grep coded_picture_number. So the output of CAP_PROP_FRAME_COUNT is just max(frame_no)+1.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值