依赖
xml
<dependency>
<groupId>org.bytedeco</groupId>
<artifactId>javacv</artifactId>
<version>1.4.3</version>
</dependency>
完整代码
java
public static void main(String[] args) {
String fileURL = "rtsp://admin:zr123456@192.168.1.199/Streaming/tracks/0101/?starttime=20240620T010203Z&endtime=20240620T010213Z";
String outFile = "C:\\Users\\admin\\Desktop\\a.flv";
FFmpegFrameRecorder recorder = null;
FFmpegFrameGrabber grabber = null;
try{
grabber = FFmpegFrameGrabber.createDefault(fileURL);
grabber.setOption("rtsp_transport", "udp");
grabber.start();
int imageWidth = grabber.getImageWidth();
int imageHeight = grabber.getImageHeight();
recorder = new FFmpegFrameRecorder(outFile, imageWidth, imageHeight);
recorder.setSampleRate(grabber.getSampleRate());
recorder.setAudioChannels(grabber.getAudioChannels());
recorder.setInterleaved(true);
recorder.setVideoBitrate(2500000);
recorder.setVideoCodec(avcodec.AV_CODEC_ID_H264);
recorder.setFormat("flv");
recorder.setFrameRate(grabber.getFrameRate());
recorder.setGopSize((int) (grabber.getFrameRate() * 2));
recorder.setPixelFormat(avutil.AV_PIX_FMT_YUV420P);
recorder.start();
// 处理视频帧
Frame frame;
while (true) {
frame = grabber.grabFrame();
if (frame == null) {
break; // 没有更多帧
}
recorder.record(frame);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// 确保资源被释放
if (recorder != null) {
recorder.stop();
}
}
}