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上上传了相关资源,并且免费下载。
声音没有视频那样好直观理解,我是在弄懂了音频的大致存储格式后,才成功的录制了本地的麦克风音频,读者后面可以参考下:
音视频的基础知识
同时从麦克风处读取的sample_fmt是AV_SAMPLE_FMT_S16,而最终写文件时,sample_fmt是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"
#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_mic = NULL;
SwrContext *audio_convert_ctx = NULL;
uint8_t *picture_buf = NULL, *frame_buf = NULL;
bool bCap = true;
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)");
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;
}
int AudioFrameIndex_mic = 0;
CreateThread(NULL, 0, AudioMicCapThreadProc, 0, 0, NULL);
while (1)
{
if (NULL == fifo_audio_mic)
{
continue;
}
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);
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));
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;
int ret2 = av_interleaved_write_frame(pFormatCtx_Out, &pkt_out_mic);
av_packet_unref(&pkt_out_mic);
}
AudioFrameIndex_mic++;
if (AudioFrameIndex_mic > 1000)
{
break;
}
}
}
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;
for (;;)
{
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_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)
{
ret = av_audio_fifo_write(fifo_audio_mic, (void **)pFrame->data, pFrame->nb_samples);
}
av_packet_unref(&packet);
}
}
return 0;
}