ffmpeg 本地麦克风声音和系统声音混音后,再混合本地桌面成最终的mp4文件

之前写过ffmpeg录制麦克风声音和pc内部声音(如播放歌曲)—混音,现在再进一步,混音后,再和采集到的桌面视频合在一处。

现在说说大致思路,如下所示,创建了四个线程

	HANDLE hThreadAudio = CreateThread(NULL, 0, AudioCapThreadProc, 0, 0, NULL);
	HANDLE hThreadAudioMic = CreateThread(NULL, 0, AudioMicCapThreadProc, 0, 0, NULL);
	HANDLE hThreadAudioMix = CreateThread(NULL, 0, AudioMixThreadProc, 0, 0, NULL);
	HANDLE hThreadVideo = CreateThread(NULL, 0, ScreenCapThreadProc, 0, 0, NULL);

其中线程hThreadAudio代表的是本地pc内部声音采集,线程hThreadAudioMic代表的是本地麦克风声音采集。线程hThreadAudioMix专门用于上面两路音频混音,线程hThreadVideo用于抓取桌面视频,然后主线程对混音后的音频数据和视频数据进行合并。

接着我们看如下四个队列变量:

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

///fifo_audio_mix是两路音频混音后,声音所要塞入的队列,便于后续做音视频同步
AVAudioFifo		*fifo_audio_mix = NULL;

其中fifo_video是线程hThreadVideo抓取的视频数据所入的队列;fifo_audio是线程hThreadAudio抓取的电脑内部音频数据所入的队列;fifo_audio_mic是线程hThreadAudioMic抓取的电脑麦克风音频数据所入的队列,fifo_audio_mix是电脑内部音频和麦克风音频数据合并后,缩入的队列。

主线程负责从fifo_video取视频数据,从fifo_audio_mix取混音后的音频数据,然后写入文件。

音频格式上,hThreadAudio和hThreadAudioMic采集到的都是AV_SAMPLE_FMT_S16格式,故混音时,采取的格式也是AV_SAMPLE_FMT_S16。最后写文件的时候,需要转换成AV_SAMPLE_FMT_FLTP格式。

下面是完整代码:

// 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/avutil.h"
#include "libavutil/fifo.h"
#include "libavutil/frame.h"
#include "libavutil/imgutils.h"

#include "libavfilter/avfilter.h"
#include "libavfilter/buffersink.h"
#include "libavfilter/buffersrc.h"

#include "SDL.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")

#pragma comment(lib, "SDL2.lib")

#ifdef __cplusplus
};
#endif




AVFormatContext	*pFormatCtx_Audio = NULL;
AVFormatContext	*pFormatCtx_AudioMic = NULL;
AVFormatContext *pFormatCtx_Out = NULL;

AVFormatContext	*pFormatCtx_Video = NULL;

AVCodecContext *pReadCodecCtx_Video = NULL;
AVCodecContext *pReadCodecCtx_Audio = NULL;
AVCodecContext *pReadCodecCtx_AudioMic = NULL;

AVCodec *pReadCodec_Video = NULL;

int iVideoStreamIndex = 0;
int iAudioStreamIndex = 1;

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

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

///fifo_audio_mix是两路音频混音后,声音所要塞入的队列,便于后续做音视频同步
AVAudioFifo		*fifo_audio_mix = NULL;

SwsContext *img_convert_ctx = NULL;
SwrContext *audio_convert_ctx = NULL;

uint8_t *picture_buf = NULL, *frame_buf = NULL;

bool bCap = true;

int AudioFrameIndex_mix = 0;


int64_t cur_pts_v = 0;
int64_t cur_pts_a = 0;


int yuv420_frame_size = 0;



AVFilterGraph* _filter_graph = NULL;
AVFilterContext* _filter_ctx_src_inner = NULL;
AVFilterContext* _filter_ctx_src_mic = NULL;
AVFilterContext* _filter_ctx_sink = NULL;


CRITICAL_SECTION VideoSection;
CRITICAL_SECTION AudioSection;
CRITICAL_SECTION AudioSection_mic;
CRITICAL_SECTION AudioSection_mix;


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

DWORD WINAPI AudioMixThreadProc(LPVOID lpParam);

DWORD WINAPI ScreenCapThreadProc(LPVOID lpParam);


