Java / OpenCV-如何在openCV中进行无损h264视频编写?

时间:2018-08-14 13:47:51

标签: java opencv video ffmpeg h.264

上次我在java下的openCV中与VideoWriter进行了一些斗争。我想使用h.264编解码器在* .mp4容器中写入视频文件-但我看不到在openCV VideoWriter中切换比特率或质量的选项。我确实使用ffmpeg作为后端构建了openCV。我只想以精确的质量值将视频文件写入原始输入视频。 我也有一些代码可以完成这项工作

import org.opencv.core.Mat;
import org.opencv.core.Size;
import org.opencv.videoio.VideoWriter;
import org.opencv.videoio.Videoio;

public class VideoOutput
{
     private final int H264_CODEC = 33;

     private VideoWriter writer;

     private String filename;

     public VideoOutput (String filename)
     {
        writer = null;

        this.filename = filename;
    }

    public void initialize(double framesPerSecond, int height, int width) throws Exception
    {

        this.writer = new VideoWriter();

        this.writer.open(filename, H264_CODEC, framesPerSecond, new Size(width, height));

        if(!writer.isOpened())
        {
             Logging.LOGGER.severe("Could not create video output file " + filename + "\n");

             throw new Exception("Could not create video output file " + filename + "\n");
        }
    }

    public void setFrame(VideoFrame videoFrame) throws Exception
    {
         if (writer.isOpened()) 
         {
             Mat frame = ImageUtil.imageToMat(videoFrame.getFrame());

             writer.write(frame);

             frame.release();
         }
    }

我希望VideoWriter提供一些选项来完成这项工作,但事实并非如此。

那么在openCV和Java OR 下进行无损h264视频编写时,我是否缺少选项或标志?也许还有另一种方法可以做到这一点? 请帮助我-如果您已经完成此操作,我将不胜感激一些示例代码来完成任务。

更新

我现在有适合我的应用程序的解决方案,所以这里是:

String fps = Double.toString(this.config.getInputConfig().getFramesPerSecond());

Runtime.getRuntime().exec(
        new String[] {
        "C:\\ffmpeg-3.4.2-win64-static\\bin\\ffmpeg.exe",
        "-framerate",
        fps,
        "-i",
        imageOutputPath + File.separator +  "%01d.jpg",
        "-c:v",
        "libx265",
        "-crf",
        "1",
        imageOutputPath + File.separator +  "ffmpeg.mp4"
        }
    );

@Gyan 的信用,他们在这篇文章中给了我正确的ffmpeg呼叫:

Win/ffmpeg - How to generate a video from images under ffmpeg?

礼物

0 个答案:

没有答案