vs版本:vs2017
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 --disable-debug --enable-shared --disable-static --enable-gpl --enable-libx264 --extra-cflags=-I/usr/local/x264/include --extra-ldflags=’-L/usr/local/x264/lib -static’ --pkgconfigdir=/usr/local/x264/lib/pkgconfig --prefix=/home/ffmpeg_x264_dll
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
libpostproc 56. 0.100 / 56. 0.100
Hyper fast Audio and Video encoder
将麦克风声音和pc内部声音混合在一起时,最开始想到的是如同将声音和视频混在一起,这方面,大家可以看我写的一篇博客:ffmpeg录制桌面视频和麦克风音频(音视频同步)
我按照这种方式,创建了两路流,一路写入麦克风声音,一路写入电脑内部声音,但是最终生成的声音文件,无法达到播放的效果。
后面查了下,ffmpeg有提供混流的方式,下面不多说,直接上代码。
// 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 "libavfilter/avfilter.h"
#include "libavfilter/buffersink.h"
#include "libavfilter/buffersrc.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;
AVFormatContext *pFormatCtx_AudioMic = NULL;
AVFormatContext *pFormatCtx_Out = NULL;
AVCodecContext *pReadCodecContext = NULL;
AVCodecContext *pReadMicCodecContext = NULL;
int AudioIndex = 0;
AVCodecContext *pCodecEncodeCtx_Audio = NULL;
AVCodec *pCodecEncode_Audio = NULL;
AVAudioFifo *fifo_audio = NULL;
AVAudioFifo *fifo_audio_mic = NULL;
SwrContext *audio_convert_ctx = NULL;
uint8_t *picture_buf = NULL, *frame_buf = NULL;
bool bCap = true;
int AudioFrameIndex = 0;
int AudioMicFrameIndex = 0;
int64_t cur_pts_a = 0;
int64_t cur_pts_a_mic = 0;
AVFilterGraph* _filter_graph = NULL;
AVFilterContext* _filter_ctx_src_inner = NULL;
AVFilterContext* _filter_ctx_src_mic = NULL;
AVFilterContext* _filter_ctx_sink = NULL;
CRITICAL_SECTION AudioSection;
CRITICAL_SECTION AudioSection_mic;
DWORD WINAPI AudioCapThreadProc(LPVOID lpParam);
DWORD WINAPI AudioMicCapThreadProc(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
使用FFmpeg混合麦克风与PC声音

该博客介绍了如何使用FFmpeg库将麦克风输入和PC内部声音混合在一起。通过创建两个输入流,分别捕获麦克风和系统声音,然后利用FFmpeg的过滤功能,将两者混合成一个音频流。作者展示了使用C++实现的代码示例,包括初始化过滤器、打开音频输入和输出、配置编码器等步骤,最终将混合音频写入输出文件。
最低0.47元/天 解锁文章
1241

被折叠的 条评论
为什么被折叠?



