简体   繁体   English

如何使用 opencv python 以 mp4 格式保存视频捕获

[英]How to save video capture in mp4 format with opencv python

Guys I'm capturing the desktop video with opencv with python with the following code:伙计们,我正在使用以下代码使用 opencv 和 python 捕获桌面视频:

import numpy as np
import cv2                                           
from mss import mss                                  
from PIL import Image

bounding_box = {'top': 100, 'left': 0, 'width': 400, 'height': 300}

sct = mss()
                                                 
while True:
 sct_img = sct.grab(bounding_box)                     
 cv2.imshow('screen', np.array(sct_img))             
 if(cv2.waitKey(1) & 0xFF) == ord('q'):
  cv2.destroyAllWindows()
  break

I want to be able to save this capture in mp4 format, how can I do this?我希望能够以 mp4 格式保存此捕获,我该怎么做?

It should work something like this.它应该像这样工作。

import numpy as np
import cv2                                           
from mss import mss                                  
from PIL import Image

bounding_box = {'top': 100, 'left': 0, 'width': 400, 'height': 300}

sct = mss()
fourcc = cv2.VideoWriter_fourcc(*'MP4V')
out = cv2.VideoWriter('output.mp4', fourcc, 20.0, (640,480))
                                                
while True:
  sct_img = sct.grab(bounding_box)   
  out.write(np.array(sct_img))                  
  cv2.imshow('screen', np.array(sct_img))             
  if(cv2.waitKey(1) & 0xFF) == ord('q'):
      cv2.destroyAllWindows()
      break

Official documentation shows example in Getting Started with Videos官方文档在Getting Started with Videos中展示了示例

import numpy as np
import cv2 as cv

cap = cv.VideoCapture(0)

# Define the codec and create VideoWriter object
fourcc = cv.VideoWriter_fourcc(*'XVID')
out = cv.VideoWriter('output.avi', fourcc, 20.0, (640,  480))

while cap.isOpened():
    ret, frame = cap.read()

    if not ret:
        print("Can't receive frame (stream end?). Exiting ...")
        break

    frame = cv.flip(frame, 0)

    # write the flipped frame
    out.write(frame)

    cv.imshow('frame', frame)

    if cv.waitKey(1) == ord('q'):
        break

# Release everything if job is finished
cap.release()
out.release()
cv.destroyAllWindows()

First you have to set codec using 4-chars code.首先,您必须使用 4 个字符的代码设置编解码器。 There is page fourcc.org with codes.有带有代码的页面fourcc.org

Different systems may use different codecs - mostly DIVX , XVID , MJPG , X264 , WMV1 , WMV2 .不同的系统可能使用不同的编解码器——主要是DIVXXVIDMJPGX264WMV1WMV2

fourcc = cv.VideoWriter_fourcc(*'XVID')

Next you have to set filename with extension, FPS , width, height接下来,您必须设置文件名和扩展名、 FPS 、宽度、高度

out = cv.VideoWriter('output.avi', fourcc, 20.0, (400,  300))

Some codecs not work with some extensions - you may need to check different combinations ( .avi , .mp4 , .mkv , .mov , etc.)某些编解码器不适用于某些扩展 - 您可能需要检查不同的组合( .avi.mp4.mkv.mov等)

If images have 400x300 and you want video 800x600 then set VideoWriter(..., (800, 600)) but you also have to resize every frames before writing because VideoWriter doesn't resize it automatically - it skip frames (without error) and finally it creates empty file (~4kb).如果图像有400x300而你想要视频800x600然后设置VideoWriter(..., (800, 600))但你还必须在写入之前调整每一帧的大小因为VideoWriter不会自动调整它的大小 - 它跳过帧(没有错误)并且最后它创建空文件(~4kb)。

frame = cv2.resize(frame, (800, 600))

out.write(frame)

VideoWriter(.., 20.0, ...) sets 20 FPS (frames per second) but it is not speed of writing but it is information for video players how fast they have to display it. VideoWriter(.., 20.0, ...)设置20 FPS (每秒帧数),但它不是写入速度,而是视频播放器必须以多快的速度显示它的信息。

If you create 20 frames in every second then player will display it in correct speed.如果您每秒创建 20 帧,那么播放器将以正确的速度显示它。 If you create 10 frames in every second then player will display it 2x faster.如果您每秒创建 10 帧,那么播放器的显示速度将提高 2 倍。 If you create 40 frames in every second then player will displau it with half speed.如果你每秒创建 40 帧,那么播放器将以一半的速度显示它。


It uses program FFMPEG and you can see list of codecs running in system console/terminal它使用程序FFMPEG ,您可以看到在系统控制台/终端中运行的编解码器列表

ffmpeg -codecs

or in source code (with 4-chars codes)或在源代码中(带有 4 个字符的代码)

http://ffmpeg.org/doxygen/trunk/isom_8c-source.html http://ffmpeg.org/doxygen/trunk/isom_8c-source.html

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

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