如何在C ++中通过FFmpeg将原始文件转换为音频文件(.wav)

穆罕默德·雷扎·喀什

我想在我的C ++应用程序中以不同的采样率将mp4格式转换为wav格式。首先,我在c ++中通过ffmpeg从mp4文件中提取了音频,然后将其转换为原始文件,但是我不知道如何将原始文件转换为具有不同采样率的wav文件。我该如何解决?

 #include "ffmpeg.h"
    int decode_packet(int *got_frame, int cached)
    {
        int ret = 0;
        int decoded = pkt.size;
        *got_frame = 0;
        if (pkt.stream_index == video_stream_idx) {
            /* decode video frame */
            ret = avcodec_decode_video2(video_dec_ctx, frame, got_frame, &pkt);
            if (ret < 0) {
    //            fprintf(stderr, "Error decoding video frame (%s)\n", av_err2str(ret));
                return ret;
            }
            if (*got_frame) {
                if (frame->width != width || frame->height != height ||
                    frame->format != pix_fmt) {
                    /* To handle this change, one could call av_image_alloc again and
                     * decode the following frames into another rawvideo file. */
    //                fprintf(stderr, "Error: Width, height and pixel format have to be "
    //                        "constant in a rawvideo file, but the width, height or "
    //                        "pixel format of the input video changed:\n"
    //                        "old: width = %d, height = %d, format = %s\n"
    //                        "new: width = %d, height = %d, format = %s\n",
    //                        width, height, av_get_pix_fmt_name(pix_fmt),
    //                        frame->width, frame->height,
    //                        av_get_pix_fmt_name(frame->format));
                    return -1;
                }
                printf("video_frame%s n:%d coded_n:%d\n",
                       cached ? "(cached)" : "",
                       video_frame_count++, frame->coded_picture_number);
                /* copy decoded frame to destination buffer:
                 * this is required since rawvideo expects non aligned data */
                av_image_copy(video_dst_data, video_dst_linesize,
                              (const uint8_t **)(frame->data), frame->linesize,
                              pix_fmt, width, height);
                /* write to rawvideo file */
                fwrite(video_dst_data[0], 1, video_dst_bufsize, video_dst_file);
            }
        } else if (pkt.stream_index == audio_stream_idx) {
            /* decode audio frame */
            ret = avcodec_decode_audio4(audio_dec_ctx, frame, got_frame, &pkt);
            if (ret < 0) {
    //            fprintf(stderr, "Error decoding audio frame (%s)\n", av_err2str(ret));
                return ret;
            }
            /* Some audio decoders decode only part of the packet, and have to be
             * called again with the remainder of the packet data.
             * Sample: fate-suite/lossless-audio/luckynight-partial.shn
             * Also, some decoders might over-read the packet. */
            decoded = FFMIN(ret, pkt.size);
            if (*got_frame) {
                size_t unpadded_linesize = frame->nb_samples * av_get_bytes_per_sample((AVSampleFormat)frame->format);
    //            printf("audio_frame%s n:%d nb_samples:%d pts:%s\n",
    //                   cached ? "(cached)" : "",
    //                   audio_frame_count++, frame->nb_samples,
    //                   av_ts2timestr(frame->pts, &audio_dec_ctx->time_base));
                /* Write the raw audio data samples of the first plane. This works
                 * fine for packed formats (e.g. AV_SAMPLE_FMT_S16). However,
                 * most audio decoders output planar audio, which uses a separate
                 * plane of audio samples for each channel (e.g. AV_SAMPLE_FMT_S16P).
                 * In other words, this code will write only the first audio channel
                 * in these cases.
                 * You should use libswresample or libavfilter to convert the frame
                 * to packed data. */
    //            fwrite(frame->extended_data[0], 1, unpadded_linesize, audio_dst_file);
                //encode function
                encode(cOut, frame, &pktout, audio_dst_file);


    //            av_init_packet(&pktout);
    //            pktout.data = NULL; // packet data will be allocated by the encoder
    //            pktout.size = 0;
    //            /* encode the samples */
    //            ret = avcodec_encode_audio2(cOut, &pktout, frame, &got_outputOut);
    //            if (ret < 0) {
    //                fprintf(stderr, "Error encoding audio frame\n");
    //                exit(1);
    //            }
    //            if (got_outputOut) {
    //                fwrite(pktout.data, 1, pktout.size, audio_dst_file);
    //                av_free_packet(&pktout);
    //            }

            }
        }
        /* If we use frame reference counting, we own the data and need
         * to de-reference it when we don't use it anymore */
        if (*got_frame && refcount)
            av_frame_unref(frame);
        return decoded;
    }
陈柏霖

首先,您应该使用Libswresample对音频数据进行重新采样。

然后,您可以将音频原始数据保存为wav格式。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何将G.723.1类型的音频文件转换为WAV格式?

来自分类Dev

使用ffmpeg javascript将wav音频文件转换为pcm音频文件

