package com.nmcc.demo.utils; import lombok.extern.slf4j.Slf4j; import org.bytedeco.javacpp.avcodec; import org.bytedeco.javacpp.avutil; import org.bytedeco.javacv.FFmpegFrameGrabber; import org.bytedeco.javacv.FFmpegFrameRecorder; import org.bytedeco.javacv.Frame; import java.io.File; /** * @author * @date 2020/2/17 * <p> * Description: */ @Slf4j public class ConvertVideo { private static final int FRAME_RATE = 30; Private static Final int VIDEO_BITRATE = 1048576 ; Private static Final int COMPRESS_WIDTH = 320. ; / ** * length relationship found video compression and video using * video 7M after more than 56s video a 9M compressed video * a 22M of 5s of compressed video 624k * @param File * @param checkCompress * @return * / public static String Convert (File File, Boolean checkCompress) { FFmpegFrameGrabber framegrabber = new new FFmpegFrameGrabber (file.getAbsolutePath ()); String fileName = null; Frame captured_frame = null; FFmpegFrameRecorder recorder = null; try { frameGrabber.start(); fileName = file.getAbsolutePath().replace(".mp4", "_edited.mp4"); log.info("wight:{},height:{}",frameGrabber.getImageWidth(), frameGrabber.getImageHeight()); int height = frameGrabber.getImageHeight(); int widht = frameGrabber.getImageWidth(); if(checkCompress && needCompress(file.length())){ height = calculateHeight(frameGrabber.getImageWidth(), frameGrabber.getImageHeight(), COMPRESS_WIDTH); widht = COMPRESS_WIDTH; log.info("new wight:{},height:{}",widht, height); } recorder = new FFmpegFrameRecorder(fileName, widht, height, frameGrabber.getAudioChannels()); recorder.setFrameRate(FRAME_RATE); //下面这行打开就报错 //recorder.setSampleFormat(frameGrabber.getSampleFormat()); recorder.setSampleRate(frameGrabber.getSampleRate()); //recorder.setAudioChannels(1); recorder.setVideoOption("preset", "veryfast"); // yuv420p,像素 recorder.setPixelFormat(avutil.AV_PIX_FMT_YUV420P); recorder.setVideoCodec(avcodec.AV_CODEC_ID_H264); recorder.setAudioCodec(avcodec.AV_CODEC_ID_AAC); recorder.setFormat("mp4"); //比特 //recorder.setVideoBitrate(VIDEO_BITRATE); recorder.start(); while (true) { try { captured_frame = frameGrabber.grabFrame(); if (captured_frame == null) { System.out.println("!!! end cvQueryFrame"); break; } recorder.setTimestamp(frameGrabber.getTimestamp()); recorder.record(captured_frame); } catch (Exception e) { } } recorder.stop(); recorder.release(); frameGrabber.stop(); } catch (Exception e) { e.printStackTrace(); } //File.delete (); return fileName; } / ** * need for compression, greater than 3MB * @param length * @return * / public static Boolean needCompress ( Long length) { log.info ( "Video size: {}" , length); return length> = 3145728 ; } / ** * calculate new geometric height * @param W * @param H * @param NW * @return * / Private static int calculateHeight ( int W, int H, int NW) { Double S = Integer.valueOf (H) .doubleValue () / Integer.valueOf (W) .doubleValue (); int height = ( int ) (NW * S); // If the width and height is not even recorder.start (); being given IF (height = 2% 0! ) { height + =. 1 ; } return height; } }
Introducing compressed video dependent java
<!--start视频截取相关-->
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>javacv</artifactId>
<version>1.4.3</version>
<exclusions>
<exclusion>
<groupId>org.bytedeco</groupId>
<artifactId>javacpp</artifactId>
</exclusion>
<exclusion>
<groupId>org.bytedeco.javacpp-presets</groupId>
<artifactId>flycapture</artifactId>
</exclusion>
<exclusion>
<groupId>org.bytedeco.javacpp-presets</groupId>
<artifactId>libdc1394</artifactId>
</exclusion>
<exclusion>
<groupId>org.bytedeco.javacpp-presets</groupId>
<artifactId>libfreenect</artifactId>
</exclusion>
<exclusion>
<groupId>org.bytedeco.javacpp-presets</groupId>
<artifactId>libfreenect2</artifactId>
</exclusion>
<exclusion>
<groupId>org.bytedeco.javacpp-presets</groupId>
<artifactId>librealsense</artifactId>
</exclusion>
<exclusion>
<groupId>org.bytedeco.javacpp-presets</groupId>
<artifactId>videoinput</artifactId>
</exclusion>
<exclusion>
<groupId>org.bytedeco.javacpp-presets</groupId>
<artifactId>opencv</artifactId>
</exclusion>
<exclusion>
<groupId>org.bytedeco.javacpp-presets</groupId>
<artifactId>tesseract</artifactId>
</exclusion>
<exclusion>
<groupId>org.bytedeco.javacpp-presets</groupId>
<artifactId>leptonica</artifactId>
</exclusion>
<exclusion>
<groupId>org.bytedeco.javacpp-presets</groupId>
<artifactId>flandmark</artifactId>
</exclusion>
<exclusion>
<groupId>org.bytedeco.javacpp-presets</groupId>
<artifactId>artoolkitplus</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>javacv-platform</artifactId>
<version>1.4.3</version>
<exclusions>
<exclusion>
<groupId>org.bytedeco</groupId>
<artifactId>javacv</artifactId>
</exclusion>
<exclusion>
<groupId>org.bytedeco.javacpp-presets</groupId>
<artifactId>flycapture-platform</artifactId>
</exclusion>
<exclusion>
<groupId>org.bytedeco.javacpp-presets</groupId>
<artifactId>libdc1394-platform</artifactId>
</exclusion>
<exclusion>
<groupId>org.bytedeco.javacpp-presets</groupId>
<artifactId>libfreenect-platform</artifactId>
</exclusion>
<exclusion>
<groupId>org.bytedeco.javacpp-presets</groupId>
<artifactId>libfreenect2-platform</artifactId>
</exclusion>
<exclusion>
<groupId>org.bytedeco.javacpp-presets</groupId>
<artifactId>librealsense-platform</artifactId>
</exclusion>
<exclusion>
<groupId>org.bytedeco.javacpp-presets</groupId>
<artifactId>videoinput-platform</artifactId>
</exclusion>
<exclusion>
<groupId>org.bytedeco.javacpp-presets</groupId>
<artifactId>opencv-platform</artifactId>
</exclusion>
<exclusion>
<groupId>org.bytedeco.javacpp-presets</groupId>
<artifactId>tesseract-platform</artifactId>
</exclusion>
<exclusion>
<groupId>org.bytedeco.javacpp-presets</groupId>
<artifactId>leptonica-platform</artifactId>
</exclusion>
<exclusion>
<groupId>org.bytedeco.javacpp-presets</groupId>
<artifactId>flandmark-platform</artifactId>
</exclusion>
<exclusion>
<groupId>org.bytedeco.javacpp-presets</groupId>
<artifactId>artoolkitplus-platform</artifactId>
</exclusion>
</exclusions>
</dependency>
<! - video capture relevant end ->