org.bytedeco.javacpp.avformat.AVFormatContext Java Examples

The following examples show how to use org.bytedeco.javacpp.avformat.AVFormatContext. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: testRecorder.java    From easyCV with Apache License 2.0 6 votes vote down vote up
/**
 * 从输入音视频流获取音视频格式
 * 
 * @param avfc
 */
private void getInputParam(AVFormatContext avfc) {
	// get input video and audio stream indices from ifmt_ctx
	for (int idx = 0; idx < avfc.nb_streams(); idx++) {
		System.err.println("读取流参数:"+idx);
		AVStream stream = avfc.streams(idx);
		AVCodecParameters codecpar = stream.codecpar();
		if (codecpar.codec_type() == AVMEDIA_TYPE_VIDEO) {
			AVRational frame_rate = stream.r_frame_rate();
			if (frame_rate.num() != AV_NOPTS_VALUE && frame_rate.den() != 0) {
				this.frameRate = (frame_rate.num()) / (frame_rate.den());
			}
			this.videoindex=idx;
			this.in_videoCodecpar = codecpar;
			this.in_videoStream = stream;
		} else if (codecpar.codec_type() == AVMEDIA_TYPE_AUDIO) {
			this.in_audioCodecpar = codecpar;
			this.in_audioStream = stream;
			this.audioindex=idx;
		}
	}
}
 
Example #2
Source File: GrabberTemplate4.java    From easyCV with Apache License 2.0 5 votes vote down vote up
/**
 * 查找音频通道
 * @param pFormatCtx
 * @return
 */
protected int findAudioStreamIndex(AVFormatContext formatCtx) {
	int size=formatCtx.nb_streams();
	for (int i = 0; i < size; i++) {
		AVStream stream=formatCtx.streams(i);
		AVCodecParameters codec=stream.codecpar();
		int type=codec.codec_type();
		if (type == AVMEDIA_TYPE_AUDIO) {
			return i;
		}
	}
	return -1;
}
 
Example #3
Source File: GrabberTemplate4.java    From easyCV with Apache License 2.0 5 votes vote down vote up
/**
 * 打开视频流
 * @param url -url
 * @return
 * @throws FileNotOpenException
 */
protected AVFormatContext openInput(String url) throws FileNotOpenException{
	AVFormatContext pFormatCtx = new AVFormatContext(null);
	if(avformat_open_input(pFormatCtx, url, null, null)==0) {
		return pFormatCtx;
	}
	throw new FileNotOpenException("Didn't open video file");
}
 
Example #4
Source File: TestPusher.java    From easyCV with Apache License 2.0 5 votes vote down vote up
/**
 * 指定视频帧位置获取对应视频帧
 * @param pFormatCtx
 * @param videoStream
 * @return
 */
protected AVCodecContext findVideoStream(AVFormatContext pFormatCtx ,int videoStream)throws StreamNotFoundException {
	if(videoStream >=0) {
		// Get a pointer to the codec context for the video stream
		AVStream stream=pFormatCtx.streams(videoStream);
		AVCodecContext pCodecCtx = stream.codec();
		return pCodecCtx;
	}
	throw new StreamNotFoundException("Didn't open video file");
}
 
Example #5
Source File: TestPusher.java    From easyCV with Apache License 2.0 5 votes vote down vote up
/**
 * 获取第一帧视频位置
 * 
 * @param pFormatCtx
 * @return
 */
protected int findVideoStreamIndex(AVFormatContext pFormatCtx) {
	int i = 0, videoStream = -1;
	for (i = 0; i < pFormatCtx.nb_streams(); i++) {
		AVStream stream = pFormatCtx.streams(i);
		AVCodecContext codec = stream.codec();
		if (codec.codec_type() == AVMEDIA_TYPE_VIDEO) {
			videoStream = i;
			break;
		}
	}
	return videoStream;
}
 
Example #6
Source File: TestPusher.java    From easyCV with Apache License 2.0 5 votes vote down vote up
/**
 * 检索流信息
 * 
 * @param pFormatCtx
 * @return
 */
