首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如果不使用其他libav库,如何使用libavformat?

如果不使用其他libav库,如何使用libavformat?
EN

Stack Overflow用户
提问于 2012-11-30 04:54:41
回答 1查看 7.7K关注 0票数 4

我想要一个简单的工作例子,只使用libavformat多路复用视频。有一些很好的例子(doc/examples/muxing.c),展示了用libavcodec编码,用libavformat进行多路复用,以及用libavio保存数据。然而,据我所知,还没有使用libavformat本身的例子,在缓冲区中输入编码的数据,并在缓冲区中获取多路数据。

困难是双重的:第一,使用avformat_new_stream(AVFormatContext *s, const AVCodec *c)添加流需要对编解码器的引用;第二,多路复用的输出被传递给AVFormatContext->pb,它是一个AVIOContext*。因此,似乎没有(明显的)方法将libavformat从其他libav库中分离出来。

这个问题提到了一种不使用libavio的方法:Get TS packets into buffer from libavformat

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-11-30 17:36:04

您可以避免依赖libavcodec库,但您需要头文件(例如avcodec.h)。

该计划的内容如下:

代码语言:javascript
复制
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);

要获得输出,必须始终使用AVIOContext的概念。您可以避免使用内置协议。为此,您需要创建自己的AVIOContext (::avio_alloc_context)。

更新要创建您自己的AVIOContext,您必须执行以下操作

代码语言:javascript
复制
#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;
}
票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13634539

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档