【问题标题】:opencv - videowriter control bitrateopencv - 录像机控制比特率
【发布时间】:2016-12-05 19:07:19
【问题描述】:

我有一个使用来自 opencv 的视频编写器的工作 python 脚本。

来源https://gist.github.com/stanchiang/b4e4890160a054a9c1d65f9152172600


如果我接收一个文件,无论我是简单地将视频帧传递给编写器(有效地复制文件)还是我尝试编辑帧,文件总是更大。我希望它不比原版大(因为如果你阅读我的脚本,我会模糊很多东西)。

在检查了他们的元数据后,ffprobe -v quiet -print_format json -show_format -show_streams inputFile.mp4 我注意到新文件的比特率比以前高 5.5 倍以上。

来源https://www.diffchecker.com/8r2syeln


由于比特率是文件大小的一个重要决定因素,我想知道是否

  1. 我可以通过视频编写器硬编码新文件所需的比特率
  2. 是否出于某种原因需要大幅提高比特率

【问题讨论】:

  • 您可能想切换到 FFmpeg,因为那里对所有视频参数的控制非常好,而且根据我的经验,输出的比特率不会从输入改变。
  • 不能一直切换对吧?该脚本使用 opencv 算法逐帧编辑视频。有没有办法可以使用 ffmpeg 逐帧写入新视频?
  • @stanley 否。您方法的唯一方法是使用 OpenCV 通过生成所有帧来完全编写视频,然后在完成后使用 FFMPEG 通过更改比特率来压缩视频.但是,可以使用管道直接将 RGB 数据发送到 FFMPEG,但不使用 OpenCV。
  • @rayryeng 如果我可以将 rgb 数据发送到 ffmpeg,那么是否有办法将我的 opencv 编辑帧转换为 rgb 数据以供 ffmpeg 使用?这段代码看起来如何?

标签: python opencv video-processing


【解决方案1】:

基本上这个答案https://stackoverflow.com/a/13298538/1079379

# import packages
from PIL import Image
from subprocess import Popen, PIPE
from imutils.video import VideoStream
from imutils.object_detection import non_max_suppression
from imutils import paths
import cv2
import numpy as np
import imutils

# ffmpeg setup
p = Popen(['ffmpeg', '-y', '-f', 'image2pipe', '-vcodec', 'mjpeg', '-r', '24', '-i', '-', '-vcodec', 'h264', '-qscale', '5', '-r', '24', 'video.mp4'], stdin=PIPE)

video = cv2.VideoCapture('videos.mp4')

while True:
    ret, frame = video.read()
    if ret:
        frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
        im = Image.fromarray(frame)
        im.save(p.stdin, 'JPEG')
    else:
        break

p.stdin.close()
p.wait()
video.release()
cv2.destroyAllWindows()

【讨论】:

    【解决方案2】:

    我强大的VidGear Python 库的WriteGear API 在任何平台上自动将 OpenCV 帧流水线化到 FFmpeg 的过程,同时提供相同的 opencv-python 语法。这是一个基本的python示例:

    # import libraries
    from vidgear.gears import WriteGear
    import cv2
    
    output_params = {"-vcodec":"libx264", "-crf": 0, "-preset": "fast"} #define (Codec,CRF,preset) FFmpeg tweak parameters for writer
    
    stream = cv2.VideoCapture(0) #Open live webcam video stream on first index(i.e. 0) device
    
    writer = WriteGear(output_filename = 'Output.mp4', compression_mode = True, logging = True, **output_params) #Define writer with output filename 'Output.mp4' 
    
    # infinite loop
    while True:
        
        (grabbed, frame) = stream.read()
        # read frames
    
        # check if frame empty
        if not is grabbed:
            #if True break the infinite loop
            break
        
    
        # {do something with frame here}
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    
    
        # write a modified frame to writer
        writer.write(gray) 
           
            # Show output window
        cv2.imshow("Output Frame", frame)
    
        key = cv2.waitKey(1) & 0xFF
        # check for 'q' key-press
        if key == ord("q"):
            #if 'q' key-pressed break out
            break
    
    cv2.destroyAllWindows()
    # close output window
    
    stream.release()
    # safely close video stream
    writer.close()
    # safely close writer
    

    来源:https://abhitronix.github.io/vidgear/latest/gears/writegear/compression/usage/#using-compression-mode-with-opencv

    您可以查看VidGear Docs 了解更多高级应用程序和功能。

    【讨论】:

      猜你喜欢
      • 2020-06-10
      • 1970-01-01
      • 2016-07-31
      • 1970-01-01
      • 2019-09-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-04
      相关资源
      最近更新 更多