Search code examples
ffmpeglibavcodeclibavformat

libavformat produces flv files without size while encoding


I'm encoding a video with libavcodec and libavformat. I'm using libx264 as the codec and flv as the container. I chose flv because I wanted to be able to play and cut pieces of a video while it is encoding and flv is supposed to support this behavior.

When I attempt to cut a part (first 5 seconds) of the video with ffmpeg while it is still encoding, ffmpeg produces an error:

$# ffmpeg -i file.flv -t 00:00:05.000 -c copy test.mp4
[flv @ 0x67ecb00] Could not find codec parameters for stream 0 (Video: h264, none, 2000 kb/s): unspecified size

If I wait until the file has finished encoding and run the same ffmpeg command, there is no error. In fact, ffprobe shows the following for the video:

Video: h264 (High), yuv420p, 640x480, 2000 kb/s, 25 fps, 25 tbr, 1k tbn, 50 tbc

The file is definitely more than 5 seconds long when I attempt this.

The code I'm using to do this is based heavily on the muxing.c example from libavformat. Here's a trimmed version:

AVOutputFormat *container_format;
AVFormatContext *container_format_context;
AVStream *video_stream;
char *path = "/home/user/temp.flv";

/* allocate the output media context */
avformat_alloc_output_context2(&container_format_context, NULL, NULL, path);
container_format = container_format_context->oformat;

AVCodec* codec = NULL;
/* Pull codec based on name */
codec = avcodec_find_encoder_by_name("libx264");

/*create stream */
video_stream = NULL;
video_stream = avformat_new_stream(container_format_context, codec);

video_stream->id = container_format_context->nb_streams - 1;

video_stream->codec->bit_rate = bitrate;
video_stream->codec->width = 640;
video_stream->codec->height = 480;
video_stream->codec->gop_size = 10;
video_stream->codec->qmax = 31;
video_stream->codec->qmin = 2;
video_stream->codec->pix_fmt = AV_PIX_FMT_YUV420P;
video_stream->codec->time_base = (AVRational) { 1, 25 };

AVCodecContext *avcodec_context = video_stream->codec;

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

/* open codec */
guacenc_open_avcodec(avcodec_context, codec, NULL, video_stream);

/* Allocate corresponding frame */
AVFrame* frame = av_frame_alloc();

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

if (!(container_format->flags & AVFMT_NOFILE)) {
    avio_open(&container_format_context->pb, path, AVIO_FLAG_WRITE);
}

avformat_write_header(container_format_context, NULL);

How can I get the size information to the front of the flv file so that it can be trimmed while still encoding?


Solution

  • It seems like this is an issue with libavformat. I was using 2.8, which was showing this issue. I compiled against 3.4 and the issue no longer was happening. So I'm going to use 3.4 in my application to fix this.

    I don't know when the fix was committed to libavformat, but I think it might have been at least started to be fixed with this commit: https://git.ffmpeg.org/gitweb/ffmpeg.git/commitdiff/5702416c57afb2bb062eb69d60fc42c31c91b674