大家好,欢迎来到IT知识分享网。
packagecom.nmcc.demo.utils;importlombok.extern.slf4j.Slf4j;importorg.bytedeco.javacpp.avcodec;importorg.bytedeco.javacpp.avutil;importorg.bytedeco.javacv.FFmpegFrameGrabber;importorg.bytedeco.javacv.FFmpegFrameRecorder;importorg.bytedeco.javacv.Frame;importjava.io.File;/***@author* @date 2020/2/17
*
* Description:*/@Slf4jpublic classConvertVideo {private static final int FRAME_RATE = 30;private static final int VIDEO_BITRATE = 1048576;private static final int COMPRESS_WIDTH = 320;/*** 在使用时发现视频压缩和视频时长有关系
* 一个9M的56s的视频压缩后视频7M多
* 一个22M的5s的视频压缩后视频624K
*@paramfile
*@paramcheckCompress
*@return
*/
public staticString convert(File file, Boolean checkCompress) {
FFmpegFrameGrabber frameGrabber= newFFmpegFrameGrabber(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= newFFmpegFrameRecorder(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();
returnfileName;
}/*** 是否需要压缩,大于3MB
*@paramlength
*@return
*/
public static boolean needCompress(longlength){
log.info(“video size:{}”, length);return length >= 3145728;
}/*** 等比计算新高度
*@paramw
*@paramh
*@paramnw
*@return
*/
private static int calculateHeight(int w, int h, intnw){double s = Integer.valueOf(h).doubleValue() /Integer.valueOf(w).doubleValue();int height = (int) (nw *s);//如果宽和高不是偶数recorder.start();会报错
if(height % 2 !=0){
height+= 1;
}returnheight;
}
}
引入压缩视频的java依赖
org.bytedeco
javacv
1.4.3
org.bytedeco
javacpp
org.bytedeco.javacpp-presets
flycapture
org.bytedeco.javacpp-presets
libdc1394
org.bytedeco.javacpp-presets
libfreenect
org.bytedeco.javacpp-presets
libfreenect2
org.bytedeco.javacpp-presets
librealsense
org.bytedeco.javacpp-presets
videoinput
org.bytedeco.javacpp-presets
opencv
org.bytedeco.javacpp-presets
tesseract
org.bytedeco.javacpp-presets
leptonica
org.bytedeco.javacpp-presets
flandmark
org.bytedeco.javacpp-presets
artoolkitplus
org.bytedeco
javacv-platform
1.4.3
org.bytedeco
javacv
org.bytedeco.javacpp-presets
flycapture-platform
org.bytedeco.javacpp-presets
libdc1394-platform
org.bytedeco.javacpp-presets
libfreenect-platform
org.bytedeco.javacpp-presets
libfreenect2-platform
org.bytedeco.javacpp-presets
librealsense-platform
org.bytedeco.javacpp-presets
videoinput-platform
org.bytedeco.javacpp-presets
opencv-platform
org.bytedeco.javacpp-presets
tesseract-platform
org.bytedeco.javacpp-presets
leptonica-platform
org.bytedeco.javacpp-presets
flandmark-platform
org.bytedeco.javacpp-presets
artoolkitplus-platform
免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/23547.html