peugas
peugas

Reputation: 191

FFmpeg not closing output file

I'm using FFmpeg to write video frames to a file.

So far I'm able to create an output file, grab my frames from a camera, encode the frames, write them and close the output file.

Everything works great except that I need to close my program for the output file to be available to be played. Closing the file is not enough.

If I don't close my program the output file shows as a 0KB file.

This is my output file closing code:

if (outContext)
{
    int ret = av_write_frame(outContext, NULL);
    ret = av_write_trailer(outContext);
    if (outContext->pb)
    {
        av_freep(&outContext->pb->buffer);
        av_freep(&outContext->pb);
        avio_close(outContext->pb);
    }
    avformat_free_context(outContext);
}

Am I forgetting to free/close anything?

Upvotes: 1

Views: 2052

Answers (1)

Edgar Rokjān
Edgar Rokjān

Reputation: 17483

I can name a few possible improvements:

1) It's not necessary to use this line of code:

int ret = av_write_frame(outContext, NULL);

because av_write_trailer flushes any buffered packets.

2) Also these two lines:

av_freep(&outContext->pb->buffer);
av_freep(&outContext->pb);

are redundant because they're partially duplicate avio_close functionality.

In fact, the most possible reason is that avio_close needs a valid pointer to AVIOContext as an argument. When you manually doing

av_freep(&outContext->pb);

outContext->pb becomes null and avio_close doesn't work properly.

Upvotes: 2

Related Questions