ffmpeg - libavformat/libavcodec 提供无效的容器 header

标签 ffmpeg mp4 libavcodec libavformat libx264

我正在使用 libavcodec 将流编码为 h264 并使用 libavformat 将其存储在 mp4 中。生成的容器有一个无效的 header ,可以在 VLC 中播放,但不能在任何其他播放器中播放。

我发现使用 mp4 容器和“mpeg4”编解码器会产生有效的 mp4 文件,但使用 libx265 (HEVC) 或 libx264 编解码器会产生无效的 mp4。

我可以使用 ffmpeg -i invalid.mp4 -vcodec copy valid.mp4我得到一个几乎完全相同大小的文件,但在一个有效的容器中。

这些文件的示例在这里:Broken file
Repaied file [使用右上角的下载链接查看]

我使用十六进制编辑器查看了两个文件标题的差异,无效文件比有效文件小 1 个字节。

我用来打开容器和编解码器并编写标题的代码在这里:

AVOutputFormat *container_format;
AVFormatContext *container_format_context;
AVStream *video_stream;
int ret;

/* allocate the output media context */
avformat_alloc_output_context2(&container_format_context, NULL, NULL, out_file);
if (!container_format_context) {
    log(INFO, "Unable to determine container format from filename, exiting\n");
    exit(1);
}
else {
    log(INFO, "Using container %s\n", container_format_context->oformat->name);
}

if (!container_format_context) {
    log(ERROR, "Could not build container format context. Encoding failed.");
    exit(1);
}

container_format = container_format_context->oformat;

/* Pull codec based on name */
AVCodec* codec = avcodec_find_encoder_by_name(codec_name);
if (codec == NULL) {
    log(ERROR, "Failed to locate codec \"%s\".",
            codec_name);
    exit(1);
}

/* create stream */
video_stream = NULL;
video_stream = avformat_new_stream(container_format_context, codec);
if (!video_stream) {
    log(ERROR, "Could not allocate encoder stream. Cannot continue.\n");
    exit(1);
}
video_stream->id = container_format_context->nb_streams - 1;

video_stream->time_base = video_stream->codec->time_base = (AVRational) { 1, 25};

av_dump_format(container_format_context, 0, out_file, 1);



/* Retrieve encoding context */
AVCodecContext* avcodec_context = video_stream->codec;
if (avcodec_context == NULL) {
    log(ERROR, "Failed to allocate context for "
            "codec \"%s\".", codec_name);
    exit(1);
}


/* Init context with encoding parameters */
avcodec_context->bit_rate = bitrate;
avcodec_context->width = width;
avcodec_context->height = height;
avcodec_context->gop_size = 10;
avcodec_context->max_b_frames = 1;
avcodec_context->qmax = 31;
avcodec_context->qmin = 2;
avcodec_context->pix_fmt = AV_PIX_FMT_YUV420P;

av_dump_format(container_format_context, 0, out_file, 1);

/* Open codec for use */
if (avcodec_open2(avcodec_context, codec, NULL) < 0) {
    log(ERROR, "Failed to open codec \"%s\".", codec_name);
    exit(1);
}

/* Allocate corresponding frame */
AVFrame* frame = av_frame_alloc();
if (frame == NULL) {
    exit(1);
}

/* Copy necessary data for frame from avcodec_context */
frame->format = avcodec_context->pix_fmt;
frame->width = avcodec_context->width;
frame->height = avcodec_context->height;

/* Allocate actual backing data for frame */
if (av_image_alloc(frame->data, frame->linesize, frame->width,
            frame->height, frame->format, 32) < 0) {
    exit(1);
}

/* open the output file, if the container needs it */
if (!(container_format->flags & AVFMT_NOFILE)) {
    ret = avio_open(&container_format_context->pb, out_file, AVIO_FLAG_WRITE);
    if (ret < 0) {
        log(ERROR, "Error occurred while opening output file: %s\n",
                av_err2str(ret));
        exit(1);
    }
}

/* write the stream header, if needed */
ret = avformat_write_header(container_format_context, NULL);
if (ret < 0) {
    log(ERROR, "Error occurred while writing output file header: %s\n",
                av_err2str(ret));
}

编码帧的代码在这里:
/* Init video packet */
AVPacket packet;
av_init_packet(&packet);

/* Request that encoder allocate data for packet */
packet.data = NULL;
packet.size = 0;

/* Write frame to video */
int got_data;
if (avcodec_encode_video2(avcontext, &packet, frame, &got_data) < 0) {
    log(WARNING, "Error encoding frame #%" PRId64,
            video_struct->next_pts);
    return -1;
}

/* Write corresponding data to file */
if (got_data) {
    if (packet.pts != AV_NOPTS_VALUE) {
        packet.pts = av_rescale_q(packet.pts, video_struct->output_stream->codec->time_base, video_struct->output_stream->time_base);
    }
    if (packet.dts != AV_NOPTS_VALUE) {
        packet.dts = av_rescale_q(packet.dts, video_struct->output_stream->codec->time_base, video_struct->output_stream->time_base);
    }
    write_packet(video_struct, &packet, packet.size);
    av_packet_unref(&packet);
}

以及将数据包写入视频流的代码:
static int write_packet(video_struct* video, void* data, int size) {

int ret;

/* use AVStream is not null, otherwise write to output fd */
AVPacket *pkt = (AVPacket*) data;
pkt->stream_index = video->output_stream->index;
ret = av_interleaved_write_frame(video->container_format_context, pkt);
if (ret != 0) {
    return -1;
}

/* Data was written successfully */
return ret;
}

最佳答案

解决了这个问题。问题是如果容器需要,我没有将全局 header 分配​​给容器。在将高度、宽度、比特率等属性分配给 avcodec_context 时, 我加了

if (container_format_context->oformat->flags & AVFMT_GLOBALHEADER) {
    avcodec_context->flags |= CODEC_FLAG_GLOBAL_HEADER;
}

这似乎解决了这个问题。

关于ffmpeg - libavformat/libavcodec 提供无效的容器 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47802902/

相关文章:

ffmpeg 旋转图像以馈送 ffserver

delphi - 使用 FFMpeg 播放 MemoryStream 中的视频

android - WhatsApp 视频作为 Gif 以编程方式在 Android 上共享

python - 如何用 python 打开 MP4 视频文件?

c - 使用 libavcodec 将音频编码为 aac

c++ - x264/avcodec : Deduce frame sequence number from NAL unit

windows - ffmpeg libx264 AVCodecContext 设置

python - 如何在 Anaconda 中使用 Python 的框架构建

ios - 如何将sps pps(从mp4文件读取)信息添加到每个IDR帧

php - FFMPEG 不创建缩略图和转换视频