opencv-录像机控制比特率 [英] opencv - videowriter control bitrate

查看:544
本文介绍了opencv-录像机控制比特率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个有效的python脚本,该脚本使用来自opencv的视频编写器.

I have a working python script that uses the video writer from opencv.

source https://gist.github.com/stanchiang/b4e4890160a054a9c1d65f9152172600

如果我放入一个文件,并且无论我只是将视频帧直接传递给编写器(有效地复制了文件)还是尝试编辑该帧,则文件总是更大.我希望它不大于原始内容(因为如果您阅读我的脚本,我会模糊很多东西).

If i take in a file, and regardless of whether I simply pass the video frame through to the writer (effectively duplicating the file) or if i try to edit the frame, the file is always larger. I would like for it to be no larger than the original (since if you read my script i'm blurring a lot of stuff).

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

After checking their metadata, with ffprobe -v quiet -print_format json -show_format -show_streams inputFile.mp4 I notice that the bitrate of the new file is over 5.5x higher than before.

source https://www.diffchecker.com/8r2syeln

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

since bitrate is a big determinant of file size, I'm wondering if

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

推荐答案

基本上是这个答案 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()

这篇关于opencv-录像机控制比特率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