阅读:11176回复:0
使用javaCV录制rtsp视频流
jar包依赖:
/* * * 按帧录制视频 * * @param inputFile-该地址可以是网络直播/录播地址,也可以是远程/本地文件路径 * @param outputFile-该地址只能是文件地址,如果使用该方法推送流媒体服务器会报错,原因是没有设置编码格式 * @throws org.bytedeco.javacv.FrameRecorder.Exception */ public void frameRecord(String inputFile, String outputFile, int audioChannel) throws Exception { // 获取视频源 FFmpegFrameGrabber grabber = new FFmpegFrameGrabber(inputFile); grabber.setOption("rtsp_transport", "tcp"); grabber.setFrameRate(25); grabber.setVideoBitrate(300000); // 流媒体输出地址,分辨率(长,高),是否录制音频(0:不录制/1:录制) int width = 640,height = 480; FFmpegFrameRecorder recorder = new FFmpegFrameRecorder(outputFile, width, height, audioChannel); recorder.setFrameRate(25); recorder.setInterleaved(true); recorder.setVideoCodec(avcodec.AV_CODEC_ID_H264); recorder.setFormat("mp4"); recorder.setPixelFormat(avutil.AV_PIX_FMT_YUV420P); recorder.setVideoBitrate(300000); recordByFrame(grabber, recorder, true); } private static void recordByFrame(FFmpegFrameGrabber grabber, FFmpegFrameRecorder recorder, Boolean status) throws Exception { try {//建议在线程中使用该方法 grabber.start(); recorder.start(); Frame frame = null; long beginTime = System.currentTimeMillis(); while (status && (frame = grabber.grabFrame()) != null) { recorder.record(frame); long nowTime = System.currentTimeMillis(); //此处为视频时长,根据需求设定自己的值 if (nowTime - beginTime >= 60*1000) break; } recorder.stop(); grabber.stop(); } finally { if (grabber != null) { grabber.stop(); } } } |
|