protected AVFormatContext findStreamInfo(AVFormatContext pFormatCtx) throws StreamInfoNotFoundException {
	// 解决rtsp默认udp丢帧导致检索时间过长问题
	AVDictionary options = new AVDictionary();
	av_dict_set(options, "rtsp_transport", "tcp", 0);
	// 解决rtmp检索时间过长问题
	// 限制最大读取缓存
	pFormatCtx.probesize(500 * 1024);// 设置500k能保证高清视频也能读取到关键帧
	// 限制avformat_find_stream_info最大持续时长,设置成3秒
	pFormatCtx.max_analyze_duration(3 * AV_TIME_BASE);
	if (avformat_find_stream_info(pFormatCtx, options) >= 0) {
		return pFormatCtx;
	}
	throw new StreamInfoNotFoundException("Didn't retrieve stream information");
}
 
Example #7
Source File: GrabberTemplate4.java    From easyCV with Apache License 2.0 5 votes vote down vote up
/**
 * 检索流信息(rtsp/rtmp检索时间过长问题解决)
 * @param pFormatCtx
 * @return
 */
protected AVFormatContext findStreamInfo(AVFormatContext formatCtx,AVDictionary options) throws StreamInfoNotFoundException{
	if (avformat_find_stream_info(formatCtx, options==null?(AVDictionary)null:options)>= 0) {
		return formatCtx;
	}
	throw new StreamInfoNotFoundException("Didn't retrieve stream information");
}
 
Example #8
Source File: GrabberTemplate4.java    From easyCV with Apache License 2.0 5 votes vote down vote up
/**
 * 查找视频通道
 * @param pFormatCtx
 * @return
 */
protected int findVideoStreamIndex(AVFormatContext formatCtx) {
	int size=formatCtx.nb_streams();
	for (int i = 0; i < size; i++) {
		AVStream stream=formatCtx.streams(i);
		AVCodecParameters codec=stream.codecpar();
		int type=codec.codec_type();
		if (type == AVMEDIA_TYPE_VIDEO) {
			return i;
		}
	}
	return -1;
}
 
Example #9
Source File: FFmpegRecorder.java    From easyCV with Apache License 2.0 5 votes vote down vote up
/**
 * 指定视频帧位置获取对应视频帧
 * @param pFormatCtx
 * @param videoStream
 * @return
 */
protected AVCodecContext findVideoStream(AVFormatContext pFormatCtx ,int videoStream)throws StreamNotFoundException {
	if(videoStream >=0) {
		// Get a pointer to the codec context for the video stream
		AVStream stream=pFormatCtx.streams(videoStream);
		AVCodecContext pCodecCtx = stream.codec();
		return pCodecCtx;
	}
	throw new StreamNotFoundException("Didn't open video file");
}
 
Example #10
Source File: FFmpegRecorder.java    From easyCV with Apache License 2.0 5 votes vote down vote up
/**
 * 获取第一帧视频位置
 * @param pFormatCtx
 * @return
 */
protected int findVideoStreamIndex(AVFormatContext pFormatCtx) {
	int i = 0;
	for (i = 0; i < pFormatCtx.nb_streams(); i++) {
		AVStream stream=pFormatCtx.streams(i);
		AVCodecContext codec=stream.codec();
		if (codec.codec_type() == AVMEDIA_TYPE_VIDEO) {
			return i;
		}
	}
	return -1;
}
 
Example #11
Source File: FFmpegRecorder.java    From easyCV with Apache License 2.0 5 votes vote down vote up
/**
 * 检索流信息(rtsp/rtmp检索时间过长问题解决)
 * @param pFormatCtx
 * @return
 */
protected AVFormatContext findStreamInfo(AVFormatContext pFormatCtx) throws StreamInfoNotFoundException{
	if (avformat_find_stream_info(pFormatCtx, (PointerPointer<?>)null)>= 0) {
		return pFormatCtx;
	}
	throw new StreamInfoNotFoundException("Didn't retrieve stream information");
}
 
