天天看點

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