Search code examples
ffmpegdirectshowffdshow

Capturing 48 kHz audio with FFmpeg and DirectShow (dshow input)


I tried to capture audio with 48 kHz in FFmpeg, the code as below:

 AVInputFormat* ifmt = av_find_input_format("dshow");
    CHECK_POINTER_RETURN_VALUE(ifmt, false)

    pFmtCtx = avformat_alloc_context();
    CHECK_POINTER_RETURN_VALUE(pFmtCtx, false)

    AVDictionary *param = nullptr;
    std::string sr = std::to_string(48000);
    av_dict_set(&param, "sample_rate",sr.c_str(), 0);

    int error = avformat_open_input(&pFmtCtx, ffName.c_str(), ifmt, &param);
    if (error != 0) {
        char buf[2014];
        av_strerror(error, buf, 1024);
        LOG(ERROR)<<"open audio device failed,err is "<<buf;
         return false;
    }

but "avformat_open_input" return fail, err shows "I/O error", if the sample rate is 44100, all is OK.

Now FFmpeg doesn't support capturing 48 kHz audio?


Solution

  • As @die maus mentioned, the fact that this works if sample rate is set to 44100, but not 48000, likely indicates that your input device does not support sampling at 48 kHz. This is not a limitation of FFmpeg, but of the hardware.

    And as @moi suggested, unless you have a specific need for 48 kHz, 44.1 should work just fine.

    If you really need 48 kHz (e.g. you are sending the audio to something else that expects 48 kHz), you can resample the audio. FFmpeg includes libswresample for this purpose; see the example here.