Example #12
Source File: FFmpegRecorder.java    From easyCV with Apache License 2.0 5 votes vote down vote up
/**
 * 打开视频流
 * @param url -url
 * @return
 * @throws FileNotOpenException
 */
protected AVFormatContext openInput(String url) throws FileNotOpenException{
	AVFormatContext pFormatCtx = new AVFormatContext(null);
	if(avformat_open_input(pFormatCtx, url, null, null)==0) {
		return pFormatCtx;
	}
	throw new FileNotOpenException("Didn't open video file");
}
 
Example #13
Source File: GrabberTmplate.java    From easyCV with Apache License 2.0 5 votes vote down vote up
/**
 * 指定视频帧位置获取对应视频帧
 * @param pFormatCtx
 * @param videoStream
 * @return
 */
protected AVCodecContext findVideoStream(AVFormatContext pFormatCtx ,int videoStream)throws StreamNotFoundException {
	if(videoStream >=0) {
		// Get a pointer to the codec context for the video stream
		AVStream stream=pFormatCtx.streams(videoStream);
		AVCodecContext pCodecCtx = stream.codec();
		return pCodecCtx;
	}
	throw new StreamNotFoundException("Didn't open video file");
}
 
Example #14
Source File: GrabberTmplate.java    From easyCV with Apache License 2.0 5 votes vote down vote up
/**
 * 获取第一帧视频位置
 * @param pFormatCtx
 * @return
 */
protected int findVideoStreamIndex(AVFormatContext pFormatCtx) {
	int i = 0, videoStream = -1;
	for (i = 0; i < pFormatCtx.nb_streams(); i++) {
		AVStream stream=pFormatCtx.streams(i);
		AVCodecContext codec=stream.codec();
		if (codec.codec_type() == AVMEDIA_TYPE_VIDEO) {
			videoStream = i;
			break;
		}
	}
	return videoStream;
}
 
Example #15
Source File: GrabberTmplate.java    From easyCV with Apache License 2.0 5 votes vote down vote up
/**
 * 检索流信息
 * @param pFormatCtx
 * @return
 */
protected AVFormatContext findStreamInfo(AVFormatContext pFormatCtx) throws StreamInfoNotFoundException{
	if (avformat_find_stream_info(pFormatCtx, (PointerPointer<?>) null)>= 0) {
		return pFormatCtx;
	}
	throw new StreamInfoNotFoundException("Didn't retrieve stream information");
}
 
Example #16
Source File: GrabberTmplate.java    From easyCV with Apache License 2.0 5 votes vote down vote up
/**
 * 打开视频流
 * @param url -url
 * @return
 * @throws FileNotOpenException
 */
protected AVFormatContext openInput(String url) throws FileNotOpenException{
	AVFormatContext pFormatCtx = new AVFormatContext(null);
	if(avformat_open_input(pFormatCtx, url, null, null)==0) {
		return pFormatCtx;
	}
	throw new FileNotOpenException("Didn't open video file");
}
 
Example #17
Source File: testRecorder.java    From easyCV with Apache License 2.0 5 votes vote down vote up
/**
 * 检索流信息(rtsp/rtmp检索时间过长问题解决)
 * 
 * @param pFormatCtx
 * @return
 */
protected AVFormatContext findStreamInfo(AVFormatContext pFormatCtx) throws StreamInfoNotFoundException {
	if (avformat_find_stream_info(pFormatCtx, (PointerPointer<?>) null) >= 0) {
		return pFormatCtx;
	}
	throw new StreamInfoNotFoundException("Didn't retrieve stream information");
}
 
