zoukankan      html  css  js  c++  java
  • Java基于整合ffmpeg的OpenCv3.5重组视频示例

    官方教程:https://riptutorial.com/opencv/example/28548/creating-a-video-with-opencv--java-

    个人简单封装一番,应用实例:

    import org.opencv.core.Core;
    import org.opencv.core.Mat;
    import org.opencv.core.Size;
    import org.opencv.videoio.VideoCapture;
    import org.opencv.videoio.VideoWriter;
    import org.opencv.videoio.Videoio;
    
    public class VideoOperatorTest {
    
        static {
            System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
        }
    
        public static void main(String[] args) {
    
            String localUrl = "/home/lab/1111.mp4";
            String outputPath = "/home/lab/2222.mp4";
            videoReassemble(localUrl,outputPath);
        }
    
        public static void videoReassemble(String inputPath,String outputPath) {
            VideoCapture capture = null;
            VideoWriter writer = null;
            long start = System.currentTimeMillis();
            try {
                capture = new VideoCapture(inputPath);
                int fourcc = VideoWriter.fourcc('x', '2', '6', '4'); // 编码方式 x264 
                Double fps = capture.get(Videoio.CV_CAP_PROP_FPS); // 帧率
                Size frameSize = new Size((int) capture.get(Videoio.CAP_PROP_FRAME_WIDTH),(int) capture.get(Videoio.CAP_PROP_FRAME_HEIGHT)); // 宽高
                writer = new VideoWriter(outputPath,fourcc,fps,frameSize,true);
                int i = 0;
                Mat frame = new Mat();
                while (capture.read(frame)) {
                    if (i == 0) {
                        byte buff[] = new byte[(int) (frame.total() * frame.channels())];
                        frame.get(0,0,buff);
                        // 修改首帧像素值
                        frame.put(0,0,buff);
                    }
                    writer.write(frame);
                    i++;
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (capture != null) {
                    capture.release();
                }
                if (writer != null) {
                    writer.release();
                }
            }
            long end = System.currentTimeMillis();
            System.out.println("cost millis : " + (end - start));
        }
    
    }
  • 相关阅读:
    CS round--36
    Vijos 1002 过河 dp + 思维
    汇编模拟36选7
    1137
    E. Mike and Foam 容斥原理
    Even-odd Boxes hackerrank 分类讨论
    112. 作业之地理篇 最小费用最大流模板题
    1550: Simple String 最大流解法
    Sam's Numbers 矩阵快速幂优化dp
    java.sql.SQLSyntaxErrorException: ORA-01722: 无效数字
  • 原文地址:https://www.cnblogs.com/nyatom/p/11239368.html
Copyright © 2011-2022 走看看