视频工具类
import org.bytedeco.ffmpeg.avcodec.AVCodecParameters;
import org.bytedeco.ffmpeg.avformat.AVFormatContext;
import org.bytedeco.ffmpeg.avformat.AVStream;
import org.bytedeco.ffmpeg.global.avcodec;
import org.bytedeco.javacv.FFmpegFrameGrabber;
import org.bytedeco.javacv.FFmpegFrameRecorder;
import org.bytedeco.javacv.FFmpegLogCallback;
import org.bytedeco.javacv.Frame;
import org.bytedeco.javacv.FrameGrabber;
public class VideoUtil
{
public static void videoConvert(String inputfile, String outputfile) throws Exception
{
if (outputfile.lastIndexOf('.') < 0)
{
throw new Exception("Error! Output file format undetected!");
}
String format = outputfile.substring(outputfile.lastIndexOf('.'));
FFmpegLogCallback.set();
Frame frame;
FFmpegFrameGrabber grabber = new FFmpegFrameGrabber(inputfile);
FFmpegFrameRecorder recorder = null;
try
{
System.out.println("开始初始化帧抓取器");
grabber.start(true);
System.out.println("帧抓取器初始化完成");
AVFormatContext avformatcontext = grabber.getFormatContext();
int streamNum = avformatcontext.nb_streams();
if (streamNum < 1)
{
System.out.println("文件内不存在媒体流");
throw new Exception("Error! There is no media stream in the file!");
}
double framerate = grabber.getVideoFrameRate();
System.out.printf("视频帧率[%f],视频时长[%d]秒,媒体流数量[%d]\r\n", framerate, avformatcontext.duration() / 1000000,
avformatcontext.nb_streams());
for (int i = 0; i < streamNum; i++)
{
AVStream avstream = avformatcontext.streams(i);
AVCodecParameters avcodecparameters = avstream.codecpar();
System.out.printf("流的索引[%d],编码器类型[%d],编码器ID[%d]\r\n", i, avcodecparameters.codec_type(),
avcodecparameters.codec_id());
}
int frameWidth = grabber.getImageWidth();
int frameHeight = grabber.getImageHeight();
int audiochannels = grabber.getAudioChannels();
System.out.printf("视频宽度[%d],视频高度[%d],音频通道数[%d]\r\n", frameWidth, frameHeight, audiochannels);
recorder = new FFmpegFrameRecorder(outputfile, frameWidth, frameHeight, audiochannels);
recorder.setVideoCodec(avcodec.AV_CODEC_ID_H2);
recorder.setFormat(format);
recorder.setVideoBitrate(grabber.getVideoBitrate());
recorder.setFrameRate(framerate);
recorder.setGopSize((int)framerate);
recorder.setAudioChannels(grabber.getAudioChannels());
recorder.start();
int videoframenum = 0;
int audioframenum = 0;
int dataframenum = 0;
while (null != (frame = grabber.grab()))
{
if (null != frame.image)
{
videoframenum++;
recorder.record(frame);
}
if (null != frame.samples)
{
audioframenum++;
recorder.record(frame);
}
if (null != frame.data)
{
dataframenum++;
}
}
System.out.printf("转码完成,视频帧[%d],音频帧[%d],数据帧[%d]\r\n", videoframenum, audioframenum, dataframenum);
} catch (Exception e)
{
throw e;
} finally
{
if (recorder != null)
{
try
{
recorder.close();
} catch (Exception e)
{
throw e;
}
}
try
{
grabber.close();
} catch (FrameGrabber.Exception e)
{
throw e;
}
}
}
}
测试类
public class App
{
public static void main(String[] args)
{
try
{
VideoUtil.videoConvert("/home/alderaan/test.MOV", "/result.mp4");
} catch (java.lang.Exception e)
{
e.printStackTrace();
}
System.out.println("执行完毕!");
}
}
POM
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>javacpp</artifactId>
<version>1.5.7</version>
</dependency>
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>opencv</artifactId>
<version>4.5.5-1.5.7</version>
<classifier>linux-x86_-gpu</classifier>
</dependency>
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>openblas</artifactId>
<version>0.3.19-1.5.7</version>
<classifier>linux-x86_</classifier>
</dependency>
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>ffmpeg</artifactId>
<version>5.0-1.5.7</version>
<classifier>linux-x86_-gpl</classifier>
</dependency>
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>opencv</artifactId>
<version>4.5.5-1.5.7</version>
<classifier>windows-x86_</classifier>
</dependency>
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>openblas</artifactId>
<version>0.3.19-1.5.7</version>
<classifier>windows-x86_</classifier>
</dependency>
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>ffmpeg</artifactId>
<version>5.0-1.5.7</version>
<classifier>windows-x86_</classifier>
</dependency>