ffmpeg录制桌面视频和麦克风音频(音视频同步)

vs版本:2017
ffmpeg版本号:
ffmpeg version N-102642-g864d1ef2fc Copyright © 2000-2021 the FFmpeg developers
built with gcc 8.1.0 (x86_64-win32-seh-rev0, Built by MinGW-W64 project)
configuration: --arch=x86_64 --prefix=/home/ffmpeg_static_x64 --disable-debug
libavutil 57. 0.100 / 57. 0.100
libavcodec 59. 1.100 / 59. 1.100
libavformat 59. 2.101 / 59. 2.101
libavdevice 59. 0.100 / 59. 0.100
libavfilter 8. 0.101 / 8. 0.101
libswscale 6. 0.100 / 6. 0.100
libswresample 4. 0.100 / 4. 0.100

关于ffmpeg的lib和dll,本人在csdn上上传了相关资源,并且免费下载。

本人之前写过ffmpeg录制麦克风声音ffmpeg录制桌面(队列方式)

现在将两者合入到一起,实现音视频同步,完整代码下面有给出,这里不做赘述,关于代码的理解,读者可以参看我前两篇博客。

半年前,我在写音视频同步的时候,参考过其他人的博客,其中最重要的是ffmpeg实现录屏+录音
,结果短时期未能成功,继而做其他事情去了。

如今半年过去,当我重新开始学ffmpeg时,我首先参考的是ffmpeg本身的demo,位于doc/examples文件夹下面,其中有个muxing.c,这个是关于音视频混合的例子,比较简单,容易懂,而半年前看别人的博客,对应的ffmpeg的版本比较旧,旧版本的ffmpeg里面有很多不建议的变量和方法,称之为deprecated,学习复杂度会高。新版本将deprecated的函数和变量去掉了,减少了学习的复杂性。

将新版本ffmpeg里面的demo看完后,然后再看ffmpeg实现录屏+录音,就会觉得帮助不小,多说一句,对于视频和音频的平面(planar)和打包(packed)模式,建议大家先了解下,对了解音视频至关重要。

// FfmpegAudioTest.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <Windows.h>
#include <conio.h>

#ifdef	__cplusplus
extern "C"
{
#endif
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#include "libswscale/swscale.h"
#include "libswresample/swresample.h"
#include "libavdevice/avdevice.h"
#include "libavutil/audio_fifo.h"
#include "libavutil/imgutils.h"

#pragma comment(lib, "avcodec.lib")
#pragma comment(lib, "avformat.lib")
#pragma comment(lib, "avutil.lib")
#pragma comment(lib, "avdevice.lib")
#pragma comment(lib, "avfilter.lib")

	//#pragma comment(lib, "avfilter.lib")
	//#pragma comment(lib, "postproc.lib")
#pragma comment(lib, "swresample.lib")
#pragma comment(lib, "swscale.lib")
#ifdef __cplusplus
};
#endif




AVFormatContext	*pFormatCtx_Audio = NULL, *pFormatCtx_Out = NULL;
AVCodecContext *pReadCodecContext_Audio = NULL;

AVFormatContext	*pFormatCtx_Video = NULL;
AVCodecContext	*pReadCodecCtx_Video = NULL;
AVCodec			*pReadCodec_Video = NULL;

int VideoIndex = 0;
int AudioIndex_mic = 0;

AVCodecContext	*pCodecEncodeCtx_Audio = NULL;
AVCodec			*pCodecEncode_Audio = NULL;

AVCodecContext	*pCodecEncodeCtx_Video = NULL;
AVCodec			*pCodecEncode_Video = NULL;


AVFifoBuffer	*fifo_video = NULL;
AVAudioFifo		*fifo_audio_mic = NULL;

SwrContext *audio_convert_ctx = NULL;
SwsContext *img_convert_ctx = NULL;
int frame_size = 0;

uint8_t *picture_buf = NULL, *frame_buf = NULL;

int iPicCount = 0;

int64_t cur_pts_v = 0;
int64_t cur_pts_a = 0;


CRITICAL_SECTION VideoSection;
CRITICAL_SECTION AudioSection;

DWORD WINAPI AudioMicCapThreadProc(LPVOID lpParam);
DWORD WINAPI ScreenCapThreadProc(LPVOID lpParam);

