【问题标题】:Encoding audio data using ffmpeg使用 ffmpeg 编码音频数据
【发布时间】:2019-05-30 15:08:23
【问题描述】:

我收到一个字节数组 (int8_t*),我想使用 FFMPEG 将其编码为 FLAC。我发现的所有示例都是从文件中读取数据,但对我来说并非如此。根据原始文档(见here),我想出了以下解决方案:

#include "libavcodec/avcodec.h"

// ...

// params:
//  audioData: original audio data
//  len: length of the byte array (audio data)
//  sampleRate: sample rate of the original audio data
//  frameSize: frame size of the original data
uint8_t* encodeToFlac(uint8_t* audioData, int len, int sampleRate, int frameSize) {
  uint8_t* convertedAudioData;

  // Context information
  AVCodecContext* context = avcodec_alloc_context();
  context->bit_rate = 64000;
  context->sample_rate = sampleRate;
  context->channels = 2;
  context->frame_size = frameSize;

  short* samples = malloc(frameSize * 2 * context->channels);
  int outAudioDataSize = len * 2;
  convertedAudioData = malloc(outAudioDataSize);
  int outSize = avcodec_encode_audio(c, convertedAudioData, outAudioDataSize, samples);

  return convertedAudioData;
}

我对上述解决方案有两个主要问题:

  1. 我没有指定最终的编码应该是什么(例如,MP3、FLAC 等),这让我怀疑我是否正确使用了 FFMPEG 库?

  2. 我是否拥有有关源的所有必要信息 - 原始音频数据?我不确定我是否拥有执行编码所需的所有信息。

【问题讨论】:

  • 直接使用 libflac 会容易得多。
  • 它不是 ffmpeg 的一部分,是吗?恐怕我除了 ffmpeg 没有其他东西可以使用。
  • I'm afraid I do not have anything other than ffmpeg to use你可以下载。
  • 是的,但不是这个项目,我不能。

标签: c++ ffmpeg


【解决方案1】:

你就快到了,按照这个例子:https://github.com/FFmpeg/FFmpeg/blob/master/doc/examples/encode_audio.c

第一个问题的答案: 在那里你会看到codec = avcodec_find_encoder(AV_CODEC_ID_MP2)。
在您的情况下,您猜对了,它可能是 codec = avcodec_find_encoder(AV_CODEC_ID_FLAC) 并相应地检查/修复其他值。

至于第二个......我相信你会发现自己,特别是你必须根据你的int8_t数组格式正确设置它(第158行)c->sample_fmt = AV_SAMPLE_FMT_S16。

希望对您有所帮助。

【讨论】:

    猜你喜欢
    • 2016-09-05
    • 1970-01-01
    • 1970-01-01
    • 2015-05-21
    • 2021-04-20
    • 2017-07-12
    • 1970-01-01
    • 1970-01-01
    • 2013-03-03
    相关资源
    最近更新 更多