使用 FFMPEG 解码音频

发布于 2024-11-09 14:56:05 字数 2610 浏览 1 评论 0原文

我正在使用 FFMPEG 解码音频文件,但我无法做到这一点。

    AVFormatContext *pFormatCtx;
AVCodec *pCodec_audio;
AVCodecContext *aCodecCtx_audio = NULL;
AVPacket packet;
string outfilename = "salida.avi";
string filename = "john.wav";
int audioStream;
int out_size, len, size;
FILE *outfile;
int16_t *outbuf;
uint8_t inbuf[INBUF_SIZE + FF_INPUT_BUFFER_PADDING_SIZE];
avcodec_init();
/* register all the codecs */
av_register_all();

if(av_open_input_file(&pFormatCtx, filename.c_str(), NULL, 0, NULL) != 0) {
    cerr << "Archivo no encontrado" << endl;
    return -1; // Couldn't open file
}

if(av_find_stream_info(pFormatCtx) < 0) {
    cerr << " No encontro el stream de info" << endl;
    return -1;// Couldn't find stream information
}

dump_format(pFormatCtx, 0, filename.c_str(), 0);

for(unsigned int i=0; i < pFormatCtx->nb_streams; i++)
  if(pFormatCtx->streams[i]->codec->codec_type == CODEC_TYPE_AUDIO)
      audioStream = i;
aCodecCtx_audio = pFormatCtx->streams[audioStream]->codec;
pCodec_audio = avcodec_find_decoder(aCodecCtx_audio->codec_id);
if(pCodec_audio == NULL) {
    cerr<< "Unsupported codec!" << endl;
    return -1; // Codec not found
}

if(avcodec_open(aCodecCtx_audio, pCodec_audio) < 0) {
    cerr << "No se pudo abrir el codec de audio" << endl;
    return -1;
}

outbuf = (int16_t*) av_malloc(AVCODEC_MAX_AUDIO_FRAME_SIZE);

outfile = fopen(outfilename.c_str(), "wb");
if (!outfile) {
    exit(1);
}

av_init_packet(&packet);
packet.data = inbuf;
while(av_read_frame(pFormatCtx, &packet) == 0) {
    if(packet.stream_index == audioStream) {
        size = packet.size;
        if (size == 0) {
            cerr << "Size = 0 " << endl;
            break;
        }
        while(size > 0) {
            out_size = AVCODEC_MAX_AUDIO_FRAME_SIZE;
            len = avcodec_decode_audio3(aCodecCtx_audio, outbuf, &out_size , &packet);
            //av_free_packet(&packet);
            cout << len << endl;
            if(len == -1) {
                cerr << "Error while decoding" << endl;
                return 1;
            }
            if(out_size > 0) {
                fwrite(outbuf, 1, out_size, outfile);
            }
            size -= len;
            packet.data += len;
        }
    }
}
av_free_packet(&packet);


fclose(outfile);
free(outbuf);
cout << "END CODE" << endl;
avcodec_close(aCodecCtx_audio);
av_free(aCodecCtx_audio);

在代码中,我尝试将 .wav 文件解码为 .avi 文件。输出文件(“salida.avi”)已创建,但无法播放。

I am using FFMPEG to decode an audio file, but I am not able to do it.

    AVFormatContext *pFormatCtx;
AVCodec *pCodec_audio;
AVCodecContext *aCodecCtx_audio = NULL;
AVPacket packet;
string outfilename = "salida.avi";
string filename = "john.wav";
int audioStream;
int out_size, len, size;
FILE *outfile;
int16_t *outbuf;
uint8_t inbuf[INBUF_SIZE + FF_INPUT_BUFFER_PADDING_SIZE];
avcodec_init();
/* register all the codecs */
av_register_all();

if(av_open_input_file(&pFormatCtx, filename.c_str(), NULL, 0, NULL) != 0) {
    cerr << "Archivo no encontrado" << endl;
    return -1; // Couldn't open file
}

if(av_find_stream_info(pFormatCtx) < 0) {
    cerr << " No encontro el stream de info" << endl;
    return -1;// Couldn't find stream information
}

dump_format(pFormatCtx, 0, filename.c_str(), 0);

for(unsigned int i=0; i < pFormatCtx->nb_streams; i++)
  if(pFormatCtx->streams[i]->codec->codec_type == CODEC_TYPE_AUDIO)
      audioStream = i;
aCodecCtx_audio = pFormatCtx->streams[audioStream]->codec;
pCodec_audio = avcodec_find_decoder(aCodecCtx_audio->codec_id);
if(pCodec_audio == NULL) {
    cerr<< "Unsupported codec!" << endl;
    return -1; // Codec not found
}

if(avcodec_open(aCodecCtx_audio, pCodec_audio) < 0) {
    cerr << "No se pudo abrir el codec de audio" << endl;
    return -1;
}

outbuf = (int16_t*) av_malloc(AVCODEC_MAX_AUDIO_FRAME_SIZE);

outfile = fopen(outfilename.c_str(), "wb");
if (!outfile) {
    exit(1);
}

av_init_packet(&packet);
packet.data = inbuf;
while(av_read_frame(pFormatCtx, &packet) == 0) {
    if(packet.stream_index == audioStream) {
        size = packet.size;
        if (size == 0) {
            cerr << "Size = 0 " << endl;
            break;
        }
        while(size > 0) {
            out_size = AVCODEC_MAX_AUDIO_FRAME_SIZE;
            len = avcodec_decode_audio3(aCodecCtx_audio, outbuf, &out_size , &packet);
            //av_free_packet(&packet);
            cout << len << endl;
            if(len == -1) {
                cerr << "Error while decoding" << endl;
                return 1;
            }
            if(out_size > 0) {
                fwrite(outbuf, 1, out_size, outfile);
            }
            size -= len;
            packet.data += len;
        }
    }
}
av_free_packet(&packet);


fclose(outfile);
free(outbuf);
cout << "END CODE" << endl;
avcodec_close(aCodecCtx_audio);
av_free(aCodecCtx_audio);

In the code I am trying to decode an .wav file to a .avi file. The output file ("salida.avi") is created , but does not play.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文