OpenCV Python VideoWriter bozuk videoları kaydeder

Sep 16 2020

Videodaki kareleri değiştirmeye başlamadan önce çok basit bir video kopyalamaya çalışıyorum. Ancak output.avi videosu, 2.8 mb barriers.avi videosuna kıyasla 5kb'lik bozuk bir dosyadır. OpenCV sürüm 4.2.0 ve Python sürüm 3.7.7 kullanıyorum.

İşte kod:

import cv2

input = cv2.VideoCapture("../video/barriers.avi")
height = int(input.get(cv2.CAP_PROP_FRAME_HEIGHT))
width = int(input.get(cv2.CAP_PROP_FRAME_WIDTH))

fourcc = cv2.VideoWriter_fourcc(*'DIVX')
out = cv2.VideoWriter('../video/output5.avi', fourcc, 30, (height, width), isColor=True)

while input.isOpened():
    # get validity boolean and current frame
    ret, frame = input.read()

    # if valid tag is false, loop back to start
    if not ret:
        break
    else:
        out.write(frame)

input.release()
out.release()

Çerçeve şeklini yazdırırsam, şunu elde ederim:

(480, 640, 3)

Not: Diğer yığın taşma çözümlerinden hiçbiri yardımcı olmadı.

Düzenle: cv2.imshow () kullanılırsa tüm çerçeveler iyi görüntülenir.

Yanıtlar

3 Ahx Sep 16 2020 at 03:08

Kodunuzda iki konuyu ele almak istiyorum:


Sorun 1: Bir.avivideo dosyasıoluşturmak istiyorsunuz. Bu nedenle, fourcc'nizi şu şekilde başlatmanız gerekirMJPG:

fourcc = cv2.VideoWriter_fourcc(*'MJPG')

Bir video dosyası oluşturmak için belirli kombinasyonlar vardır. Örneğin, bir oluşturmak .mp4istiyorsanız, fourcc'nizi olarak başlatırsınız *'mp4v'.


Sorun 2: Çıkış videosunun boyutunun giriş çerçevesi ile aynı olduğundan emin olun. Örneğin: Videowriter nesne boyutunuzu belirlediniz(height, width). O zaman çerçevenizin aynı boyutta olması gerekir:

frame = cv2.resize(frame, (height, width))

Kod:


import cv2

cap = cv2.VideoCapture("output.mp4")
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))

fourcc = cv2.VideoWriter_fourcc(*'MJPG')
out = cv2.VideoWriter('output5.avi', fourcc, 30, (width, height), isColor=True)

while cap.isOpened():
    # get validity boolean and current frame
    ret, frame = cap.read()

    # if valid tag is false, loop back to start
    if not ret:
        break
    else:
        frame = cv2.resize(frame, (width, height))
        out.write(frame)

cap.release()
out.release()