static char *dup_wchar_to_utf8(const wchar_t *w)
{
	char *s = NULL;
	int l = WideCharToMultiByte(CP_UTF8, 0, w, -1, 0, 0, 0, 0);
	s = (char *)av_malloc(l);
	if (s)
		WideCharToMultiByte(CP_UTF8, 0, w, -1, s, l, 0, 0);
	return s;
}


/* just pick the highest supported samplerate */
static int select_sample_rate(const AVCodec *codec)
{
	const int *p;
	int best_samplerate = 0;

	if (!codec->supported_samplerates)
		return 44100;

	p = codec->supported_samplerates;
	while (*p) {
		if (!best_samplerate || abs(44100 - *p) < abs(44100 - best_samplerate))
			best_samplerate = *p;
		p++;
	}
	return best_samplerate;
}




/* select layout with the highest channel count */
static int select_channel_layout(const AVCodec *codec)
{
	const uint64_t *p;
	uint64_t best_ch_layout = 0;
	int best_nb_channels = 0;

	if (!codec->channel_layouts)
		return AV_CH_LAYOUT_STEREO;

	p = codec->channel_layouts;
	while (*p) {
		int nb_channels = av_get_channel_layout_nb_channels(*p);

		if (nb_channels > best_nb_channels) {
			best_ch_layout = *p;
			best_nb_channels = nb_channels;
		}
		p++;
	}
	return best_ch_layout;
}


int OpenVideoCapture()
{
	const AVInputFormat *ifmt = av_find_input_format("gdigrab");
	//这里可以加参数打开,例如可以指定采集帧率
	AVDictionary *options = NULL;
	av_dict_set(&options, "framerate", "25", NULL);
	av_dict_set(&options, "probesize", "50000000", NULL);
	//av_dict_set(&options,"offset_x","20",0);
	//The distance from the top edge of the screen or desktop
	//av_dict_set(&options,"offset_y","40",0);
	//Video frame size. The default is to capture the full screen
	//av_dict_set(&options,"video_size","320x240",0);
	if (avformat_open_input(&pFormatCtx_Video, "desktop", ifmt, &options) != 0)
	{
		printf("Couldn't open input stream.(无法打开视频输入流)\n");
		return -1;
	}
	if (avformat_find_stream_info(pFormatCtx_Video, NULL) < 0)
	{
		printf("Couldn't find stream information.(无法获取视频流信息)\n");
		return -1;
	}
	if (pFormatCtx_Video->streams[0]->codecpar->codec_type != AVMEDIA_TYPE_VIDEO)
	{
		printf("Couldn't find video stream information.(无法获取视频流信息)\n");
		return -1;
	}
	pReadCodec_Video = (AVCodec *)avcodec_find_decoder(pFormatCtx_Video->streams[0]->codecpar->codec_id);

	pReadCodecCtx_Video = avcodec_alloc_context3(pReadCodec_Video);

	if (pReadCodec_Video == NULL)
	{
		printf("Codec not found.(没有找到解码器)\n");
		return -1;
	}
	if (avcodec_open2(pReadCodecCtx_Video, pReadCodec_Video, NULL) < 0)
	{
		printf("Could not open codec.(无法打开解码器)\n");
		return -1;
	}

	/* put sample parameters */
	pReadCodecCtx_Video->bit_rate = 400000;
	/* resolution must be a multiple of two */
	pReadCodecCtx_Video->width = 1920;
	pReadCodecCtx_Video->height = 1080;
	/* frames per second */
	AVRational timeBase;
	timeBase.num = 1;
	timeBase.den = 25;
	pReadCodecCtx_Video->time_base = timeBase;

	AVRational frameRate;
	frameRate.den = 1;
	frameRate.num = 25;
	pReadCodecCtx_Video->framerate = frameRate;

	/* emit one intra frame every ten frames
	 * check frame pict_type before passing frame
	 * to encoder, if frame->pict_type is AV_PICTURE_TYPE_I
	 * then gop_size is ignored and the output of encoder
	 * will always be I frame irrespective to gop_size
	 */
	pReadCodecCtx_Video->gop_size = 25;
	pReadCodecCtx_Video->max_b_frames = 1;
	pReadCodecCtx_Video->pix_fmt = AV_PIX_FMT_YUV420P;


	img_convert_ctx = sws_getContext(pReadCodecCtx_Video->width, pReadCodecCtx_Video->height, (AVPixelFormat)pFormatCtx_Video->streams[0]->codecpar->format,
		pReadCodecCtx_Video->width, pReadCodecCtx_Video->height, AV_PIX_FMT_YUV420P, SWS_BICUBIC, NULL, NULL, NULL);

	frame_size = av_image_get_buffer_size(pReadCodecCtx_Video->pix_fmt, pReadCodecCtx_Video->width, pReadCodecCtx_Video->height, 1);

	//申请30帧缓存
	fifo_video = av_fifo_alloc(30 * av_image_get_buffer_size(AV_PIX_FMT_YUV420P, pReadCodecCtx_Video->width, pReadCodecCtx_Video->height, 1));

	return 0;
}




int OpenAudioCapture()
{
	//查找输入方式
	const AVInputFormat *pAudioInputFmt = av_find_input_format("dshow");

	//以Direct Show的方式打开设备,并将 输入方式 关联到格式上下文
	const char * psDevName = dup_wchar_to_utf8(L"audio=麦克风 (2- Synaptics HD Audio)");

	if (avformat_open_input(&pFormatCtx_Audio, psDevName, pAudioInputFmt, NULL) < 0)
	{
		printf("Couldn't open input stream.(无法打开音频输入流)\n");
		return -1;
	}

	if (pFormatCtx_Audio->streams[0]->codecpar->codec_type != AVMEDIA_TYPE_AUDIO)
	{
		printf("Couldn't find video stream information.(无法获取音频流信息)\n");
		return -1;
	}


	const AVCodec *tmpCodec = avcodec_find_decoder(pFormatCtx_Audio->streams[0]->codecpar->codec_id);

	pReadCodecContext_Audio = avcodec_alloc_context3(tmpCodec);

	pReadCodecContext_Audio->sample_rate = select_sample_rate(tmpCodec);
	pReadCodecContext_Audio->channel_layout = select_channel_layout(tmpCodec);
	pReadCodecContext_Audio->channels = av_get_channel_layout_nb_channels(pReadCodecContext_Audio->channel_layout);

	pReadCodecContext_Audio->sample_fmt = (AVSampleFormat)pFormatCtx_Audio->streams[0]->codecpar->format;

	if (0 > avcodec_open2(pReadCodecContext_Audio, tmpCodec, NULL))
	{
		printf("can not find or open audio decoder!\n");
	}


	audio_convert_ctx = swr_alloc();
	av_opt_set_channel_layout(audio_convert_ctx, "in_channel_layout", AV_CH_LAYOUT_STEREO, 0);
	av_opt_set_channel_layout(audio_convert_ctx, "out_channel_layout", AV_CH_LAYOUT_STEREO, 0);
	av_opt_set_int(audio_convert_ctx, "in_sample_rate", 44100, 0);
	av_opt_set_int(audio_convert_ctx, "out_sample_rate", 44100, 0);
	av_opt_set_sample_fmt(audio_convert_ctx, "in_sample_fmt", AV_SAMPLE_FMT_S16, 0);
	av_opt_set_sample_fmt(audio_convert_ctx, "out_sample_fmt", AV_SAMPLE_FMT_FLTP, 0);

	swr_init(audio_convert_ctx);


	return 0;
}


int OpenOutPut()
{
	AVStream *pVideoStream = NULL;
	AVStream *pAudioStream = NULL;
	const char *outFileName = "test.mp4";
	avformat_alloc_output_context2(&pFormatCtx_Out, NULL, NULL, outFileName);


	if (pFormatCtx_Video->streams[0]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
	{
		VideoIndex = 0;
		pVideoStream = avformat_new_stream(pFormatCtx_Out, NULL);

		if (!pVideoStream)
		{
			printf("can not new stream for output!\n");
			return -1;
		}

		AVRational timeBase;
		timeBase.num = 1;
		timeBase.den = 50;
		pVideoStream->time_base = timeBase;


		pCodecEncode_Video = (AVCodec *)avcodec_find_encoder(pFormatCtx_Out->oformat->video_codec);
		if (!(pCodecEncode_Video)) {
			fprintf(stderr, "Could not find encoder for '%s'\n",
				avcodec_get_name(AV_CODEC_ID_MPEG4));
			exit(1);
		}

		pCodecEncodeCtx_Video = avcodec_alloc_context3(pCodecEncode_Video);
		if (!pCodecEncodeCtx_Video) {
			fprintf(stderr, "Could not alloc an encoding context\n");
			exit(1);
		}

		pCodecEncodeCtx_Video->time_base = timeBase;
		pCodecEncodeCtx_Video->codec_id = pFormatCtx_Out->oformat->video_codec;

		pCodecEncodeCtx_Video->bit_rate = 400000;
		/* Resolution must be a multiple of two. */
		//pCodecEncodeCtx_Video->width = 352;
		//pCodecEncodeCtx_Video->height = 288;
		pCodecEncodeCtx_Video->width = 1920;
		pCodecEncodeCtx_Video->height = 1080;
		/* timebase: This is the fundamental unit of time (in seconds) in terms
		 * of which frame timestamps are represented. For fixed-fps content,
		 * timebase should be 1/framerate and timestamp increments should be
		 * identical to 1. */

		pCodecEncodeCtx_Video->gop_size = 25; /* emit one intra frame every twelve frames at most */
		pCodecEncodeCtx_Video->pix_fmt = AV_PIX_FMT_YUV420P;

		if ((avcodec_open2(pCodecEncodeCtx_Video, pCodecEncode_Video, NULL)) < 0)
		{
			printf("can not open the encoder\n");
			return -1;
		}
	}


	if (pFormatCtx_Audio->streams[0]->codecpar->codec_type == AVMEDIA_TYPE_AUDIO)
	{
		AVCodecContext *pOutputCodecCtx;
		pAudioStream = avformat_new_stream(pFormatCtx_Out, NULL);

		AudioIndex_mic = 1;

		pCodecEncode_Audio = (AVCodec *)avcodec_find_encoder(pFormatCtx_Out->oformat->audio_codec);

		pCodecEncodeCtx_Audio = avcodec_alloc_context3(pCodecEncode_Audio);
		if (!pCodecEncodeCtx_Audio) {
			fprintf(stderr, "Could not alloc an encoding context\n");
			exit(1);
		}


		//pCodecEncodeCtx_Audio->codec_id = pFormatCtx_Out->oformat->audio_codec;
		pCodecEncodeCtx_Audio->sample_fmt = pCodecEncode_Audio->sample_fmts ? pCodecEncode_Audio->sample_fmts[0] : AV_SAMPLE_FMT_FLTP;
		pCodecEncodeCtx_Audio->bit_rate = 64000;
		pCodecEncodeCtx_Audio->sample_rate = 44100;
		if (pCodecEncode_Audio->supported_samplerates) {
			pCodecEncodeCtx_Audio->sample_rate = pCodecEncode_Audio->supported_samplerates[0];
			for (int i = 0; pCodecEncode_Audio->supported_samplerates[i]; i++) {
				if (pCodecEncode_Audio->supported_samplerates[i] == 44100)
					pCodecEncodeCtx_Audio->sample_rate = 44100;
			}
		}
		pCodecEncodeCtx_Audio->channels = av_get_channel_layout_nb_channels(pCodecEncodeCtx_Audio->channel_layout);
		pCodecEncodeCtx_Audio->channel_layout = AV_CH_LAYOUT_STEREO;
		if (pCodecEncode_Audio->channel_layouts) {
			pCodecEncodeCtx_Audio->channel_layout = pCodecEncode_Audio->channel_layouts[0];
			for (int i = 0; pCodecEncode_Audio->channel_layouts[i]; i++) {
				if (pCodecEncode_Audio->channel_layouts[i] == AV_CH_LAYOUT_STEREO)
					pCodecEncodeCtx_Audio->channel_layout = AV_CH_LAYOUT_STEREO;
			}
		}
		pCodecEncodeCtx_Audio->channels = av_get_channel_layout_nb_channels(pCodecEncodeCtx_Audio->channel_layout);


		AVRational timeBase;
		timeBase.den = pCodecEncodeCtx_Audio->sample_rate;
		timeBase.num = 1;
		pAudioStream->time_base = timeBase;

		if (avcodec_open2(pCodecEncodeCtx_Audio, pCodecEncode_Audio, 0) < 0)
		{
			//编码器打开失败,退出程序
			return -1;
		}
	}


	if (!(pFormatCtx_Out->oformat->flags & AVFMT_NOFILE))
	{
		if (avio_open(&pFormatCtx_Out->pb, outFileName, AVIO_FLAG_WRITE) < 0)
		{
			printf("can not open output file handle!\n");
			return -1;
		}
	}

	avcodec_parameters_from_context(pVideoStream->codecpar, pCodecEncodeCtx_Video);
	avcodec_parameters_from_context(pAudioStream->codecpar, pCodecEncodeCtx_Audio);

	if (avformat_write_header(pFormatCtx_Out, NULL) < 0)
	{
		printf("can not write the header of the output file!\n");
		return -1;
	}

	return 0;
}


int main(int argc, char* argv[])
{
	int ret = 0;

	AVSampleFormat sample_fmt = AV_SAMPLE_FMT_S16;
	int iSize = av_get_bytes_per_sample(sample_fmt);


	avdevice_register_all();

	if (OpenVideoCapture() < 0)
	{
		return -1;
	}

	if (OpenAudioCapture() < 0)
	{
		return -1;
	}

	if (OpenOutPut() < 0)
	{
		return -1;
	}

	InitializeCriticalSection(&VideoSection);
	InitializeCriticalSection(&AudioSection);

	AVFrame *pFrameYUVInMain = av_frame_alloc();
	uint8_t *out_buffer_yuv420 = (uint8_t *)av_malloc(frame_size);
	av_image_fill_arrays(pFrameYUVInMain->data, pFrameYUVInMain->linesize, out_buffer_yuv420, AV_PIX_FMT_YUV420P, pReadCodecCtx_Video->width, pReadCodecCtx_Video->height, 1);

	int AudioFrameIndex_mic = 0;
	AVPacket packet = { 0 };

	CreateThread(NULL, 0, ScreenCapThreadProc, 0, 0, NULL);
	CreateThread(NULL, 0, AudioMicCapThreadProc, 0, 0, NULL);

	while (iPicCount < 1000)
	{
		if (NULL == fifo_audio_mic)
		{
			continue;
		}
		if (av_compare_ts(cur_pts_v, pFormatCtx_Out->streams[VideoIndex]->time_base,
			cur_pts_a, pFormatCtx_Out->streams[AudioIndex_mic]->time_base) <= 0)
		{
			if (av_fifo_size(fifo_video) >= frame_size)
			{
				EnterCriticalSection(&VideoSection);
				av_fifo_generic_read(fifo_video, out_buffer_yuv420, frame_size, NULL);
				LeaveCriticalSection(&VideoSection);

				packet.pts = iPicCount;
				packet.dts = iPicCount;
				av_packet_rescale_ts(&packet, pReadCodecCtx_Video->time_base, pFormatCtx_Out->streams[0]->time_base);

				pFrameYUVInMain->width = pReadCodecCtx_Video->width;
				pFrameYUVInMain->height = pReadCodecCtx_Video->height;
				pFrameYUVInMain->format = AV_PIX_FMT_YUV420P;

				pFrameYUVInMain->pts = packet.pts;
				pFrameYUVInMain->pkt_dts = packet.pts;

				cur_pts_v = packet.pts;

				av_packet_unref(&packet);

				ret = avcodec_send_frame(pCodecEncodeCtx_Video, pFrameYUVInMain);

				ret = avcodec_receive_packet(pCodecEncodeCtx_Video, &packet);

				ret = av_interleaved_write_frame(pFormatCtx_Out, &packet);
				avio_flush(pFormatCtx_Out->pb);

				iPicCount++;
			}
		}
		else
		{
			if (av_audio_fifo_size(fifo_audio_mic) >=
				(pFormatCtx_Out->streams[AudioIndex_mic]->codecpar->frame_size > 0 ? pFormatCtx_Out->streams[AudioIndex_mic]->codecpar->frame_size : 1024))
			{
				AVFrame *frame_mic = NULL;
				frame_mic = av_frame_alloc();

				frame_mic->nb_samples = pFormatCtx_Out->streams[AudioIndex_mic]->codecpar->frame_size > 0 ? pFormatCtx_Out->streams[AudioIndex_mic]->codecpar->frame_size : 1024;
				frame_mic->channel_layout = pFormatCtx_Out->streams[AudioIndex_mic]->codecpar->channel_layout;
				frame_mic->format = pFormatCtx_Out->streams[AudioIndex_mic]->codecpar->format;
				frame_mic->sample_rate = pFormatCtx_Out->streams[AudioIndex_mic]->codecpar->sample_rate;
				av_frame_get_buffer(frame_mic, 0);

				EnterCriticalSection(&AudioSection);
				int readcount = av_audio_fifo_read(fifo_audio_mic, (void **)frame_mic->data,
					(pFormatCtx_Out->streams[AudioIndex_mic]->codecpar->frame_size > 0 ? pFormatCtx_Out->streams[AudioIndex_mic]->codecpar->frame_size : 1024));
				LeaveCriticalSection(&AudioSection);

				AVPacket pkt_out_mic = { 0 };

				int got_picture_mic = -1;
				pkt_out_mic.data = NULL;
				pkt_out_mic.size = 0;

				frame_mic->pts = AudioFrameIndex_mic * pFormatCtx_Out->streams[AudioIndex_mic]->codecpar->frame_size;


				AVFrame *frame_mic_encode = NULL;
				frame_mic_encode = av_frame_alloc();

				frame_mic_encode->nb_samples = pCodecEncodeCtx_Audio->frame_size;
				frame_mic_encode->channel_layout = pCodecEncodeCtx_Audio->channel_layout;
				frame_mic_encode->format = pCodecEncodeCtx_Audio->sample_fmt;
				frame_mic_encode->sample_rate = pCodecEncodeCtx_Audio->sample_rate;
				av_frame_get_buffer(frame_mic_encode, 0);



				int dst_nb_samples = av_rescale_rnd(swr_get_delay(audio_convert_ctx, frame_mic->sample_rate) + frame_mic->nb_samples, frame_mic->sample_rate, frame_mic->sample_rate, AVRounding(1));

				//uint8_t *audio_buf = NULL;
				uint8_t *audio_buf[2] = { 0 };
				audio_buf[0] = (uint8_t *)frame_mic_encode->data[0];
				audio_buf[1] = (uint8_t *)frame_mic_encode->data[1];

				int nb = swr_convert(audio_convert_ctx, audio_buf, dst_nb_samples, (const uint8_t**)frame_mic->data, frame_mic->nb_samples);

				ret = avcodec_send_frame(pCodecEncodeCtx_Audio, frame_mic_encode);

				ret = avcodec_receive_packet(pCodecEncodeCtx_Audio, &pkt_out_mic);
				if (ret == AVERROR(EAGAIN))
				{
					continue;
				}
				av_frame_free(&frame_mic);
				av_frame_free(&frame_mic_encode);
				{
					pkt_out_mic.stream_index = AudioIndex_mic;
					pkt_out_mic.pts = AudioFrameIndex_mic * pFormatCtx_Out->streams[AudioIndex_mic]->codecpar->frame_size;
					pkt_out_mic.dts = AudioFrameIndex_mic * pFormatCtx_Out->streams[AudioIndex_mic]->codecpar->frame_size;
					pkt_out_mic.duration = pFormatCtx_Out->streams[AudioIndex_mic]->codecpar->frame_size;

					cur_pts_a = pkt_out_mic.pts;

					int ret2 = av_interleaved_write_frame(pFormatCtx_Out, &pkt_out_mic);
					av_packet_unref(&pkt_out_mic);
				}
				AudioFrameIndex_mic++;
			}
		}
	}

	av_write_trailer(pFormatCtx_Out);

	avio_close(pFormatCtx_Out->pb);
	avformat_free_context(pFormatCtx_Out);

	if (pFormatCtx_Audio != NULL)
	{
		avformat_close_input(&pFormatCtx_Audio);
		pFormatCtx_Audio = NULL;
	}

	return 0;
}



DWORD WINAPI AudioMicCapThreadProc(LPVOID lpParam)
{
	AVFrame *pFrame;
	pFrame = av_frame_alloc();

	AVPacket packet = { 0 };
	int ret = 0;
	while (iPicCount < 1000)
	{
		av_packet_unref(&packet);
		if (av_read_frame(pFormatCtx_Audio, &packet) < 0)
		{
			continue;
		}

		ret = avcodec_send_packet(pReadCodecContext_Audio, &packet);
		if (ret >= 0)
		{
			ret = avcodec_receive_frame(pReadCodecContext_Audio, pFrame);
			if (ret == AVERROR(EAGAIN))
			{
				break;
			}
			else if (ret == AVERROR_EOF)
			{
				return 0;
			}
			else if (ret < 0) {
				fprintf(stderr, "Error during decoding\n");
				exit(1);
			}

			if (NULL == fifo_audio_mic)
			{
				fifo_audio_mic = av_audio_fifo_alloc((AVSampleFormat)pFormatCtx_Audio->streams[0]->codecpar->format,
					pFormatCtx_Audio->streams[0]->codecpar->channels, 30 * pFrame->nb_samples);
			}

			int buf_space = av_audio_fifo_space(fifo_audio_mic);
			if (av_audio_fifo_space(fifo_audio_mic) >= pFrame->nb_samples)
			{
				EnterCriticalSection(&AudioSection);
				ret = av_audio_fifo_write(fifo_audio_mic, (void **)pFrame->data, pFrame->nb_samples);
				LeaveCriticalSection(&AudioSection);
			}



			av_packet_unref(&packet);
		}

	}

	return 0;
}





DWORD WINAPI ScreenCapThreadProc(LPVOID lpParam)
{
	AVFrame *pFrame;
	pFrame = av_frame_alloc();

	AVFrame *pFrameYUV = av_frame_alloc();
	int frame_size = av_image_get_buffer_size(AV_PIX_FMT_YUV420P, pReadCodecCtx_Video->width, pReadCodecCtx_Video->height, 1);
	uint8_t *out_buffer_yuv420 = (uint8_t *)av_malloc(frame_size);
	av_image_fill_arrays(pFrameYUV->data, pFrameYUV->linesize, out_buffer_yuv420, AV_PIX_FMT_YUV420P, pReadCodecCtx_Video->width, pReadCodecCtx_Video->height, 1);

	int y_size = pReadCodecCtx_Video->width * pReadCodecCtx_Video->height;

	AVPacket packet = { 0 };
	int ret = 0;
	while (iPicCount < 1000)
	{
		av_packet_unref(&packet);
		if (av_read_frame(pFormatCtx_Video, &packet) < 0)
		{
			continue;
		}

		ret = avcodec_send_packet(pReadCodecCtx_Video, &packet);

		if (ret >= 0)
		{
			ret = avcodec_receive_frame(pReadCodecCtx_Video, pFrame);
			if (ret == AVERROR(EAGAIN))
			{
				continue;
			}
			else if (ret == AVERROR_EOF)
			{
				break;
			}
			else if (ret < 0) {
				fprintf(stderr, "Error during decoding\n");
				break;
			}


			int iScale = sws_scale(img_convert_ctx, (const uint8_t* const*)pFrame->data, pFrame->linesize, 0, pCodecEncodeCtx_Video->height, pFrameYUV->data, pFrameYUV->linesize);

			if (av_fifo_space(fifo_video) >= frame_size)
			{
				EnterCriticalSection(&VideoSection);
				av_fifo_generic_write(fifo_video, pFrameYUV->data[0], y_size, NULL);
				av_fifo_generic_write(fifo_video, pFrameYUV->data[1], y_size / 4, NULL);
				av_fifo_generic_write(fifo_video, pFrameYUV->data[2], y_size / 4, NULL);
				LeaveCriticalSection(&VideoSection);
			}

		}


		if (ret == AVERROR(EAGAIN))
		{
			continue;
		}
	}

	av_frame_free(&pFrame);
	av_frame_free(&pFrameYUV);
	return 0;
}




  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 8
    评论
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值