整体项目结构:
pom文件引入依赖:
<dependencies> <dependency> <groupId>org.bytedeco</groupId> <artifactId>javacv-platform</artifactId> <version>1.5.7</version> </dependency> </dependencies>
1.播放视频 video类:
import org.bytedeco.javacv.CanvasFrame;
import org.bytedeco.javacv.FFmpegFrameGrabber;
import org.bytedeco.javacv.Frame;
import org.bytedeco.javacv.OpenCVFrameConverter;
import javax.swing.*;
public class Video {
public static void main(String[] args) {
FFmpegFrameGrabber grabber = null;
try {
String file = "rtsp://admin:Zjlab@2022@10.0.108.100:554/Streaming/Channels/101";
grabber = FFmpegFrameGrabber.createDefault(file);
grabber.setOption("rtsp_transport", "tcp"); // 使用tcp的方式,不然会丢包很严重
grabber.setImageWidth(1280);
grabber.setImageHeight(720);
System.out.println("grabber start");
grabber.start();
//1.播放视频
CanvasFrame canvasFrame = new CanvasFrame("摄像机");
canvasFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
canvasFrame.setAlwaysOnTop(true);
OpenCVFrameConverter.ToMat converter = new OpenCVFrameConverter.ToMat();
while (true) {
Frame frame = grabber.grabImage();
canvasFrame.showImage(frame);
}
} catch (Exception e) {
System.out.println(e);
} finally {
}
}
}
效果展示:
2.转换hls流
import org.bytedeco.ffmpeg.global.avcodec;
import org.bytedeco.ffmpeg.global.avutil;
import org.bytedeco.javacv.*;
import java.io.IOException;
public class RtspTransform {
public static void main(String[] args) throws IOException {
String rtspUrl="rtsp://admin:Zjlab@2022@10.0.108.100:554/Streaming/Channels/101";
String hlsUrl="D:\\code\\temp\\hls\\baibing.m3u8";
RtspTransform.convertMediaToM3u8ByHttp(rtspUrl,
hlsUrl ,"5",20);
}
public static void convertMediaToM3u8ByHttp(String inputStream, String m3u8Url, String hls_time,int maxSize) throws IOException {
avutil.av_log_set_level(avutil.AV_LOG_INFO);
FFmpegLogCallback.set();
FFmpegFrameGrabber grabber = new FFmpegFrameGrabber(inputStream);
grabber.start();
FFmpegFrameRecorder recorder = new FFmpegFrameRecorder(m3u8Url, grabber.getImageWidth(), grabber.getImageHeight(), grabber.getAudioChannels());
recorder.setFormat("hls");
recorder.setOption("hls_time", hls_time);
recorder.setOption("hls_list_size", maxSize-1+"");
recorder.setOption("hls_flags", "delete_segments");
recorder.setOption("hls_delete_threshold", "1");
recorder.setOption("hls_segment_type", "mpegts");
// http属性
recorder.setOption("method", "POST");
recorder.setFrameRate(25);
recorder.setGopSize(2 * 25);
recorder.setVideoQuality(1.0);
recorder.setVideoBitrate(10 * 1024);
recorder.setVideoCodec(avcodec.AV_CODEC_ID_H264);
recorder.setAudioCodec(avcodec.AV_CODEC_ID_AAC);
recorder.start();
Frame frame;
while ((frame = grabber.grabImage()) != null) {
try {
recorder.record(frame);
} catch (FrameRecorder.Exception e) {
e.printStackTrace();
}
}
recorder.setTimestamp(grabber.getTimestamp());
recorder.close();
grabber.close();
}
}
效果展示
3.抽帧
import org.bytedeco.javacv.FFmpegFrameGrabber;
import javax.imageio.ImageIO;
import java.io.File;
public class FrameExtract {
public static void main(String[] args) {
FFmpegFrameGrabber grabber = null;
try {
String file = "rtsp://admin:Zjlab@2022@10.0.108.100:554/Streaming/Channels/101";
grabber = FFmpegFrameGrabber.createDefault(file);
grabber.setOption("rtsp_transport", "tcp"); // 使用tcp的方式,不然会丢包很严重
grabber.setImageWidth(1280);
grabber.setImageHeight(720);
System.out.println("grabber start");
grabber.start();
//2.帧截图
int i = 0;
while (i < 1000) {
File outPut = new File("images/" + i + ".jpeg");
org.bytedeco.javacv.Frame frame = grabber.grabImage();
if (frame != null) {
ImageIO.write(OpencvUtil.FrameToBufferedImage(frame), "jpeg", outPut);
System.out.println("图片已保存");
Thread.sleep(1000);
i++;
}
}
grabber.stop();
grabber.release();
} catch (Exception e) {
System.out.println(e);
} finally {
}
}
}
效果:
工具类代码:
import org.bytedeco.javacv.Java2DFrameConverter;
import java.awt.image.BufferedImage;
public class OpencvUtil {
public static BufferedImage FrameToBufferedImage(org.bytedeco.javacv.Frame frame) {
Java2DFrameConverter converter = new Java2DFrameConverter();
BufferedImage bufferedImage = converter.getBufferedImage(frame);
// bufferedImage=rotateClockwise90(bufferedImage);
return bufferedImage;
}
/**
* 处理图片,将图片旋转90度。
*/
public static BufferedImage rotateClockwise90(BufferedImage bi) {
int width = bi.getWidth();
int height = bi.getHeight();
BufferedImage bufferedImage = new BufferedImage(height, width, bi.getType());
for (int i = 0; i < width; i++)
for (int j = 0; j < height; j++)
bufferedImage.setRGB(j, i, bi.getRGB(i, j));
return bufferedImage;
}
}