186 lines
7.1 KiB
Plaintext
186 lines
7.1 KiB
Plaintext
//package com.sercurityControl.proteam.util;
|
|
//
|
|
//import com.sercurityControl.proteam.controller.VideoFormat;
|
|
//import org.bytedeco.ffmpeg.global.avcodec;
|
|
//import org.bytedeco.javacv.FFmpegFrameGrabber;
|
|
//import org.bytedeco.javacv.FFmpegFrameRecorder;
|
|
//import org.bytedeco.javacv.Frame;
|
|
//import org.slf4j.Logger;
|
|
//import org.slf4j.LoggerFactory;
|
|
//
|
|
//import java.io.File;
|
|
//import java.nio.file.FileAlreadyExistsException;
|
|
//import java.nio.file.Files;
|
|
//import java.nio.file.StandardCopyOption;
|
|
//
|
|
///**
|
|
// * @author bonus
|
|
// * @data 2023/5/22 11:19
|
|
// * @description convertToMp4
|
|
// */
|
|
//public class WebmUtil {
|
|
//
|
|
// private static final Logger logger = LoggerFactory.getLogger(WebmUtil.class);
|
|
//
|
|
// private static final String DEFAULT_EXTNAME = "mp4";
|
|
//
|
|
// private static final String DEFAULT_FORMAT = "mov,mp4,m4a,3gp,3g2,mj2";
|
|
//
|
|
// private VideoFormat videoFormat;
|
|
//
|
|
// public WebmUtil(String absolutePath) {
|
|
// this(new File(absolutePath));
|
|
// }
|
|
//
|
|
// public WebmUtil(String parent, String fileName) {
|
|
// this(new File(parent, fileName));
|
|
// }
|
|
//
|
|
// public WebmUtil(File file) {
|
|
// VideoFormat videoFormat = new VideoFormat();
|
|
// FFmpegFrameGrabber frameGrabber = null;
|
|
// try {
|
|
// videoFormat.setExists(file.exists());
|
|
// videoFormat.setFile(file.isFile());
|
|
// videoFormat.setAbsolutePath(file.getAbsolutePath());
|
|
// videoFormat.setParent(file.getParent());
|
|
// videoFormat.setName(file.getName());
|
|
// if (!file.isFile()) {
|
|
// return;
|
|
// }
|
|
// int dotLastIndex = videoFormat.getName().lastIndexOf(".");
|
|
// if (dotLastIndex > 0 && dotLastIndex < videoFormat.getName().length() - 1) {
|
|
// videoFormat.setSimpleName(videoFormat.getName().substring(0, dotLastIndex));
|
|
// videoFormat.setExtName(videoFormat.getName().substring(dotLastIndex + 1));
|
|
// } else {
|
|
// videoFormat.setSimpleName(videoFormat.getName());
|
|
// }
|
|
// frameGrabber = FFmpegFrameGrabber.createDefault(file);
|
|
// frameGrabber.start();
|
|
// videoFormat.setVideoCodec(frameGrabber.getVideoCodec());
|
|
// videoFormat.setAudioCodec(frameGrabber.getAudioCodec());
|
|
// videoFormat.setFormat(frameGrabber.getFormat());
|
|
// } catch (Exception e) {
|
|
// logger.warn(file.getAbsolutePath() + " -> error", e);
|
|
// } finally {
|
|
// if (frameGrabber != null) {
|
|
// try {
|
|
// frameGrabber.close();
|
|
// } catch (org.bytedeco.javacv.FrameGrabber.Exception e) {
|
|
// logger.warn("frameGrabber.close异常", e);
|
|
// }
|
|
// }
|
|
// }
|
|
// this.videoFormat = videoFormat;
|
|
// }
|
|
//
|
|
// protected VideoFormat getVideoFormat() {
|
|
// return videoFormat;
|
|
// }
|
|
//
|
|
// public boolean isVideo() {
|
|
// return this.getVideoFormat().getVideoCodec() != avcodec.AV_CODEC_ID_NONE;
|
|
// }
|
|
//
|
|
// protected boolean hasAudio() {
|
|
// return this.getVideoFormat().getAudioCodec() != avcodec.AV_CODEC_ID_NONE;
|
|
// }
|
|
//
|
|
// protected boolean isMp4() {
|
|
// return DEFAULT_EXTNAME.equalsIgnoreCase(this.getVideoFormat().getExtName());
|
|
// }
|
|
//
|
|
// protected boolean canPlayInH5IgnoreExtName() {
|
|
// if (!this.isVideo()) {
|
|
// return false;
|
|
// }
|
|
// VideoFormat videoFormat = this.videoFormat;
|
|
// if (this.hasAudio() && videoFormat.getAudioCodec() != avcodec.AV_CODEC_ID_AAC) {
|
|
// return false;
|
|
// }
|
|
// if (videoFormat.getVideoCodec() != avcodec.AV_CODEC_ID_H264) {
|
|
// return false;
|
|
// }
|
|
// return DEFAULT_FORMAT.equals(videoFormat.getFormat());
|
|
// }
|
|
//
|
|
// public boolean canPlayInH5() {
|
|
// if (!this.isMp4()) {
|
|
// return false;
|
|
// }
|
|
// return this.canPlayInH5IgnoreExtName();
|
|
// }
|
|
//
|
|
// public String convert2Mp4() throws Exception {
|
|
// return this.convert2Mp4(this.videoFormat.getParent());
|
|
// }
|
|
//
|
|
// /**
|
|
// * @param outputParent 输出目录
|
|
// * @return 输出文件的绝对路径
|
|
// * @throws Exception
|
|
// */
|
|
// public String convert2Mp4(String outputParent) throws Exception {
|
|
// VideoFormat videoFormat = this.videoFormat;
|
|
// if (!this.isVideo()) {
|
|
// throw new org.bytedeco.javacv.FrameGrabber.Exception("[" + videoFormat.getAbsolutePath() + "] 不是视频文件");
|
|
// }
|
|
// File outfile = new File(outputParent);
|
|
// if (outfile.isFile()) {
|
|
// throw new FileAlreadyExistsException("[" + outputParent + "] 是文件并且已存在");
|
|
// }
|
|
// if (!outfile.exists()) {
|
|
// outfile.mkdirs();
|
|
// }
|
|
// String outFilePath = new File(outputParent, videoFormat.getSimpleName() + "_recode." + DEFAULT_EXTNAME).getAbsolutePath();
|
|
// outFilePath = outFilePath.replace("\\", "/");
|
|
// //如果视频本来就能在浏览器中播放,则只进行复制
|
|
// if (this.canPlayInH5IgnoreExtName()) {
|
|
// Files.copy(new File(videoFormat.getParent(), videoFormat.getName()).toPath(), new File(outFilePath).toPath(), StandardCopyOption.REPLACE_EXISTING);
|
|
// return outFilePath;
|
|
// }
|
|
// FFmpegFrameGrabber frameGrabber = FFmpegFrameGrabber.createDefault(new File(videoFormat.getParent(), videoFormat.getName()));
|
|
// Frame capturedFrame = null;
|
|
// FFmpegFrameRecorder recorder = null;
|
|
// try {
|
|
// frameGrabber.start();
|
|
// recorder = new FFmpegFrameRecorder(outFilePath, frameGrabber.getImageWidth(), frameGrabber.getImageHeight(), frameGrabber.getAudioChannels());
|
|
// recorder.setVideoCodec(avcodec.AV_CODEC_ID_H264);
|
|
// recorder.setFormat(DEFAULT_EXTNAME);
|
|
// recorder.setFrameRate(frameGrabber.getFrameRate());
|
|
// recorder.setVideoBitrate(frameGrabber.getVideoBitrate());
|
|
// recorder.setAspectRatio(frameGrabber.getAspectRatio());
|
|
// recorder.setAudioOptions(frameGrabber.getAudioOptions());
|
|
// recorder.setSampleRate(frameGrabber.getSampleRate());
|
|
// recorder.setAudioCodec(avcodec.AV_CODEC_ID_AAC);
|
|
// recorder.start();
|
|
// while (true) {
|
|
// capturedFrame = frameGrabber.grabFrame();
|
|
// if (capturedFrame == null) {
|
|
// logger.info(outFilePath + "转码完成");
|
|
// break;
|
|
// }
|
|
// recorder.record(capturedFrame);
|
|
// }
|
|
// } finally {
|
|
// if (recorder != null) {
|
|
// try {
|
|
// recorder.close();
|
|
// } catch (Exception e) {
|
|
// logger.warn("recorder.close异常", e);
|
|
// }
|
|
// }
|
|
// try {
|
|
// frameGrabber.close();
|
|
// } catch (org.bytedeco.javacv.FrameGrabber.Exception e) {
|
|
// logger.warn("frameGrabber.close异常", e);
|
|
// }
|
|
// File fileDelete = new File(videoFormat.getAbsolutePath());
|
|
// fileDelete.delete();
|
|
// }
|
|
// return outFilePath;
|
|
// }
|
|
//
|
|
//
|
|
//}
|