Example #18
Source File: GrabberTemplate4.java    From easyCV with Apache License 2.0 5 votes vote down vote up
/**
	 * 指定视频帧位置获取对应视频帧
	 * @param pFormatCtx
	 * @param videoStream
	 * @return
	 */
	protected AVCodecParameters findVideoParameters(AVFormatContext formatCtx ,int videoStreamIndex)throws StreamNotFoundException {
		if(videoStreamIndex >=0) {
			// Get a pointer to the codec context for the video stream
			AVStream stream=formatCtx.streams(videoStreamIndex);
//			AVCodecContext pCodecCtx = stream.codec();
			AVCodecParameters codecParam=stream.codecpar();
			return codecParam;
		}
		//if no stream,throws Excetion.
		throw new StreamNotFoundException("Didn't open video file");
	}
 
Example #19
Source File: GrabberTemplate4.java    From easyCV with Apache License 2.0 5 votes vote down vote up
/**
 * 查找并尝试打开解码器
 * @return 
 */
protected AVCodecContext findAndOpenCodec(AVFormatContext formatCtx ,int videoStreamIndex) {
	// Find codec param
	AVCodecParameters codecParameters=findVideoParameters(formatCtx ,videoStreamIndex);

	// Find the decoder for the video stream
	AVCodec codec = avcodec_find_decoder(codecParameters.codec_id());

	if (codec == null) {
		Console.err("Codec not found!");
		throw new CodecNotFoundExpception("Codec not found!");
	}
	AVDictionary optionsDict = null;
	AVCodecContext codecCtx = avcodec_alloc_context3(codec);

	//convert to codecContext
	if(avcodec_parameters_to_context( codecCtx, codecParameters)<0) {
		Console.err("Could not convert parameter to codecContext!");
		throw new CodecNotFoundExpception("Could not convert parameter to codecContext!"); // Could not open codec
	}

	// Open codec
	if (avcodec_open2(codecCtx, codec, optionsDict) < 0) {
		Console.err("Could not open codec!");
		throw new CodecNotFoundExpception("Could not open codec!"); // Could not open codec
	}
	
	return codecCtx;
}
 
Example #20
Source File: JavaCVRecord.java    From easyCV with Apache License 2.0 5 votes vote down vote up
/**
 * 输出视频到文件或者流服务
 * 
 * @param out
 *            -输出位置(支持流服务和文件)
 * @return
 * @throws IOException
 */
public Recorder to(String out) throws IOException {
	if (out == null) {
		throw new Exception("输出视频不能为空");
	}
	this.out = out;
	// 录制/推流器
	record = new FFmpegFrameRecorderPlus(out, width, height);
	record.setVideoOption("crf", "18");
	record.setGopSize(2);
	record.setFrameRate(framerate);
	record.setVideoBitrate(bitrate);

	record.setAudioChannels(audioChannels);
	record.setAudioBitrate(audioBitrate);
	record.setSampleRate(sampleRate);
	AVFormatContext fc = null;
	//rtmp和flv
	if (hasRTMPFLV(out)) {
		// 封装格式flv,并使用h264和aac编码
		record.setFormat("flv");
		record.setVideoCodec(AV_CODEC_ID_H264);
		record.setAudioCodec(AV_CODEC_ID_AAC);
	}else if(hasMP4(out)){//MP4
		record.setFormat("mp4");
		record.setVideoCodec(AV_CODEC_ID_H264);
		record.setAudioCodec(AV_CODEC_ID_AAC);
	}
	record.start(fc);
	return this;
}
 
