ffmpeg实现实时录音并推流的功能

基本方法就是在原有的推流代码基础上(比如rtmp推流的代码),修改打开输入设备的代码改为如下类似的代码,调整一下源文件的变量等设置,就可以实现了:

	
//输入(Input)
// 	if ((ret = avformat_open_input(&ifmt_ctx, in_filename, 0, 0)) < 0) {
// 		printf( "Could not open input file.");
// 		goto end;
// 	}
// 	if ((ret = avformat_find_stream_info(ifmt_ctx, 0)) < 0) {
// 		printf( "Failed to retrieve input stream information");
// 		goto end;
// 	}
// 
// 	int videoindex=-1;
// 	for(i=0; i<ifmt_ctx->nb_streams; i++) 
// 		if(ifmt_ctx->streams[i]->codec->codec_type==AVMEDIA_TYPE_AUDIO){
// 			videoindex=i;
// 			break;
// 		}
// 
// 		av_dump_format(ifmt_ctx, 0, in_filename, 0);

	//查找输入方式
	pAudioInputFmt =av_find_input_format("dshow");
	//以Direct Show<pAudioInputFmt>的方式打开设备psDevName,并将 输入方式 关联到格式上下文pFmtCtx
	// 	函数的各个参数:
	// 	1、AVFormatContext **ps:指向用户提供的结构体,一般可以将这个参数定义指向空然后传递到函数中,这样avformat_open_input函数将会分配这个结构体的内存空间并初始化。
	// 	2、const char *filename:打开视频文件的文件名。
	// 	3、AVInputFormat *fmt:如果这个参数不为空,则指定固定的输入格式,否则自动检测输入格式;一般设为空即可。
	// 	4、AVDictionary **options:由AVFormatContext和demuxer-private options组成的字典结构,可设为空。
	char * psDevName = dup_wchar_to_utf8(L"audio=麦克风 (Realtek High Definition Au");
	assert(avformat_open_input(&pFmtCtx,psDevName,pAudioInputFmt,NULL) == 0);
	//该函数可以读取一部分视音频数据并且获得一些相关的信息
// avformat_find_stream_info()代码比较长,难以全部分析,在这里只能简单记录一下它的要点。
// 该函数主要用于给每个媒体流(音频/视频)的AVStream结构体赋值。
// 我们大致浏览一下这个函数的代码,会发现它其实已经实现了解码器的查找,解码器的打开,
// 视音频帧的读取,视音频帧的解码等工作。换句话说,该函数实际上已经“走通”的解码的整个流程。
// 下面看一下除了成员变量赋值之外,该函数的几个关键流程。
// 1.查找解码器:find_decoder()
// 2.打开解码器:avcodec_open2()
// 3.读取完整的一帧压缩编码的数据:read_frame_internal()
// 注:av_read_frame()内部实际上就是调用的read_frame_internal()。
// 4.解码一些压缩编码数据:try_decode_frame()
	AVDictionary* pOptions = NULL;
	pFmtCtx->probesize = 1 *1024;
	pFmtCtx->max_analyze_duration = 1 * AV_TIME_BASE;
	// Retrieve stream information
	if(avformat_find_stream_info(pFmtCtx,&pOptions)<0)
	{
		printf("Couldn't find stream information.\n");
		return -1;
	}
// 	if(avformat_find_stream_info(pFmtCtx,NULL)<0)  
// 		return -1; 
	for(int i=0; i<pFmtCtx->nb_streams; i++) 
	{
		if(pFmtCtx->streams[i]->codec->codec_type==AVMEDIA_TYPE_AUDIO)
		{
			iAudioIndex=i;
			//用于查找FFmpeg的解码器<音频>。
			AVCodec *tmpCodec = avcodec_find_decoder(pFmtCtx->streams[i]->codec->codec_id);
// 我们可以简单梳理一下avcodec_open2()所做的工作,如下所列:
// (1)为各种结构体分配内存(通过各种av_malloc()实现)。
// (2)将输入的AVDictionary形式的选项设置到AVCodecContext。
// (3)其他一些零零碎碎的检查,比如说检查编解码器是否处于“实验”阶段。
// (4)如果是编码器,检查输入参数是否符合编码器的要求
// (5)调用AVCodec的init()初始化具体的解码器。
			if(0 > avcodec_open2(pFmtCtx->streams[i]->codec, tmpCodec, NULL))//打开解码器<音频>//打开麦克风录音
			{
				printf("can not find or open decoder!\n");
			}
			break;
		}
	}
// * Print detailed information about the input or output format, such as
// * duration, bitrate, streams, container, programs, metadata, side data,
// * codec and time base.
// * @param ic        the context to analyze
// * @param index     index of the stream to dump information about
// * @param url       the URL to print, such as source or destination file
// * @param is_output Select whether the specified context is an input(0) or output(1)
av_dump_format(pFmtCtx, 0, NULL, 0);

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章