SpringBoot使用JavaCV处理rtsp流(摄像头画面监控等)
为什么写这篇文章?
起因是项目中会用到rtsp流,用于分析视频,和语音口令,还有就是控制摄像头和做监控时候做云端存储功能。在网上找了一大圈发现这方面资料很少,资料中大部分人还是FFmpeg的安装程序加代码的方式实现。
(图片来源网络,侵删)
其实JAVACV和OPENCV是非常强大的。当然我了解的也不多(因为源码没有什么注释!!理解起来很困难)。这篇文章最主要是针对于目前GPT和通义前问中提供的代码进行纠错和改正(特别是从rtsp中只取出音频)。
部分代码是他人提供,所以看起来会有差异,不过我会进行说明。
pom
org.bytedeco javacv-platform 1.5.9
取出视频保存为mp4代码
/** * 拉取指定时长的视频-保存为mp4 * * @param rtspUrl 流地址 * @param duration 时长(秒)(TimeUnit Seconds) * @param outputFile 输出位置 */ public static void videoPuller(String rtspUrl, int duration, String outputFile) { // 创建抓取器 FFmpegFrameGrabber grabber = new FFmpegFrameGrabber(rtspUrl); try { grabber.start(); // 创建录制器-设置的文件保存位置以及视频画面的宽度和高度 FFmpegFrameRecorder recorder = new FFmpegFrameRecorder(outputFile, grabber.getImageWidth(), grabber.getImageHeight()); recorder.setVideoCodec(avcodec.AV_CODEC_ID_H264); // 设置视频编解码器 recorder.setFormat("mp4"); // 设置视频输出格式 // 设置音频相关参数 recorder.setAudioCodec(avcodec.AV_CODEC_ID_AAC); //这里直接写2以及后续写固定值也可以,当然我们可以从grabber对象中get出来 //grabber.getAudioChannels(); // recorder.setAudioChannels(grabber.getAudioChannels()); recorder.setAudioChannels(2); //grabber.getSampleRate(); recorder.setSampleRate(44100); //grabber.getAudioBitrate(); recorder.setAudioBitrate(192000); recorder.start(); Frame frame; long startTime = System.currentTimeMillis(); long endTime = startTime + (duration * 1000); while ((frame = grabber.grabFrame()) != null && System.currentTimeMillis() recorder.record(frame); } recorder.stop(); grabber.stop(); recorder.close(); grabber.close(); } catch (FrameGrabber.Exception | FrameRecorder.Exception e) { e.printStackTrace(); } } public static void main(String[] args) throws IOException { videoPuller("rtsp流地址",60, "D:\\va\\a.mp4"); } public static void extractAudioFromRtsp(String rtspUrl, String outputPath, int maxDurationSeconds) throws Exception { FFmpegFrameGrabber grabber = new FFmpegFrameGrabber(rtspUrl); grabber.start(); //1.从视频流中取出读取到的参数 int audioChannels = grabber.getAudioChannels();//音频通道 int sampleRate = grabber.getSampleRate(); int audioBitrate = grabber.getAudioBitrate(); //创建recorder对象,设置编码器和解码器 //这里也是所有AI都回答错误的地方 //通义和GPT会回答为这样 //FFmpegFrameRecorder recorder = new FFmpegFrameRecorder(outputPath, audioChannels, sampleRate); //上面这种错误传参方式导致,audioChannels变为为宽,sampleRate为高 FFmpegFrameRecorder recorder = new FFmpegFrameRecorder(outputPath, audioChannels); recorder.setSampleRate(sampleRate); recorder.setAudioBitrate(audioBitrate); recorder.setAudioCodec(avcodec.AV_CODEC_ID_PCM_S16LE); // 使用PCM 16位小端模式编码 recorder.start(); long startTime = System.currentTimeMillis(); Frame frame; while ((frame = grabber.grabSamples()) != null && (System.currentTimeMillis() - startTime) / 1000 extractAudioFromRtsp(DEFAULT_RTSP_URL, "D:\\va\\audio.wav", 60), executor); CompletableFuture.allOf(future1, future2, future3).thenRun(() -> System.out.println("所有任务都已完成")); // 在应用结束前关闭线程池 CompletableFuture.supplyAsync(() -> { return true; }, executor) .thenAccept((v) -> executor.shutdown()); } }
文章版权声明:除非注明,否则均为主机测评原创文章,转载或复制请以超链接形式并注明出处。