Example #21
Source File: TestFFmpeg.java    From easyCV with Apache License 2.0 4 votes vote down vote up
/**
	 * 把YUVJ420P数据编码保存成jpg图片
	 * @param pFrame -YUVJ420P数据
	 * @param index -序号
	 * @return
	 */
	private static int saveImg(AVFrame pFrame, int index,String out_file) {
		int width= pFrame.width(), height= pFrame.height();
		// 分配AVFormatContext对象
		AVFormatContext pFormatCtx = avformat_alloc_context();
		// 设置输出文件格式
		pFormatCtx.oformat(av_guess_format("PNG", null, null));
		if (pFormatCtx.oformat() == null) {
			return -1;
		}
		// 创建并初始化一个和该url相关的AVIOContext
		AVIOContext pb = new AVIOContext();
		if (avio_open(pb, out_file, AVIO_FLAG_READ_WRITE) < 0) {
			System.err.println("Couldn't open output file.");
			return -1;
		}
		pFormatCtx.pb(pb);
		// 构建一个新stream
		AVCodec codec = null;
		AVStream pAVStream = avformat_new_stream(pFormatCtx, codec);
		if (pAVStream == null) {
			return -1;
		}
		// 设置该stream的信息
		AVCodecContext pCodecCtx = pAVStream.codec();
		pCodecCtx.codec_id(pFormatCtx.oformat().video_codec());
		pCodecCtx.codec_type(AVMEDIA_TYPE_VIDEO);
		pCodecCtx.pix_fmt(pFrame.format());
		pCodecCtx.width(width);
		pCodecCtx.height(height);
		pCodecCtx.time_base().num(1);
		pCodecCtx.time_base().den(25);
		// Begin Output some information
		av_dump_format(pFormatCtx, 0, out_file, 1);
		// End Output some information
		// 查找解码器
		AVCodec pCodec = avcodec_find_encoder(pCodecCtx.codec_id());
		if (pCodec == null) {
			System.err.println("Codec not found.");
			return -1;
		}
		// 设置pCodecCtx的解码器为pCodec
		if (avcodec_open2(pCodecCtx, pCodec, (PointerPointer) null) < 0) {
			System.err.println("Could not open codec.");
			return -1;
		}

		// Write Header
		avformat_write_header(pFormatCtx, (PointerPointer) null);

		int y_size = width * height;

		// 给AVPacket分配足够大的空间
		AVPacket pkt = new AVPacket();
		av_new_packet(pkt, y_size * 3);
		//
		int[] got_picture_arr = { 0 };
//		IntPointer got_picture = new IntPointer(got_picture_arr);
		int ret = avcodec_encode_video2(pCodecCtx, pkt, pFrame, got_picture_arr);
		if (ret < 0) {
			System.err.println("Encode Error.\n");
			return -1;
		}
		if (pkt != null && !pkt.isNull()) {
			// pkt.stream_index = pAVStream->index;
			ret = av_write_frame(pFormatCtx, pkt);
		}
		// Write Trailer
		if (av_write_trailer(pFormatCtx) >= 0) {
			System.err.println("Encode Successful.");
		}

		av_free_packet(pkt);

		if (pAVStream != null) {
			avcodec_close(pAVStream.codec());
		}

		if (pFormatCtx != null) {
			avio_close(pFormatCtx.pb());
			avformat_free_context(pFormatCtx);
		}

		return 0;
	}
 
Example #22
Source File: FFmpegRecorder.java    From easyCV with Apache License 2.0 4 votes vote down vote up
/**
	 * 抓取视频帧(默认跳过音频帧和空帧)
	 * @param url
	 * @param fmt - 像素格式,比如AV_PIX_FMT_BGR24
	 * @return
	 * @throws IOException
	 */
	public ByteBuffer grabVideoFrame(String url,int fmt) throws IOException {
		
		// Open video file
		AVFormatContext pFormatCtx=openInput(url);
//		if(url.indexOf("rtmp")>=0) {
			//解决rtmp检索时间过长问题
		    //限制最大读取缓存
		    pFormatCtx.probesize(PROBESIZE);//设置500k能保证高清视频也能读取到关键帧
		  //限制avformat_find_stream_info最大持续时长,设置成3秒
		    pFormatCtx.max_analyze_duration(MAX_ANALYZE_DURATION);
//		}
		// Retrieve stream information
		pFormatCtx=findStreamInfo(pFormatCtx);
		// Dump information about file onto standard error
		//av_dump_format(pFormatCtx, 0, url, 0);

		//Find a video stream
		int videoStream=findVideoStreamIndex(pFormatCtx);
		AVCodecContext pCodecCtx =findVideoStream(pFormatCtx,videoStream);
		
		// Find the decoder for the video stream
		pCodecCtx= findAndOpenCodec(pCodecCtx);
		// Allocate video frame
		AVFrame pFrame = av_frame_alloc();
		AVPacket packet = new AVPacket();
		int[] frameFinished = new int[1];
		
		// Read frames and save first five frames to disk
		while (av_read_frame(pFormatCtx, packet) >= 0) {
			// Is this a packet from the video stream?
			if (packet.stream_index() == videoStream) {
				// Decode video frame
				avcodec_decode_video2(pCodecCtx, pFrame, frameFinished, packet);
				// Did we get a video frame?
				if (frameFinished != null&&frameFinished[0] != 0) {
					
				}
			}
			// Free the packet that was allocated by av_read_frame
			av_free_packet(packet);
		}
		//ge
		av_free(pFrame);// Free the YUV frame
		avcodec_close(pCodecCtx);// Close the codec
		avformat_close_input(pFormatCtx);// Close the video file
		return null;
	}
 
