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()
熱點內容