[英]Encoding audio data using ffmpeg
我收到一個字節數組( int8_t*
),我想使用FFMPEG將其編碼為FLAC。 我發現的所有示例都是從文件讀取數據,對我而言並非如此。 在原始文檔(請參閱此處 )之后,我提出了以下解決方案:
#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;
}
上述解決方案存在兩個主要問題:
我沒有指定最終的編碼格式(例如MP3,FLAC等),這使我想知道是否正確使用了FFMPEG庫?
我是否擁有有關來源的所有必要信息-原始音頻數據? 我不確定我是否具有執行編碼所需的所有必要信息。
您快到了,請按照以下示例操作: 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)
並相應地檢查/修復其他值。
至於第二個……我相信您會發現自己的,尤其是必須正確設置此設置(第158行) c->sample_fmt = AV_SAMPLE_FMT_S16
根據您的int8_t
數組格式化。
希望能有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.