请稍等 ...
×

采纳答案成功!

向帮助你的同学说点啥吧!感谢那些助人为乐的人

aac编码无法播放

等了几天 没人回答 整的我都想退款了
编辑后的提问不会出现在待回答区域 这什么逻辑这

我能想到的需要补充的信息就这些了

ffmpeg 命令行采集

D:\SSD\library\installed\bin>ffmpeg.exe -f dshow -i audio="Microphone (USB Audio Device)" out.pcm
ffmpeg version N-109977-gaca7ef78cc-20230309 Copyright (c) 2000-2023 the FFmpeg developers
  built with gcc 12.2.0 (crosstool-NG 1.25.0.90_cf9beb1)
  configuration: --prefix=/ffbuild/prefix --pkg-config-flags=--static --pkg-config=pkg-config --cross-prefix=x86_64-w64-mingw32- --arch=x86_64 --target-os=mingw32 --enable-gpl --enable-version3 --disable-debug --enable-shared --disable-static --disable-w32threads --enable-pthreads --enable-iconv --enable-libxml2 --enable-zlib --enable-libfreetype --enable-libfribidi --enable-gmp --enable-lzma --enable-fontconfig --enable-libvorbis --enable-opencl --disable-libpulse --enable-libvmaf --disable-libxcb --disable-xlib --enable-amf --enable-libaom --enable-libaribb24 --enable-avisynth --enable-chromaprint --enable-libdav1d --enable-libdavs2 --disable-libfdk-aac --enable-ffnvcodec --enable-cuda-llvm --enable-frei0r --enable-libgme --enable-libkvazaar --enable-libass --enable-libbluray --enable-libjxl --enable-libmp3lame --enable-libopus --enable-librist --enable-libssh --enable-libtheora --enable-libvpx --enable-libwebp --enable-lv2 --disable-libmfx --enable-libvpl --enable-openal --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenh264 --enable-libopenjpeg --enable-libopenmpt --enable-librav1e --enable-librubberband --enable-schannel --enable-sdl2 --enable-libsoxr --enable-libsrt --enable-libsvtav1 --enable-libtwolame --enable-libuavs3d --disable-libdrm --disable-vaapi --enable-libvidstab --enable-vulkan --enable-libshaderc --enable-libplacebo --enable-libx264 --enable-libx265 --enable-libxavs2 --enable-libxvid --enable-libzimg --enable-libzvbi --extra-cflags=-DLIBTWOLAME_STATIC --extra-cxxflags= --extra-ldflags=-pthread --extra-ldexeflags= --extra-libs=-lgomp --extra-version=20230309
  libavutil      58.  3.100 / 58.  3.100
  libavcodec     60.  6.100 / 60.  6.100
  libavformat    60.  4.100 / 60.  4.100
  libavdevice    60.  2.100 / 60.  2.100
  libavfilter     9.  4.100 /  9.  4.100
  libswscale      7.  2.100 /  7.  2.100
  libswresample   4. 11.100 /  4. 11.100
  libpostproc    57.  2.100 / 57.  2.100
