简体   繁体   English

无法在 opencv 中保存视频

[英]Can't save a video in opencv

I am trying to save my video using opencv write method but the video is saved with 0 kb.我正在尝试使用 opencv 写入方法保存我的视频,但视频以 0 kb 保存。 what's wrong in my code.我的代码有什么问题。

  import cv2

  cap = cv2.VideoCapture("k1.mp4")
  fgbg = cv2.bgsegm.createBackgroundSubtractorMOG()
  fourcc = cv2.VideoWriter_fourcc(*'MP42')
  out = cv2.VideoWriter('output.mp4', fourcc, 20.0, (640,480))

  while cap.isOpened():
     ret, frame = cap.read()
     gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
     fgmask = fgbg.apply(gray)
     thresh = 2
     maxValue = 255
     ret, th1 = cv2.threshold(fgmask, thresh, maxValue, cv2.THRESH_BINARY)

     color_space = cv2.applyColorMap(th1, cv2.COLORMAP_JET)
     result_vid = cv2.addWeighted(frame, 0.7, color_space, 0.7, 0)
     cv2.imshow("vid", result_vid)
     out.write(result_vid)
     if cv2.waitKey(20) == ord('q'):
         break

 cap.release()
 out.release()
 cv2.destroyAllWindows()
 import cv2
 cap = cv2.VideoCapture(0)

 # Automatically grab width and height from video feed
 # (returns float which we need to convert to integer for later on!)
 width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
 height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))


 # MACOS AND LINUX: *'XVID' (MacOS users may want to try VIDX as well just in case)
 # WINDOWS *'VIDX'
 writer = cv2.VideoWriter('local_capture.mp4', cv2.VideoWriter_fourcc(*'VIDX'),25, (width, height))


 # This loop keeps recording until you hit Q or escape the window
 # You may want to instead use some sort of timer, like from time import sleep and then just record for 5 seconds.
while True:

     # Capture frame-by-frame
     ret, frame = cap.read()


     # Write the video
     writer.write(frame)

     # Display the resulting frame
     cv2.imshow('frame',frame)

     # This command let's us quit with the "q" button on a keyboard.
     # Simply pressing X on the window won't work!
     if cv2.waitKey(1) & 0xFF == ord('q'):
         break
cap.release()
writer.release()
cv2.destroyAllWindows()

The problem is that the video codec and the video container format do not match.问题是视频编解码器和视频容器格式不匹配。

When executing your code, I am getting an error message (in the console windows):执行代码时,我收到一条错误消息(在控制台窗口中):

OpenCV: FFMPEG: tag 0x3234504d/'MP42' is not supported with codec id 15 and format 'mp4 / MP4 (MPEG-4 Part 14)' OpenCV: FFMPEG: tag 0x3234504d/'MP42' 不支持编解码器 ID 15 和格式 'mp4 / MP4 (MPEG-4 Part 14)'
[mp4 @ 00000155e95dcec0] Could not find tag for codec msmpeg4v2 in stream #0, codec not currently supported in container [mp4 @ 00000155e95dcec0] 在流 #0 中找不到编解码器 msmpeg4v2 的标签,容器当前不支持编解码器

  • You are using fourcc = cv2.VideoWriter_fourcc(*'MP42') , and M420 applies video codec MPEG-4v2 .您正在使用fourcc = cv2.VideoWriter_fourcc(*'MP42') ,并且M420应用视频编解码器MPEG-4v2
  • Video output file name is 'output.mp4' .视频输出文件名为'output.mp4'
    The .mp4 extension applies MP4 container format. .mp4扩展名应用 MP4 容器格式。

Apparently .mp4 video file cannot contain video encoded with MPEG-4v2 codec.显然.mp4视频文件不能包含使用MPEG-4v2编解码器编码的视频。

You may either change codec, or change file format.您可以更改编解码器或更改文件格式。

Example:例子:

  • Changing output file name to 'output.avi' or 'output.wmv' works.将输出文件名更改为'output.avi''output.wmv'有效。
  • Changing the codec to MPEG-4 : fourcc = cv2.VideoWriter_fourcc(*'mp4v') (and keeping file name 'output.mp4' ) also works.将编解码器更改为MPEG-4fourcc = cv2.VideoWriter_fourcc(*'mp4v') (并保留文件名'output.mp4' )也有效。

One more issue:还有一个问题:

Add the following code after ret, frame = cap.read() :ret, frame = cap.read()之后添加以下代码:

if not ret:
    break;

Although your solution was to match the video codec and the video container format correctly, I wanted to add that another common reason the output file size is 0 kb when writing videos with OpenCV is a discrepancy between the captured video frame size and the output video frame size. Although your solution was to match the video codec and the video container format correctly, I wanted to add that another common reason the output file size is 0 kb when writing videos with OpenCV is a discrepancy between the captured video frame size and the output video frame尺寸。 This can be solved by replacing the hard coded output frame size with one calculated from the input video.这可以通过将硬编码的 output 帧大小替换为从输入视频计算得出的帧大小来解决。

cap = cv2.VideoCapture('input.mp4')
w = int(cap.get(3))
h = int(cap.get(4))
frameSize = (w, h)
fps = 20
fourCC = cv2.VideoWriter_fourcc(*'MP4V')
out = cv2.VideoWriter('output.mp4', fourCC, fps, frameSize)

while(True):
    ret, frame = cap.read()
    ...
    result_vid = ...
    ...
    out.write(result_vid)
    ...

cap.release()
out.release()
cv2.destroyAllWindows()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM