1. 程式人生 > >使用opencv實現視訊分解圖片和圖片合成視屏

使用opencv實現視訊分解圖片和圖片合成視屏

# 視訊分解成圖片
import cv2
cap = cv2.VideoCapture("22.mp4")  # 獲取開啟的控制代碼
isOpened = cap.isOpened  # 判斷是否開啟
print(isOpened)
fps = cap.get(cv2.CAP_PROP_FPS)
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
print(fps,width,height)
i = 0
while(isOpened):
    if i == 10:
        break
    else:
        i = i+1
    (flag,frame) = cap.read()
    fileName = "image" +str(i) +".jpg"
    print(fileName)
    if flag == True:
        cv2.imwrite(fileName,frame,(cv2.IMWRITE_JPEG_QUALITY,100))
print('end')


#圖片合成視訊
import cv2
img = cv2.imread("1.jpg")
imgInfo = img.shape
size = (imgInfo[0],imgInfo[1])
print(size)
videoWrite = cv2.VideoWriter("33.mp4",-1,5,size)# 寫入物件的建立
# 檔名稱    2. 編碼器    3.幀率    4. size
for i in range(1,10):
    fileName = "image"+str(i)+".jpg"
    img = cv2.imread(fileName)
    videoWrite.write(img) # 寫入方法  
    
print('end')