Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8907329
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T02:53:16+00:00 2026-06-15T02:53:16+00:00

I would like a simple working example of using just libavformat to mux video.

  • 0

I would like a simple working example of using just libavformat to mux video. There are nice examples (doc/examples/muxing.c) that show encoding with libavcodec, muxing with libavformat and saving the data with libavio. However, there is no example I know of that uses libavformat by itself, feeding in encoded data in a buffer and getting muxed data in a buffer.

The difficulty is two-fold: one, adding a stream with avformat_new_stream(AVFormatContext *s, const AVCodec *c) requires a reference to the codec; and two, the output from muxing is passed to AVFormatContext->pb which is an AVIOContext*. Thus there seems to be no (obvious) way to extricate libavformat from the other libav libraries.

See also: This question mentions a way to not use libavio: Get TS packets into buffer from libavformat

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T02:53:18+00:00Added an answer on June 15, 2026 at 2:53 am

    You can avoid dependencies on libavcodec library, but you need the header files (for example, avcodec.h).

    The scheme is as follows:

    AVOutputFormat * oFmt= ::av_guess_format("mp4", NULL, NULL);
    AVFormatContext *oFmtCtx = NULL;
    ::avformat_alloc_output_context2(&oFmtCtx, oFmt, NULL, NULL);
    AVStream * oStrm = ::avformat_new_stream(oFmtCtx, NULL);
    AVCodecContext * strmCodec = oFmtCtx->streams[0]->codec;
    
    // Fill the required properties for codec context.
    // *from the documentation:
    // *The user sets codec information, the muxer writes it to the output.
    // *Mandatory fields as specified in AVCodecContext
    // *documentation must be set even if this AVCodecContext is
    // *not actually used for encoding.
    my_tune_codec(strmCodec); 
    
    if (oFmtCtx->oformat->flags & AVFMT_NOFILE)
    {
      ::avio_open2(&oFmtCtx->pb, fileName, AVIO_FLAG_WRITE, NULL, NULL);
    }
    ::avformat_write_header(oFmtCtx, NULL);
    // .....
    // writing loop
    // .....
    ::av_write_trailer(oFmtCtx);
    ::avio_close(oFmtCtx->pb);
    ::avformat_free_context(oFmtCtx);
    

    To get the output, you always have to use the concept of AVIOContext. You can avoid using the built-in protocols. To do this you need to create your own AVIOContext (::avio_alloc_context).

    UPD
    To create your own AVIOContext, you have to do something like this

    #include <stdio.h>
    extern "C" {
    #include <libavformat/avio.h>
    #include <libavformat/avformat.h>
    }
    
    static const int kBufferSize = 32768;
    
    class my_iocontext_private
    {
    public:
        my_iocontext_private(FILE * f) : buffer_size_(kBufferSize),
            buffer_(static_cast<unsigned char*>(::av_malloc(buffer_size_))), f_(f) {
            ctx_ = ::avio_alloc_context(buffer_, buffer_size_, AVIO_FLAG_WRITE, this, 
                &my_iocontext_private::read, &my_iocontext_private::write, &my_iocontext_private::seek);
        }
    
        ~my_iocontext_private()    { av_free(buffer_); }
    
        static int read(void *opaque, unsigned char *buf, int buf_size) {
            my_iocontext_private* h = static_cast<my_iocontext_private*>(opaque);
            return fread(buf, 1, buf_size, h->f_);
        }
    
        static int write(void *opaque, unsigned char *buf, int buf_size) {
            my_iocontext_private* h = static_cast<my_iocontext_private*>(opaque);
            return fwrite(buf, 1, buf_size, h->f_);
        }
    
        static int64_t seek(void *opaque, int64_t offset, int whence) {
            my_iocontext_private* h = static_cast<my_iocontext_private*>(opaque);
    
            // use lseeki64 instead of fseek
            return fseek(h->f_, static_cast<long>(offset), whence);        
        }
        ::AVIOContext *get_avio() { return ctx_; }
    
    private:
        int buffer_size_;
        unsigned char * buffer_;  
        FILE * f_;
        ::AVIOContext * ctx_;
    };
    
    int main(int argc, char* argv[])
    {
        FILE * f = fopen("myfile.dmp", "wb");    
        my_iocontext_private priv_ctx(f); 
    
        AVFormatContext * ctx = ::avformat_alloc_context();
        ctx->pb = priv_ctx.get_avio();
    
        /// using ctx
    
        fclose(f); 
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i would like a simple help... i have a url like this: example.com/profile.php?id= &
I would like to get a working example in PHP that parses a SOAP
I am using Visual Studio 2010 Pro for simple C programming, I would like
I would like links to be trigger on double click. Something simple like this:
I am developing a site and i would like some simple markup. I would
I would like to create simple objects at runtime (textbox, label, etc) and add
I would like to make a simple notice / news system for website administrators.
I would like to create a simple fluid layout whereby 4 columns of Equal
I would like to implement a simple queueing service specific to a project. Where
I would like to implement a simple AR desktop application. This application should first

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.