opencvpython视频
发布时间: 2024-12-21 18:11:31
❶ python OpenCV视频拆分图片代码
# coding:utf-8
import cv2
import numpy as np
import os
print("1")
vc = cv2.VideoCapture("123.mp4")
C = 1
print("2")
if vc.isOpened():
rVal, frame = vc.read()
else:
print("3")
rVal = False
while rVal:
print(C)
if C % 1000 == 0: # every 5 fps write frame to img
path='./image/'+str(C)+'.jpg'
cv2.imwrite(path, frame)
# cropped001 = frame2[0:300,300:600] #y change from 0 to 300 x change from 300 to 600
# cv2.im write('./cropped/'+str(c)+'001.jpg',cropped001)
print(C)
cv2.waitKey(1)
C = C + 1
vc.release()
❷ python如何用opencv把一个视频按每10秒一小段切下来
你好,下面是相应的一个代码,你可以参考一下:
importcv2
importos
#先导入openCV
#将一个长的视频缩短,按照比例缩短,只保存其中的一部分
#如下面代码就是将一个长视频每10秒取10秒,合并成一个短视频
cap=cv2.VideoCapture('myvideo.avi')
ifnotcap.isOpened():
print('videoisnotopened')
else:
#每秒25帧
num=0
#取10秒
needTime=250
#每10秒
timeSpace=250
#获取视频帧率
fps=cap.get(cv2.CAP_PROP_FPS)
#AVI格式编码输出XVID
videoWriter=cv2.VideoWriter('result//resultVideo_2.avi',cv2.VideoWriter_fourcc('X','V','I','D'),fps,frameSize=(320,288))
while(1):
success,frame=cap.read()
if(num%timeSpace<=needTime):
videoWriter.write(frame)
print('write'+str(num))
num=num+1
ifnotsuccess:
print('finished')
break
ifcv2.waitKey(100)&0xFF==ord('q'):
break
cap.release()
热点内容