簡體   English   中英

如何使用openCV將視頻導出為.mp4?

[英]How to export video as .mp4 using openCV?

我正在嘗試使用openCV將視頻導出為.mp4。 我已經嘗試了幾種編解碼器,但目前還沒有成功。

此功能可根據幀構造視頻:

def create_movie(self, out_directory, fps, total_frames):
    img1 = cv2.imread("temp/scr0.png")
    height, width, layers =  img1.shape
    codec = cv2.cv.CV_FOURCC('X','V','I','D')
    video = cv2.VideoWriter(out_directory, codec, fps, (width, height))

    for i in range(total_frames):
        img_name = "temp/scr" + str(i) + ".png"
        img = cv2.imread(img_name)
        video.write(img)

    video.release()
    cv2.destroyAllWindows()

我通常會使用不同的編解碼器得到下一條錯誤消息:

Tag XVID/0x44495658 incompatible with output codec id '13'

有可能做到這一點,怎么做?

有一個非直接的解決方案。 您導出為.avi,然后使用python的調用轉換為.mp4的調用終端命令。

from subprocess import call

dir = out_directory.strip(".avi")
command = "avconv -i %s.avi -c:v libx264 -c:a copy %s.mp4" % (dir, dir)
call(command.split())

回答這個問題可能有點晚了,但是如果您想使用OpenCV編寫.MP4文件,請嘗試以下操作:

import cv2
#your previous code here

fourcc = cv2.VideoWriter_fourcc(*'a\0\0\0')
out = cv2.VideoWriter('out.mp4', fourcc, fps, res)

#the character '\0' is the Null-Terminator or simply 0x00 in the ASCII-Table
#tag: *'a\0\0\0' corresponds to 0x00000061

#your following code here

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM