ffmpeg录制电脑内部声音(如内部歌曲声音)

本文介绍了如何使用FFmpeg 2017版本在Windows上通过DirectShow捕获音频,选择合适的采样率和通道布局,并进行音频转换,以实现实时录音并保存为MP4文件的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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上上传了相关资源,并且免费下载。

本文抓取的是电脑内部声音,需要先安装软件screen capture recorder。
软件的下载地址是
http://sourceforge.net/projects/screencapturer/files/
在安装软件之前,我们先看下电脑支持的设备列表,用的是如下命令
ffmpeg -list_devices true -f dshow -i dummy
在这里插入图片描述
安装了软件之后,我们再看下电脑支持的设备列表
在这里插入图片描述
下面是具体的代码

// 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"

#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 = NULL;

int AudioIndex_mic;

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


AVAudioFifo		*fifo_audio = NULL;

SwrContext *audio_convert_ctx = NULL;

uint8_t *picture_buf = NULL, *frame_buf = NULL;

bool bCap = true;

int AudioFrameIndex = 0;


DWORD WINAPI AudioMicCapThreadProc(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 OpenAudioCapture()
{
	//查找输入方式
	const AVInputFormat *pAudioInputFmt = av_find_input_format("dshow");

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

	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 = avcodec_alloc_context3(tmpCodec);

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

	pReadCodecContext->sample_fmt = (AVSampleFormat)pFormatCtx_Audio->streams[0]->codecpar->format;
	//pReadCodecContext->sample_fmt = AV_SAMPLE_FMT_FLTP;

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


	return 0;
}


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


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

		AudioIndex_mic = 0;

		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(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();


	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);

	ret = swr_init(audio_convert_ctx);

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

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

	HANDLE hThread = CreateThread(NULL, 0, AudioMicCapThreadProc, 0, 0, NULL);

	while (1)
	{
		if (NULL == fifo_audio)
		{
			continue;
		}
		if (av_audio_fifo_size(fifo_audio) >=
			(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);

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

			AVPacket pkt_out_mic = { 0 };

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

			frame_mic->pts = AudioFrameIndex * 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 * pFormatCtx_Out->streams[AudioIndex_mic]->codecpar->frame_size;
				pkt_out_mic.dts = AudioFrameIndex * pFormatCtx_Out->streams[AudioIndex_mic]->codecpar->frame_size;
				pkt_out_mic.duration = pFormatCtx_Out->streams[AudioIndex_mic]->codecpar->frame_size;

				int ret2 = av_interleaved_write_frame(pFormatCtx_Out, &pkt_out_mic);
				av_packet_unref(&pkt_out_mic);
			}
			AudioFrameIndex++;
			if (AudioFrameIndex > 3000)
			{
				bCap = false;
				break;
			}
		}
	}
	Sleep(100);
	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(bCap)
	{
		av_packet_unref(&packet);
		if (av_read_frame(pFormatCtx_Audio, &packet) < 0)
		{
			continue;
		}

		ret = avcodec_send_packet(pReadCodecContext, &packet);
		if (ret >= 0)
		{
			ret = avcodec_receive_frame(pReadCodecContext, 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)
			{
				fifo_audio = 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);
			if (av_audio_fifo_space(fifo_audio) >= pFrame->nb_samples)
			{
				ret = av_audio_fifo_write(fifo_audio, (void **)pFrame->data, pFrame->nb_samples);
			}



			av_packet_unref(&packet);
		}

	}

	return 0;
}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值