Mac下调用av_dump_format无法识别AVSampleFormat

来源:2-3 FFmpeg基本信息查询命令实战

qq_慕斯卡0073816

2020-06-12

程序是这样写的,就是打开音频设备,调用一下av_dump_format就退出:

#include <stdio.h>
#include <string>
#ifdef _WIN32
#include <jlib/win32/UnicodeTool.h>
#endif

#pragma warning(disable:4819)
#pragma warning(disable:4996)

#ifdef __cplusplus
extern "C" {
#endif
#include <libavcodec/avcodec.h>
#include <libavdevice/avdevice.h>
#include <libavformat/avformat.h>
#include <libavutil/avutil.h>
#ifdef __cplusplus
}
#endif

#ifdef _WIN32
#pragma comment(lib, "avcodec.lib")
#pragma comment(lib, "avdevice.lib")
#pragma comment(lib, "avformat.lib")
#pragma comment(lib, "avutil.lib")
#endif

int main()
{
#ifdef _WIN32
	const char* device = "dshow";
	std::string audio_ = jlib::win32::mbcs_to_utf8("audio=麦克风 (Realtek High Definition Audio)");
	const char* audio = audio_.data();
#elif defined(__APPLE__)
	const char* device = "avfoundation";
	const char* audio = ":0";
#else
#error todo
#endif

	avdevice_register_all(); // 这一句必须有,否则下面返回的 ifmt 是 NULL
	AVInputFormat* ifmt = av_find_input_format(device);
	if (!ifmt) {
		fprintf(stderr, "Failed to find input format for input device '%s'\n", device);
		return -1;
	}
	AVFormatContext* ic = NULL;
	char msg[1024];
	int ret = avformat_open_input(&ic, audio, ifmt, NULL);
	if (ret < 0) {
		av_strerror(ret, msg, sizeof(msg));
		fprintf(stderr, "Failed to open audio device '%s':%s\n", audio, msg);
		return ret;
	}

	av_dump_format(ic, 0, audio, 0);
	avformat_close_input(&ic);

	return 0;
}
  1. Windows下执行结果:
    windows

    可以看到有 s16 ,说明获取到了 AVSampleFormat,后面会说根据什么判断的。

  2. Mac下执行结果:
    Mac
    可以看到并没有输出 AVSampleFormat 相关的信息。

  3. 追踪 av_dump_format
    输出 Stream #0:0 相关信息的代码是在文件 util.c 函数 dump_stream_format内,又调用了 avcodec_string 函数,具体是 1330行:

    if (enc->sample_fmt != AV_SAMPLE_FMT_NONE) {
        snprintf(buf + strlen(buf), buf_size - strlen(buf),
                 ", %s", av_get_sample_fmt_name(enc->sample_fmt));
    }
    

    但是Macenc->sample_fmt总是 -1。问题是直接执行 ffmpeg -f avfoundation -i :0 结果是有这个信息的:
    ffmpeg
    flt ,说明 ffmpeg 内部是可以识别的,不知道我的代码缺少了什么关键点,ffmpeg 的源码太庞杂了,读不下去。。。请高手指点一下,感激不尽。

写回答

1回答

李超

2020-07-15

这个只能一点点追代码,实际上阅读代码的能力是你成为高手的必经之路。如果你目前这种能力还不够的话,我觉得你可以把这个问题先放一放,先跟着课程走,等后面能力够的时候再深入

0
0

经典再升级-FFmpeg5.0核心技术精讲,打造音视频播放器

学好FFmpeg核心技术,做高效,高薪,有竞争力的音视频工程师

2726 学习 · 814 问题

查看课程