来自分类Dev

使用ffmpeg javascript将wav音频文件转换为pcm音频文件

来自分类Dev

如何在JavaScript或PHP中将WAV音频文件转换为FLAC格式

来自分类Dev

如何从WAV文件中获取原始音频?

来自分类Dev

如何从WAV文件中获取原始音频?

来自分类Dev

如何在不实际转换的情况下确定转换后的音频文件的理论文件大小(即,将mp3转换为wav)

来自分类Dev

如何使用ffmpeg将原始PCM数据转换为有效的WAV文件?

来自分类Dev

如何在静音段将音频文件(mp3或wav)划分为较小的文件

来自分类Dev

如何从文件夹将音频文件拆分为多个音频WAV文件

来自分类Dev

如何在C ++中分割WAV文件?

来自分类Dev

如何将 wav 文件转换为 JSON 文件

来自分类Dev

在iOS中将录制的音频文件转换为.wav格式

来自分类Dev

在iOS中将录制的音频文件转换为.wav格式

来自分类Dev

将音频文件转换为WAV文件时,“必须已经是浮点数”

来自分类Dev

如何将AAC(.m4a)音频文件解码为WAV?

来自分类Dev

如何在ffmpeg中合并视频flv和音频wav文件

来自分类Dev

使用LAME将音频文件从.wav转换为MP3时出现问题

来自分类Dev

如何使用Pocketsphinx将.wav文件转换为文本?

来自分类Dev

如何将频谱图矩阵转换为WAV文件

来自分类Dev

如何在Perl中使用HTTP POST接收wav音频文件并在Asterisk AGI中播放

来自分类Dev

如何在python中将音频文件(wav格式)拼接为1秒的拼接?

来自分类Dev

如何使用Python在.Wav中转换音频的原始数据文件

来自分类Dev

如何在GitHub上使用此代码POVoiceHUD将.caf文件转换为.wav或.wma文件

来自分类Dev

复制WAV文件C ++

来自分类Dev

如何从WAV文件创建音频ISO文件?

来自分类Dev

如何从WAV文件创建音频ISO文件?

来自分类Dev

如何将多个WAV / FLAC /音频CD合并到一个主大音频文件(> 4GB)中

来自分类Dev

如何播放音频文件(.mp3、.flac、.wav),然后使用 ffmpeg 循环播放(每隔几秒混合一次)另一个音频文件(wav)

Related 相关文章

  1. 1

    如何将G.723.1类型的音频文件转换为WAV格式?

  2. 2

    使用ffmpeg javascript将wav音频文件转换为pcm音频文件

  3. 3

    使用ffmpeg javascript将wav音频文件转换为pcm音频文件

  4. 4

    如何在JavaScript或PHP中将WAV音频文件转换为FLAC格式

  5. 5

    如何从WAV文件中获取原始音频?

  6. 6

    如何从WAV文件中获取原始音频?

  7. 7

    如何在不实际转换的情况下确定转换后的音频文件的理论文件大小(即,将mp3转换为wav)

  8. 8

    如何使用ffmpeg将原始PCM数据转换为有效的WAV文件?

  9. 9

    如何在静音段将音频文件(mp3或wav)划分为较小的文件

  10. 10

    如何从文件夹将音频文件拆分为多个音频WAV文件

  11. 11

    如何在C ++中分割WAV文件?

  12. 12

    如何将 wav 文件转换为 JSON 文件

  13. 13

    在iOS中将录制的音频文件转换为.wav格式

  14. 14

    在iOS中将录制的音频文件转换为.wav格式

  15. 15

    将音频文件转换为WAV文件时,“必须已经是浮点数”

  16. 16

    如何将AAC(.m4a)音频文件解码为WAV?

  17. 17

    如何在ffmpeg中合并视频flv和音频wav文件

  18. 18

    使用LAME将音频文件从.wav转换为MP3时出现问题

  19. 19

    如何使用Pocketsphinx将.wav文件转换为文本?

  20. 20

    如何将频谱图矩阵转换为WAV文件

  21. 21

    如何在Perl中使用HTTP POST接收wav音频文件并在Asterisk AGI中播放

  22. 22

    如何在python中将音频文件(wav格式)拼接为1秒的拼接?

  23. 23

    如何使用Python在.Wav中转换音频的原始数据文件

  24. 24

    如何在GitHub上使用此代码POVoiceHUD将.caf文件转换为.wav或.wma文件

  25. 25

    复制WAV文件C ++

  26. 26

    如何从WAV文件创建音频ISO文件?

  27. 27

    如何从WAV文件创建音频ISO文件?

  28. 28

    如何将多个WAV / FLAC /音频CD合并到一个主大音频文件(> 4GB)中

  29. 29

    如何播放音频文件(.mp3、.flac、.wav),然后使用 ffmpeg 循环播放(每隔几秒混合一次)另一个音频文件(wav)

热门标签

归档