在Python中为OpenCV视频对象指定压缩质量 [英] Specify Compression Quality in Python for OpenCV Video Object

查看:853
本文介绍了在Python中为OpenCV视频对象指定压缩质量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须使用单个目录中包含的大量jpeg图像来创建低分辨率AVI视频。我有近一百个目录,每个目录可能包含数千个图像。



为了使该过程自动化,我使用OpenCV编写了一个python脚本来创建视频对象,从给定目录加载每个图像,并将每个图像写入到该目录的特定视频文件。所有这些都很好。我的问题是如何控制视频对象的压缩质量。



VideoWriter模块接受5个参数。第二个参数 fourcc设置压缩代码。


cv2.VideoWriter.open(filename,fourcc,fps,frameSize [,isColor])


我们可以在cv2.VideoWriter中使用四个字符代码(即fourcc)指定压缩代码。


fourcc = cv2.cv.CV_FOURCC('M','S','V','C')#Microspoft视频1


这种方法行之有效,只是视频对象的压缩质量始终设置为最大。



如果让fourcc = -1,则将打开一个Windows视频压缩对话框,该对话框允许用户选择视频压缩并在两者之间设置压缩质量(或时间质量比) 0和100。





每个视频必须是AVI,并且必须满足某些文件大小要求。如果使用最大压缩质量,则视频文件太大。但是,要求用户为每个视频选择视频压缩和压缩质量会破坏脚本的自动化。



那么,如何在不使用Windows Video Compression对话框的情况下为视频对象指定一种压缩质量?



我的代码的全部内容发布在下面

  import cv2,os 

Workspace = J:向jpg进行AVI测试
year = 2014
file_extension = avi
Image_file_dir =工作区+ \\12-ECD-BONNETT CREEK Y INT( +年+)
打印Image_file_dir
Image_file_list = os.listdir(Image_file_dir)
打印 Image_file_list: + str(Image_file_list)

img_cnt = 0 $对于Image_file_list中的Image_file的b
$ b:

如果Image_file.split(。)[-1] == jpg:

Image_file_path = Image_file_dir + \\ + Image_file
打印Image_file_path

img1 = cv2.imread(Image_file_path)

height,width,layers = img1.shape

休息

fourcc = cv2.cv.CV_FOURCC('M','S','V','C')#Microspoft Video 1
## fourcc = -1
video_object =工作区+ \\12-ECD-邦纳特CREEK Y INT( +年+)。 + file_extension
video = cv2.VideoWriter(video_object,\
fourcc,\
9,\
(宽度,高度))

对于Image_file_list中的Image_file:

如果Image_file.split(。)[-1] == jpg:

img_cnt + = 1
Image_file_path = Image_file_dir + \\ + Image_file
print str(img_cnt)+) + Image_file_path

img1 = cv2.imread(Image_file_path)

视频.write(img1)

cv2.destroyAllWindows()
video.release()


解决方案

对不起! OpenCV没有界面可让您配置压缩率。



<我最近需要做类似的事情。我的做法是以编程方式调用 ffmpeg 并将其用于将OpenCV创建的AVI转换为MP4文件。



这会将100MB的文件转换为不足900KB


I must create low resolution AVI videos using a large number of jpeg images contained in individual directories. I have nearly one hundred directories, and each directory may contain many thousands of images.

In order to automate this process, I have written a python script using OpenCV to create a video object, load each image from a given directory, and write each image to a specific video file for that directory. All this works great. My problem is how to control the compression quality for the video object.

The VideoWriter module accepts 5 parameters. The second parameter, 'fourcc', sets the compression code.

cv2.VideoWriter.open(filename, fourcc, fps, frameSize[, isColor])

We can specify a compression code within cv2.VideoWriter using a "four character code" (i.e. fourcc).

fourcc = cv2.cv.CV_FOURCC('M','S','V','C') #Microspoft Video 1

This approach works except the compression quality of the video object is always set at it's maximum.

If we let fourcc = -1, a Windows Video Compression dialog box opens which allows the user to select a video compression AND set Compression Quality (or Temporal Quality Ratio) between 0 and 100.

Each video must be an AVI and must meet certain file size requirements. The video file is too big if the maximum compression quality is used. However, requiring the user to select the video compression and compression quality for each video defeats the script's automation.

So, how does one specify a the compression quality for the video object without using the Windows Video Compression dialog box?

The entirety of my code is posted below

import cv2, os

Workspace = "J:\jpg to AVI test"
year = "2014"
file_extension = "avi"
Image_file_dir = Workspace + "\\12 - ECD-BONNETT CREEK Y INT (" + year + ")"
print Image_file_dir
Image_file_list = os.listdir(Image_file_dir)
print "Image_file_list: " + str(Image_file_list)

img_cnt = 0

for Image_file in Image_file_list:

    if Image_file.split(".")[-1] == "jpg":

        Image_file_path = Image_file_dir + "\\" + Image_file
        print Image_file_path

        img1 = cv2.imread(Image_file_path)

        height , width , layers =  img1.shape

        break

fourcc = cv2.cv.CV_FOURCC('M','S','V','C') #Microspoft Video 1
##fourcc = -1
video_object = Workspace + "\\12 - ECD-BONNETT CREEK Y INT (" + year + ")." + file_extension
video = cv2.VideoWriter(video_object,\
                        fourcc,\
                        9,\
                        (width,height))

for Image_file in Image_file_list:

    if Image_file.split(".")[-1] == "jpg":

        img_cnt += 1
        Image_file_path = Image_file_dir + "\\" + Image_file
        print str(img_cnt) + ") " + Image_file_path

        img1 = cv2.imread(Image_file_path)

        video.write(img1)

cv2.destroyAllWindows() 
video.release()

解决方案

Sorry! OpenCV has no interface to let you configure the compression rate.

I recently needed to do a similar thing. My course of action was to invoke ffmpeg programatically and use it to convert the AVI created by OpenCV into a MP4 file.

This turned a 100MB file into less than 900KB.

这篇关于在Python中为OpenCV视频对象指定压缩质量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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