官方教程: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)); } }