c++-ffmpeg如何从mp3文件中提取音频数据?
发布时间:2022-05-10 04:52:33 224
相关标签: # golang
在ffmpeg文档中,给出了mp2解码的示例。我尝试将此应用于mp3:
#define SOURCE_FILE "ignore/audio01.mp3"
#define TARGET_FILE "ignore/target-audio01.pcm"
#define AUDIO_INBUF_SIZE 20480
#define AUDIO_REFILL_THRESH 4096
#define av_perr(errnum) \
char av_err_buff[AV_ERROR_MAX_STRING_SIZE]; \
av_strerror(errnum, (char *) &av_err_buff, AV_ERROR_MAX_STRING_SIZE); \
fprintf(stderr, "\033[91m%s\033[0m\n", av_err_buff);
static int decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile) {
int ret, i, j;
int data_size;
ret = avcodec_send_packet(dec_ctx, pkt);
if (ret < 0) {
av_perr(ret);
return EXIT_FAILURE;
}
while (ret >= 0) {
ret = avcodec_receive_frame(dec_ctx, frame);
if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
break;
} else if (ret < 0) {
av_perr(ret);
return EXIT_FAILURE;
}
data_size = av_get_bytes_per_sample(dec_ctx->sample_fmt);
if (data_size < 0) {
av_perr(data_size);
return EXIT_FAILURE;
}
for (i = 0; i < frame->nb_samples; i++) {
for (j = 0; j < dec_ctx->channels; j++) {
fwrite(frame->data[j] + data_size * i, 1, data_size, outfile);
}
}
}
return EXIT_SUCCESS;
}
#define IS_NULL_PTR(ptr, message) \
if (!ptr) { \
fprintf(stderr, "\033[91m%s\033[0m\n", message); \
goto FINALLY; \
}
#define av_perr(errnum) \
char av_err_buff[AV_ERROR_MAX_STRING_SIZE]; \
av_strerror(errnum, (char *) &av_err_buff, AV_ERROR_MAX_STRING_SIZE); \
fprintf(stderr, "\033[91m%s\033[0m\n", av_err_buff);
int main(int argc, char **argv) {
const AVCodec *codec;
AVCodecContext *c = nullptr;
AVCodecParserContext *parser = nullptr;
enum AVSampleFormat sfmt;
int ret = 0, len = 0, n_channels = 0;
FILE *source_file = nullptr, *target_file = nullptr;
const char *fmt = nullptr;
uint8_t inbuf[AUDIO_INBUF_SIZE + AV_INPUT_BUFFER_PADDING_SIZE];
uint8_t *data = nullptr;
size_t data_size = 0;
AVPacket *pkt = nullptr;
AVFrame *decode_frame = nullptr;
...
data = inbuf;
data_size = fread(inbuf, 1, AUDIO_INBUF_SIZE, source_file);
while (data_size > 0) {
ret = av_parser_parse2(parser, c, &pkt->data, &pkt->size, data, data_size, AV_NOPTS_VALUE, AV_NOPTS_VALUE, 0);
if (ret < 0) {
fprintf(stderr, "\033[91mError while parsing\033[0m\n");
goto FINALLY;
}
data += ret;
data_size -= ret;
if (pkt->size && decode(c, pkt, decode_frame, target_file)) goto FINALLY;
if (data_size < AUDIO_REFILL_THRESH) {
memmove(inbuf, data, data_size);
data = inbuf;
len = fread(data + data_size, 1, AUDIO_INBUF_SIZE - data_size, source_file);
if (len > 0) data_size += len;
}
}
...
}
我发现了以下错误:
[mp3float @ 0x55c51ac63440] Header missing
Invalid data found when processing input
以下是ffmpeg库的版本:
libavutil 57. 17.100 / 57. 17.100
libavcodec 59. 18.100 / 59. 18.100
libavformat 59. 16.100 / 59. 16.100
libavdevice 59. 4.100 / 59. 4.100
libavfilter 8. 24.100 / 8. 24.100
libswscale 6. 4.100 / 6. 4.100
libswresample 4. 3.100 / 4. 3.100
libpostproc 56. 3.100 / 56. 3.100
我想这是因为mp3和mp2帧的数据格式存在根本性差异,但我找不到解码mp3的方法。
mp3和mp2音频格式的本质区别是什么?另外,我能做些什么来正确处理mp3音频?
特别声明:以上内容(图片及文字)均为互联网收集或者用户上传发布,本站仅提供信息存储服务!如有侵权或有涉及法律问题请联系我们。
举报