从MemoryStream播放视频,使用FFMpeg [英] Play a Video from MemoryStream, Using FFMpeg

查看:575
本文介绍了从MemoryStream播放视频,使用FFMpeg的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很困难,使用FFMpeg搜索如何使用TMemoryStream(或内存中类似的缓冲区)播放视频文件。我看过很多东西,其中包括UltraStarDX,Delphi等昂贵的FFMpeg组件。



一个组件叫做 FFMpeg Vcl Player 声称从内存流播放视频格式。我下载了试用版,我猜这是使用CircularBuffer.pas的(可能)。



有没有人知道如何做?



编辑:
现在更好的问题是如何播放加密的视频文件,使用FFMpeg或类似的库。

解决方案

要从内存流播放视频,您可以使用自定义 AVIOContext

  static const int kBufferSize = 4 * 1024; 

class my_iocontext_private
{
private:
my_iocontext_private(my_iocontext_private const&);
my_iocontext_private& operator =(my_iocontext_private const&);

public:
my_iocontext_private(IInputStreamPtr inputStream)
:inputStream_(inputStream)
,buffer_size_(kBufferSize)
,buffer_(static_cast< unsigned char *> ;(:: av_malloc(buffer_size_))){
ctx_ = :: avio_alloc_context(buffer_,buffer_size_,0,this,
& my_iocontext_private :: read,NULL,& my_iocontext_private :: seek);
}

〜my_iocontext_private(){
:: av_free(ctx_);
:: av_free(buffer_);
}

void reset_inner_context(){ctx_ = NULL; buffer_ = NULL; }

static int read(void * opaque,unsigned char * buf,int buf_size){
my_iocontext_private * h = static_cast< my_iocontext_private *>(opaque)
return h-> inputStream _-> Read(buf,buf_size);
}

static int64_t seek(void * opaque,int64_t offset,int whence){
my_iocontext_private * h = static_cast< my_iocontext_private *>(opaque)

if(0x10000 == whence)
return h-> inputStream _-> Size();

return h-> inputStream _-> Seek(offset,whichce);
}

:: AVIOContext * get_avio(){return ctx_; }

private:
IInputStreamPtr inputStream_; //抽象流接口,你可以调整它到TMemoryStream
int buffer_size_;
unsigned char * buffer_;
:: AVIOContext * ctx_;
};


//// ..........

///准备输入流:
IInputStreamPtr inputStream = MyCustomCreateInputStreamFromMemory( );

my_iocontext_private priv_ctx(inputStream);
AVFormatContext * ctx = :: avformat_alloc_context();
ctx-> pb = priv_ctx.get_avio();

int err = avformat_open_input(& ctx,whatevertext,NULL,NULL);
if(err< 0)
return -1;

////正常使用ctx
//// avformat_find_stream_info(ctx,NULL);
//// av_read_frame(ctx,& pkt);
//// etc ..


I'm having a hard time, searching how to play a video file from a TMemoryStream (or a similar buffer in memory) using FFMpeg. I've seen many things, including UltraStarDX, expensive FFMpeg components for Delphi and so on.

One component called FFMpeg Vcl Player claims to play video formats from a memory stream. I downloaded the trial version and I guess it uses CircularBuffer.pas for that matter (maybe).

Does any one know how to do this?

Edit: Now the better question is how to play an encrypted video file, using FFMpeg or similar libraries.

解决方案

To play video from memory stream, you can use custom AVIOContext.

static const int kBufferSize = 4 * 1024;

class my_iocontext_private
{
private:
    my_iocontext_private(my_iocontext_private const &);
    my_iocontext_private& operator = (my_iocontext_private const &);

public:
    my_iocontext_private(IInputStreamPtr inputStream)
       : inputStream_(inputStream)
       , buffer_size_(kBufferSize)
       , buffer_(static_cast<unsigned char*>(::av_malloc(buffer_size_))) {
    ctx_ = ::avio_alloc_context(buffer_, buffer_size_, 0, this,
           &my_iocontext_private::read, NULL, &my_iocontext_private::seek); 
    }

    ~my_iocontext_private() { 
        ::av_free(ctx_);
        ::av_free(buffer_); 
    }

    void reset_inner_context() { ctx_ = NULL; buffer_ = NULL; }

    static int read(void *opaque, unsigned char *buf, int buf_size) {
        my_iocontext_private* h = static_cast<my_iocontext_private*>(opaque);
        return h->inputStream_->Read(buf, buf_size); 
    }

    static int64_t seek(void *opaque, int64_t offset, int whence) {
        my_iocontext_private* h = static_cast<my_iocontext_private*>(opaque);

        if (0x10000 == whence)
            return h->inputStream_->Size();

        return h->inputStream_->Seek(offset, whence); 
    }

    ::AVIOContext *get_avio() { return ctx_; }

    private:
       IInputStreamPtr inputStream_; // abstract stream interface, You can adapt it to TMemoryStream  
       int buffer_size_;
       unsigned char * buffer_;  
       ::AVIOContext * ctx_;
   };


   //// ..........

   /// prepare input stream:
   IInputStreamPtr inputStream = MyCustomCreateInputStreamFromMemory();

   my_iocontext_private priv_ctx(inputStream);
   AVFormatContext * ctx = ::avformat_alloc_context();
   ctx->pb = priv_ctx.get_avio();

   int err = avformat_open_input(&ctx, "arbitrarytext", NULL, NULL);
   if (err < 0) 
       return -1;

   //// normal usage of ctx
   //// avformat_find_stream_info(ctx, NULL);
   //// av_read_frame(ctx, &pkt); 
   //// etc..

这篇关于从MemoryStream播放视频,使用FFMpeg的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