Example #23
Source File: GrabberTmplate.java    From easyCV with Apache License 2.0 4 votes vote down vote up
/**
	 * 抓取视频帧(默认跳过音频帧和空帧)
	 * @param url
	 * @param fmt - 像素格式,比如AV_PIX_FMT_BGR24
	 * @return
	 * @throws IOException
	 */
	public ByteBuffer grabVideoFrame(String url,int fmt) throws IOException {
		// Open video file
		AVFormatContext pFormatCtx=openInput(url);

		// Retrieve stream information
		pFormatCtx=findStreamInfo(pFormatCtx);

		// Dump information about file onto standard error
		//av_dump_format(pFormatCtx, 0, url, 0);

		//Find a video stream
		int videoStream=findVideoStreamIndex(pFormatCtx);
		AVCodecContext pCodecCtx =findVideoStream(pFormatCtx,videoStream);
		
		// Find the decoder for the video stream
		pCodecCtx= findAndOpenCodec(pCodecCtx);

		// Allocate video frame
		AVFrame pFrame = av_frame_alloc();
		//Allocate an AVFrame structure
		AVFrame pFrameRGB = av_frame_alloc();

		width = pCodecCtx.width();
		height = pCodecCtx.height();
		pFrameRGB.width(width);
		pFrameRGB.height(height);
		pFrameRGB.format(fmt);

		// Determine required buffer size and allocate buffer
		int numBytes = avpicture_get_size(fmt, width, height);

		SwsContext sws_ctx = sws_getContext(width, height, pCodecCtx.pix_fmt(), width, height,fmt, SWS_BILINEAR, null, null, (DoublePointer) null);

		BytePointer buffer = new BytePointer(av_malloc(numBytes));
		// Assign appropriate parts of buffer to image planes in pFrameRGB
		// Note that pFrameRGB is an AVFrame, but AVFrame is a superset
		// of AVPicture
		avpicture_fill(new AVPicture(pFrameRGB), buffer, fmt, width, height);
		AVPacket packet = new AVPacket();
		int[] frameFinished = new int[1];
		try {
			// Read frames and save first five frames to disk
			while (av_read_frame(pFormatCtx, packet) >= 0) {
				// Is this a packet from the video stream?
				if (packet.stream_index() == videoStream) {
					// Decode video frame
					avcodec_decode_video2(pCodecCtx, pFrame, frameFinished, packet);
					// Did we get a video frame?
					if (frameFinished != null&&frameFinished[0] > 0) {
						// Convert the image from its native format to BGR
						sws_scale(sws_ctx, pFrame.data(), pFrame.linesize(), 0, height, pFrameRGB.data(),pFrameRGB.linesize());
						//Convert BGR to ByteBuffer
						saveFrame(pFrameRGB, width, height);
					}
				}
				// Free the packet that was allocated by av_read_frame
				av_free_packet(packet);
			}
			return null;
		}finally {
			//Don't free buffer
//			av_free(buffer);
			av_free(pFrameRGB);// Free the RGB image
			av_free(pFrame);// Free the YUV frame
			sws_freeContext(sws_ctx);//Free SwsContext
			avcodec_close(pCodecCtx);// Close the codec
			avformat_close_input(pFormatCtx);// Close the video file
		}
	}
 
