javacv-ffmpeg(三)视频截取

说明

支持本地文件、rtmp、rtsp、http-flv、hls

一、代码示例

			boolean isStart = true;// 该变量建议设置为全局控制变量,用于控制录制结束
			FFmpegFrameGrabber ff = new FFmpegFrameGrabber(url);
			// 微秒 大概为设置时间的两倍 TimeoutOption代码在示例文章二
			grabber.setOption(TimeoutOption.RW_TIMEOUT.getKey(), timeout);
			// rtsp 默认udp 丢包 改为tcp
			grabber.setOption("rtsp_transport", "tcp");
			grabber.start();
			//好多视频熟悉可以获取后打印 示例几个
			log.info("ImageWidth:" + grabber.getImageWidth());
			log.info("ImageHeight:" + grabber.getImageHeight());
			log.info("AudioChannels:" + grabber.getAudioChannels());
			log.info("Format:" + grabber.getFormat());
			FFmpegFrameRecorder recorder = new FFmpegFrameRecorder(outputFile, imageWidth, imageHeight, grabber.getAudioChannels());
					recorder.setFrameRate(grabber.getFrameRate());
//					recorder.setAudioBitrate(grabber.getAudioBitrate());
//					recorder.setSampleRate(grabber.getSampleRate());
//					recorder.setGopSize(2);
					recorder.setFormat(Format);
					recorder.setAudioCodecName("aac");
					recorder.setVideoCodec(grabber.getVideoCodec());
			Frame f = null;
			//如果想截取规定时间段视频 请看系列文章二
			while (isStart) {
				f = grabber.grabFrame();
				recorder.record(f);
			}
			recorder.stop();
			recorder.release();
			grabber.stop();
			grabber.release();
			

二、视频格式

rtmp、rtsp、http-flv保存为MP4
hls保存为ts

你可能感兴趣的:(javacv)