[aist#0:0/pcm_s16le @ 000001b508cec300] Guessed Channel Layout: stereo
Input #0, dshow, from 'audio=Microphone (USB Audio Device)':
  Duration: N/A, start: 218321.328000, bitrate: 1411 kb/s
  Stream #0:0: Audio: pcm_s16le, 44100 Hz, 2 channels, s16, 1411 kb/s
Stream mapping:
  Stream #0:0 -> #0:0 (pcm_s16le (native) -> adpcm_ima_alp (native))
Press [q] to stop, [?] for help
Output #0, alp, to 'out.pcm':
  Metadata:
    encoder         : Lavf60.4.100
  Stream #0:0: Audio: adpcm_ima_alp, 44100 Hz, stereo, s16, 352 kb/s
    Metadata:
      encoder         : Lavc60.6.100 adpcm_ima_alp
size=     129kB time=00:00:02.99 bitrate= 352.5kbits/s speed=1.22x
video:0kB audio:129kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: 0.015141%
Exiting normally, received signal 2.

编码之后无法播放

D:\CLionProjects\learn-ffmpeg\cmake-build-debug>ffplay.exe test.aac
ffplay version N-109977-gaca7ef78cc-20230309 Copyright (c) 2003-2023 the FFmpeg developers
  built with gcc 12.2.0 (crosstool-NG 1.25.0.90_cf9beb1)
  configuration: --prefix=/ffbuild/prefix --pkg-config-flags=--static --pkg-config=pkg-config --cross-prefix=x86_64-w64-mingw32- --arch=x86_64 --target-os=mingw32 --enable-gpl --enable-version3 --disable-debug --enable-shared --disable-static --disable-w32threads --enable-pthreads --enable-iconv --enable-libxml2 --enable-zlib --enable-libfreetype --enable-libfribidi --enable-gmp --enable-lzma --enable-fontconfig --enable-libvorbis --enable-opencl --disable-libpulse --enable-libvmaf --disable-libxcb --disable-xlib --enable-amf --enable-libaom --enable-libaribb24 --enable-avisynth --enable-chromaprint --enable-libdav1d --enable-libdavs2 --disable-libfdk-aac --enable-ffnvcodec --enable-cuda-llvm --enable-frei0r --enable-libgme --enable-libkvazaar --enable-libass --enable-libbluray --enable-libjxl --enable-libmp3lame --enable-libopus --enable-librist --enable-libssh --enable-libtheora --enable-libvpx --enable-libwebp --enable-lv2 --disable-libmfx --enable-libvpl --enable-openal --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenh264 --enable-libopenjpeg --enable-libopenmpt --enable-librav1e --enable-librubberband --enable-schannel --enable-sdl2 --enable-libsoxr --enable-libsrt --enable-libsvtav1 --enable-libtwolame --enable-libuavs3d --disable-libdrm --disable-vaapi --enable-libvidstab --enable-vulkan --enable-libshaderc --enable-libplacebo --enable-libx264 --enable-libx265 --enable-libxavs2 --enable-libxvid --enable-libzimg --enable-libzvbi --extra-cflags=-DLIBTWOLAME_STATIC --extra-cxxflags= --extra-ldflags=-pthread --extra-ldexeflags= --extra-libs=-lgomp --extra-version=20230309
  libavutil      58.  3.100 / 58.  3.100
  libavcodec     60.  6.100 / 60.  6.100
  libavformat    60.  4.100 / 60.  4.100
  libavdevice    60.  2.100 / 60.  2.100
  libavfilter     9.  4.100 /  9.  4.100
  libswscale      7.  2.100 /  7.  2.100
  libswresample   4. 11.100 /  4. 11.100
  libpostproc    57.  2.100 / 57.  2.100
[aac @ 00000199189c7300] Format aac detected only with low score of 1, misdetection possible!
test.aac: End of file
extern "C" {
#include <libavcodec/avcodec.h>
#include <libavdevice/avdevice.h>
#include <libavformat/avformat.h>
#include <libavutil/avutil.h>
#include <libswresample/swresample.h>
}

#include <cassert>
#include <iostream>

int main() {
    using std::cout;
    av_log_set_level(AV_LOG_TRACE);
    char errbuf[1024] = {0};

    avdevice_register_all();

    auto iformat = av_find_input_format("dshow");

    AVFormatContext *fmt_ctx = nullptr;


    auto ret = avformat_open_input(&fmt_ctx, "audio=Microphone (USB Audio Device)", iformat, nullptr);
    //    auto ret = avformat_open_input(&fmt_ctx, "audio=耳机 (F910 Hands-Free AG Audio)", iformat, nullptr);
    ;

    if (ret != 0) {

        av_strerror(ret, errbuf, sizeof(errbuf));
        std::cout << errbuf << '\n';
        return ret;
    }

    auto swr_ctx = swr_alloc_set_opts(nullptr,
                                      AV_CH_LAYOUT_STEREO, AV_SAMPLE_FMT_FLTP, 44100,
                                      AV_CH_LAYOUT_STEREO, AV_SAMPLE_FMT_S16, 44100, 0, nullptr);

    ret = swr_init(swr_ctx);
    if (ret != 0) {
        av_strerror(ret, errbuf, sizeof(errbuf));
        std::cout << errbuf << '\n';
        return ret;
    }

    uint8_t **src_data = nullptr;
    int linesize = 0;
    int nbSamples = 88200 / 2 / 2;
    auto destNbSamples = 1024; 
    // 不懂重采样的原理   随手调的1024  起码avcodec_send_frame 不会返回AVERROR(EINVAL) 了

    uint8_t **dest_data = nullptr;
    int dest_linesize = 0;

    av_samples_alloc_array_and_samples(&src_data, &linesize, 2, nbSamples, AV_SAMPLE_FMT_S16, 0);
    av_samples_alloc_array_and_samples(&dest_data, &dest_linesize, // dest_linesize 4096
                                       2, destNbSamples, AV_SAMPLE_FMT_FLTP, 0);

    if (src_data == nullptr || dest_data == nullptr) {
        return -1;
    }

    auto encode_aac = avcodec_find_encoder(AV_CODEC_ID_AAC);
    auto audio_aac_ctx = avcodec_alloc_context3(encode_aac);

    audio_aac_ctx->sample_fmt = AV_SAMPLE_FMT_FLTP;
    audio_aac_ctx->channel_layout = AV_CH_LAYOUT_STEREO;
    audio_aac_ctx->sample_rate = 44100;
    audio_aac_ctx->bit_rate = 0;
    audio_aac_ctx->profile = FF_PROFILE_AAC_HE_V2;

    ret = avcodec_open2(audio_aac_ctx, encode_aac, nullptr);

    if (ret != 0) {
        av_strerror(ret, errbuf, sizeof(errbuf));
        std::cout << errbuf << '\n';
        return ret;
    }

    auto src_frame = av_frame_alloc();
    src_frame->nb_samples = audio_aac_ctx->frame_size;// 1024
    src_frame->format = encode_aac->sample_fmts[0];
    src_frame->channel_layout = AV_CH_LAYOUT_STEREO;

    ret = av_frame_get_buffer(src_frame, 0);// 4096
    if (ret != 0) {
        av_strerror(ret, errbuf, sizeof(errbuf));
        std::cout << errbuf << '\n';
        return ret;
    }

    AVPacket destPacket = {0};

    auto fp_pcm = fopen("test.pcm", "wb");
    auto fp = fopen("test.aac", "wb");

    AVPacket *pkt = av_packet_alloc();
    auto count = 20;

    while ((ret = av_read_frame(fmt_ctx, pkt)) == 0 && count--) {

        memcpy(src_data[0], pkt->data, pkt->size);

        swr_convert(swr_ctx, dest_data, destNbSamples, (const uint8_t **) src_data, nbSamples);
        fwrite(dest_data[0], 1, dest_linesize, fp_pcm);

        memcpy(src_frame->data[0], dest_data[0], dest_linesize);

        ret = avcodec_send_frame(audio_aac_ctx, src_frame);

        if (ret == AVERROR(EAGAIN)) {
            continue;
        }

        if (ret != 0) {
            av_strerror(ret, errbuf, sizeof(errbuf));
            std::cout << errbuf << '\n';
            return ret;
        }

        while ((ret = avcodec_receive_packet(audio_aac_ctx, &destPacket)) == 0) {
            fwrite(destPacket.data, 1, destPacket.size, fp);
        }

        av_packet_unref(pkt);
    }

    av_packet_unref(pkt);

    swr_free(&swr_ctx);

    avformat_close_input(&fmt_ctx);
}

正在回答 回答被采纳积分+3

1回答

李超 2023-05-08 16:08:08

这个问答系统可能有BUG,你最好加入到课程的QQ群里,这样沟通起来比较方便。在PC端进入课程,在右侧有入群密码。

对于你这个问题,你不编码的请联系采集到的声音可以正常播放吗?

0 回复 有任何疑惑可以回复我~
  • 提问者 juvenile_time #1
    正常采集是可以播放的
    回复 有任何疑惑可以回复我~ 2023-05-08 16:13:48
  • 李超 回复 提问者 juvenile_time #2
    你用的不是fdkaac 吧,用的系统自带的编码器?
    回复 有任何疑惑可以回复我~ 2023-05-08 16:20:31
问题已解决,确定采纳
还有疑问,暂不采纳
意见反馈 帮助中心 APP下载
官方微信