gpt4 book ai didi

c - 使用 Libav 对视频的原始音频解码被切碎

转载 作者:行者123 更新时间:2023-12-04 04:05:45 28 4
gpt4 key购买 nike

我目前正在使用 libav 将视频的音频流提取到原始 PCM 文件中。

此代码适用于 mp3,但当我尝试使用 mp4 视频时,Audacity 上导入的原始格式显示奇怪的 0 到 -1 之间的规则下降线。

Audacity Waveform

这是我的实现。

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>

#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libswresample/swresample.h>

int decode_raw(AVFormatContext *format_ctx)
{
AVCodec *codec = NULL;
AVCodecContext* codec_ctx = NULL;
AVFrame* frame = NULL;
AVPacket packet;
int stream_idx = av_find_best_stream(format_ctx, AVMEDIA_TYPE_AUDIO, -1, -1, &codec, 0);
int res;

if (stream_idx < 0) {
printf("Could not find stream.\n");
return (1);
}

if ((codec_ctx = avcodec_alloc_context3(codec)) == NULL) {
printf("Could not allocate codec context.\n");
return (1);
}

if (avcodec_parameters_to_context(codec_ctx, format_ctx->streams[stream_idx]->codecpar) < 0) {
printf("Could not setup codec context parameters.\n");
return (1);
}

// Explicitly request non planar data.
codec_ctx->request_sample_fmt = av_get_packed_sample_fmt(codec_ctx->sample_fmt);

if (avcodec_open2(codec_ctx, codec, NULL) != 0) {
printf("Could not open codec.\n");
return (1);
}

if ((frame = av_frame_alloc()) == NULL) {
printf("Could not alloc frame.\n");
return (1);
}

av_init_packet(&packet);

int fd = open("raw", O_CREAT | O_WRONLY | O_TRUNC);

// Decode frames.
while ((res = av_read_frame(format_ctx, &packet)) == 0) {
// Does the packet belong to the correct stream?
if (packet.stream_index != stream_idx) {
av_packet_unref(&packet);
continue;
}

// We have a valid packet => send it to the decoder.
if ((res = avcodec_send_packet(codec_ctx, &packet)) != 0) {
printf("Failed to send packet: %d.\n", res);
break;
}

av_packet_unref(&packet);
res = avcodec_receive_frame(codec_ctx, frame);

if (res == AVERROR(EAGAIN) || res == AVERROR_EOF)
break;
else if (res < 0) {
printf("Failed to decode packet: %d.\n", res);
return (1);
}

write(fd, frame->extended_data[0], frame->linesize[0]);
}

close(fd);
av_frame_free(&frame);
avcodec_close(codec_ctx);
avcodec_free_context(&codec_ctx);
return (0);
}

int main(int argc, char **argv)
{
AVFormatContext *av_format_ctx = NULL;

if (argc != 2) {
printf("./streamer [file]\n");
return (1);
}

if (avformat_open_input(&av_format_ctx, argv[1], NULL, NULL) != 0) {
printf("Could not open input file.");
return (1);
}

if (avformat_find_stream_info(av_format_ctx, NULL) != 0) {
printf("Could not find stream information.");
return (1);
}

decode_raw(av_format_ctx);
avformat_close_input(&av_format_ctx);
return (0);
}

我尝试了什么

  • 检查字节顺序以及我是否在 Audacity 中正确导入了原始文件
  • 执行相应的ffmpeg命令ffmpeg -i video.mp4 -f f32le output.raw(我的代码输出AV_SAMPLE_FMT_FLT)比较两个文件。

我 hexdump 了这两个文件并找到了这个。

// 96 1f 03 3f - 22 03 0c 3f
// Doesn't exist in the output of my program?

5581a0 7c ad 6f bc 96 1f 03 3f 4f 01 25 3e 22 03 0c 3f |.o....?O.%>"..? // ffmpeg
5580d0 7c ad 6f bc 4f 01 25 3e 3a d2 89 3e 7c d7 9a 3e |.o.O.%>:..>|..> // my implementation

编辑 #1

在经历了无数次令人失望的经历之后,AAC 音频流在解码后似乎已损坏。但是,ffmpeg 的原始 PCM 输出适用于 MP4。

我尝试使用 swr_convert 对音频帧重新采样,但它的文档太少,我遇到了很多问题。

最佳答案

问题

打印音频流信息后。我注意到 AAC(mp4 文件的音频编解码器)不支持非平面格式(打包)。

// Explicitly request non planar data.
codec_ctx->request_sample_fmt = av_get_packed_sample_fmt(codec_ctx->sample_fmt);

由于不支持请求的格式,mp4 文件的音频流被解码为平面,这与 mp3 文件不同。

---------
Codec: MP3 (MPEG audio layer 3)
Supported sample formats: fltp, flt # MP3 support non planar
---------
Stream: 0
Sample Format: fltp
Sample Rate: 48000
Sample Size: 4
Channels: 2
Planar Output: yes

---------
Codec: AAC (Advanced Audio Coding)
Supported sample formats: fltp # AAC doesn't support non planar
---------
Stream: 1
Sample Format: fltp
Sample Rate: 44100
Sample Size: 4
Channels: 2
Planar Output: yes

解决方案

为了解决这个问题,我删除了上面的行以保持流平面。我还必须更改我在文件中的写入方式。

由于格式是平面 LR, LR, LR 而不是打包 LL LL RR RR,我不得不手动交替写入每个 channel 。

因为逐字节写入需要很长时间,所以我写了一个函数,先写入缓冲区,然后再将缓冲区写入文件。

void audio_pack_stream(AVCodecContext* codec_ctx, AVFrame *frame, uint8_t *dst, int *size)
{
int bytes = av_get_bytes_per_sample(codec_ctx->sample_fmt);
int actual = 0;

for (int i = 0; i < frame->nb_samples; i++) {
for(int j = 0; j < codec_ctx->channels; j++)
for (int k = 0; k < bytes; k++)
dst[*size++] = frame->extended_data[j][actual + k];
actual += bytes;
}
return (size);
}

// After avcodec_receive_frame

uint8_t output[4096 * 8];
int size;

audio_pack_stream(codec_ctx, frame, output, &size);
write(fd, output, size);

Fixed Audacity Waveform

关于c - 使用 Libav 对视频的原始音频解码被切碎,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62539816/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com