typedef struct BufferSourceContext {
   
   
	const AVClass    *bscclass;
	AVFifoBuffer     *fifo;
	AVRational        time_base;     ///< time_base to set in the output link
	AVRational        frame_rate;    ///< frame_rate to set in the output link
	unsigned          nb_failed_requests;
	unsigned          warning_limit;

	/* video only */
	int               w, h;
	enum AVPixelFormat  pix_fmt;
	AVRational        pixel_aspect;
	char              *sws_param;

	AVBufferRef *hw_frames_ctx;

	/* audio only */
	int sample_rate;
	enum AVSampleFormat sample_fmt;
	int channels;
	uint64_t channel_layout;
	char    *channel_layout_str;

	int got_format_from_params;
	int eof;
} BufferSourceContext;



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 InitFilter(const char* filter_desc)
{
   
   
	char args_inner[512];
	const char* pad_name_inner = "in0";
	char args_mic[512];
	const char* pad_name_mic = "in1";

	AVFilter* filter_src_spk = (AVFilter *)avfilter_get_by_name("abuffer");
	AVFilter* filter_src_mic = (AVFilter *)avfilter_get_by_name("abuffer");
	AVFilter* filter_sink = (AVFilter *)avfilter_get_by_name("abuffersink");
	AVFilterInOut* filter_output_inner = avfilter_inout_alloc();
	AVFilterInOut* filter_output_mic = avfilter_inout_alloc();
	AVFilterInOut* filter_input = avfilter_inout_alloc();
	_filter_graph = avfilter_graph_alloc();

	sprintf_s(args_inner, sizeof(args_inner), "time_base=%d/%d:sample_rate=%d:sample_fmt=%s:channel_layout=0x%I64x",
		pReadCodecCtx_Audio->time_base.num,
		pReadCodecCtx_Audio->time_base.den,
		pReadCodecCtx_Audio->sample_rate,
		av_get_sample_fmt_name((AVSampleFormat)pReadCodecCtx_Audio->sample_fmt),
		pReadCodecCtx_Audio->channel_layout);
	sprintf_s(args_mic, sizeof(args_mic), "time_base=%d/%d:sample_rate=%d:sample_fmt=%s:channel_layout=0x%I64x",
		pReadCodecCtx_AudioMic->time_base.num,
		pReadCodecCtx_AudioMic->time_base.den,
		pReadCodecCtx_AudioMic->sample_rate,
		av_get_sample_fmt_name((AVSampleFormat)pReadCodecCtx_AudioMic->sample_fmt),
		pReadCodecCtx_AudioMic->channel_layout);

	//sprintf_s(args_spk, sizeof(args_spk), "time_base=%d/%d:sample_rate=%d:sample_fmt=%s:channel_layout=0x%I64x", _fmt_ctx_out->streams[_index_a_out]->codec->time_base.num, _fmt_ctx_out->streams[_index_a_out]->codec->time_base.den, _fmt_ctx_out->streams[_index_a_out]->codec->sample_rate, av_get_sample_fmt_name(_fmt_ctx_out->streams[_index_a_out]->codec->sample_fmt), _fmt_ctx_out->streams[_index_a_out]->codec->channel_layout);
	//sprintf_s(args_mic, sizeof(args_mic), "time_base=%d/%d:sample_rate=%d:sample_fmt=%s:channel_layout=0x%I64x", _fmt_ctx_out->streams[_index_a_out]->codec->time_base.num, _fmt_ctx_out->streams[_index_a_out]->codec->time_base.den, _fmt_ctx_out->streams[_index_a_out]->codec->sample_rate, av_get_sample_fmt_name(_fmt_ctx_out->streams[_index_a_out]->codec->sample_fmt), _fmt_ctx_out->streams[_index_a_out]->codec->channel_layout);


	int ret = 0;
	ret = avfilter_graph_create_filter(&_filter_ctx_src_inner, filter_src_spk, pad_name_inner, args_inner, NULL, _filter_graph);
	if (ret 
要使用 FFmpeg 实现麦克风音频桌面音频的混音,通常需要以下几个关键步骤:采集麦克风音频、采集桌面音频、将两者进行混音,最后将混合后的音频与桌面视频进行合输出最终MP4 文件。 ### 采集麦克风音频 麦克风音频的采集可以通过 `dshow` 设备进行捕获。例如,在 Windows 环境下可以使用如下命令: ```bash -i audio="麦克风名称" ``` 完整命令示例: ```bash ffmpeg -f dshow -i audio="麦克风 (Realtek Audio)" ... ``` ### 采集桌面音频 桌面音频(系统音频)的采集可以通过 `dshow` 捕获系统音频设备。例如: ```bash -f dshow -i audio="立体声混音" ``` 完整命令示例: ```bash ffmpeg -f dshow -i audio="立体声混音 (Realtek Audio)" ... ``` ### 混音麦克风音频桌面音频 FFmpeg 提供了内置的音频滤镜来实现混音操作。可以使用 `amix` 音频滤镜来混合多个音频流。具体命令如下: ```bash -filter_complex amix=inputs=2:duration=first:dropout_transition=3 ``` 完整命令示例: ```bash ffmpeg \ -f dshow -i audio="麦克风 (Realtek Audio)" \ -f dshow -i audio="立体声混音 (Realtek Audio)" \ -filter_complex amix=inputs=2:duration=first:dropout_transition=3 \ ... ``` ### 采集桌面视频 桌面视频的采集可以使用 `gdigrab` 设备进行捕获。例如: ```bash -f gdigrab -framerate 30 -i desktop ``` 完整命令示例: ```bash ffmpeg -f gdigrab -framerate 30 -i desktop ... ``` ### 合并视频混合音频 将采集到的桌面视频混合后的音频合并为最终MP4 文件,可以使用以下命令: ```bash -c:v libx264 -preset ultrafast -pix_fmt yuv420p -c:a aac output.mp4 ``` 完整命令示例: ```bash ffmpeg \ -f gdigrab -framerate 30 -i desktop \ -f dshow -i audio="麦克风 (Realtek Audio)" \ -f dshow -i audio="立体声混音 (Realtek Audio)" \ -filter_complex amix=inputs=2:duration=first:dropout_transition=3 \ -c:v libx264 -preset ultrafast -pix_fmt yuv420p -c:a aac \ output.mp4 ``` ### 代码实现混音原理 在程序实现中,如果直接操作音频数据,可以采用 PCM 数据叠加的方式进行混音。例如,假设 `PCM1` `PCM2` 是两路音频数据,则混合后的音频数据为: ```c MIX_PCM = PCM1 / 2 + PCM2 / 2 ``` 在混音过程中,如果需要禁用某一路音频,可以将该路音频数据置零: ```c if (m_bflagAudioSys) { memset(input_frame->data[0], 0, input_frame->nb_samples * 2 * 2); } ``` ### 音频格式转换 在 FFmpeg 中,音频采集时通常使用 `AV_SAMPLE_FMT_S16` 格式,而在编码输出时需要转换为 `AV_SAMPLE_FMT_FLTP` 格式。这可以通过 `swr_convert` 函数进行音频重采样格式转换。 ### 示例代码 以下是一个简单的音频混音格式转换的代码示例: ```c // 假设 input_frame1 input_frame2 是两路输入音频帧 AVFrame *mix_frame = av_frame_alloc(); mix_frame->sample_rate = input_frame1->sample_rate; mix_frame->channel_layout = input_frame1->channel_layout; mix_frame->format = AV_SAMPLE_FMT_S16; av_frame_get_buffer(mix_frame, 0); // 混音操作 for (int i = 0; i < input_frame1->nb_samples; i++) { ((int16_t *)mix_frame->data[0])[i] = ((int16_t *)input_frame1->data[0])[i] / 2 + ((int16_t *)input_frame2->data[0])[i] / 2; } // 格式转换 SwrContext *swr_ctx = swr_alloc_set_opts(NULL, AV_CH_LAYOUT_STEREO, AV_SAMPLE_FMT_FLTP, mix_frame->sample_rate, AV_CH_LAYOUT_STEREO, AV_SAMPLE_FMT_S16, mix_frame->sample_rate, 0, NULL); swr_convert_frame(swr_ctx, output_frame, mix_frame); ``` ### 总结 通过上述方法,可以使用 FFmpeg 实现麦克风音频桌面音频的混音操作,并结合桌面视频录制生最终MP4 文件。在实际应用中,需要注意音频格式的转换混音算法的实现,以确保音频质量。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值