Example #24
Source File: FFmpegFrameRecorderPlus.java    From easyCV with Apache License 2.0 4 votes vote down vote up
public void start(AVFormatContext ifmt_ctx) throws Exception {
    this.ifmt_ctx = ifmt_ctx;
    start();
}
 
Example #25
Source File: JavaCVRecord.java    From easyCV with Apache License 2.0 4 votes vote down vote up
/**
 * 转发源视频到输出(复制)
 * 
 * @param src
 *            -源视频
 * @param out
 *            -输出流媒体服务地址
 * @return
 * @throws org.bytedeco.javacv.FrameRecorder.Exception
 */
public Recorder stream(String src, String out) throws IOException {
	if (src == null || out == null) {
		throw new Exception("源视频和输出为空");
	}
	this.src = src;
	this.out = out;
	// 采集/抓取器
	grabber = new FFmpegFrameGrabber(src);
	grabber.start();
	if (width < 0 || height < 0) {
		width = grabber.getImageWidth();
		height = grabber.getImageHeight();
	}
	// 视频参数
	int audiocodecid = grabber.getAudioCodec();
	int codecid = grabber.getVideoCodec();
	double framerate = grabber.getVideoFrameRate();// 帧率
	int bitrate = grabber.getVideoBitrate();// 比特率

	// 音频参数
	// 想要录制音频,这三个参数必须有:audioChannels > 0 && audioBitrate > 0 && sampleRate > 0
	int audioChannels = grabber.getAudioChannels();
	int audioBitrate = grabber.getAudioBitrate();
	int sampleRate = grabber.getSampleRate();

	// 录制/推流器
	record = new FFmpegFrameRecorderPlus(out, width, height);
	record.setVideoOption("crf", "18");
	record.setGopSize(1);

	record.setFrameRate(framerate);
	record.setVideoBitrate(bitrate);
	record.setAudioChannels(audioChannels);
	record.setAudioBitrate(audioBitrate);
	record.setSampleRate(sampleRate);
	AVFormatContext fc = null;
	//rtmp和flv
	if (hasRTMPFLV(out)) {
		// 封装格式flv,并使用h264和aac编码
		record.setFormat("flv");
		record.setVideoCodec(AV_CODEC_ID_H264);
		record.setAudioCodec(AV_CODEC_ID_AAC);
		if(hasRTMPFLV(src)) {
			fc = grabber.getFormatContext();
		}
	}else if(hasMP4(out)){//MP4
		record.setFormat("mp4");
		record.setVideoCodec(AV_CODEC_ID_H264);
		record.setAudioCodec(AV_CODEC_ID_AAC);
	}
	record.start(fc);
	return this;
}
 
Example #26
Source File: TestPusher.java    From easyCV with Apache License 2.0 3 votes vote down vote up
/**
 * 打开视频流
 * 
 * @param url
 *            -url
 * @return
 * @throws FileNotOpenException
 */
protected AVFormatContext openInput(String url) throws FileNotOpenException {
	AVFormatContext pFormatCtx = new AVFormatContext(null);
	if (avformat_open_input(pFormatCtx, url, null, null) == 0) {
		return pFormatCtx;
	}
	throw new FileNotOpenException("Didn't open video file");
}
 
Example #27
Source File: testRecorder.java    From easyCV with Apache License 2.0 3 votes vote down vote up
/**
 * 打开视频流
 * 
 * @param url
 *            -url
 * @return
 * @throws FileNotOpenException
 */
protected AVFormatContext openInput(String url) throws FileNotOpenException {
	AVFormatContext pFormatCtx = new AVFormatContext(null);
	if (avformat_open_input(pFormatCtx, url, null, null) == 0) {
		return pFormatCtx;
	}
	throw new FileNotOpenException("Didn't open video file");
}