delphi – 从MemoryStream播放视频,使用FFMpeg

前端之家收集整理的这篇文章主要介绍了delphi – 从MemoryStream播放视频,使用FFMpeg前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我很困难,使用FFMpeg搜索如何从TMemoryStream(或内存中的类似缓冲区)播放视频文件.我看到很多东西,包括UltraStarDX,Delphi等昂贵的FFMpeg组件.

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

有谁知道如何做到这一点?

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

解决方法

要从内存流播放视频,您可以使用自定义AVIOContext.
  1. static const int kBufferSize = 4 * 1024;
  2.  
  3. class my_iocontext_private
  4. {
  5. private:
  6. my_iocontext_private(my_iocontext_private const &);
  7. my_iocontext_private& operator = (my_iocontext_private const &);
  8.  
  9. public:
  10. my_iocontext_private(IInputStreamPtr inputStream)
  11. : inputStream_(inputStream),buffer_size_(kBufferSize),buffer_(static_cast<unsigned char*>(::av_malloc(buffer_size_))) {
  12. ctx_ = ::avio_alloc_context(buffer_,buffer_size_,this,&my_iocontext_private::read,NULL,&my_iocontext_private::seek);
  13. }
  14.  
  15. ~my_iocontext_private() {
  16. ::av_free(ctx_);
  17. ::av_free(buffer_);
  18. }
  19.  
  20. void reset_inner_context() { ctx_ = NULL; buffer_ = NULL; }
  21.  
  22. static int read(void *opaque,unsigned char *buf,int buf_size) {
  23. my_iocontext_private* h = static_cast<my_iocontext_private*>(opaque);
  24. return h->inputStream_->Read(buf,buf_size);
  25. }
  26.  
  27. static int64_t seek(void *opaque,int64_t offset,int whence) {
  28. my_iocontext_private* h = static_cast<my_iocontext_private*>(opaque);
  29.  
  30. if (0x10000 == whence)
  31. return h->inputStream_->Size();
  32.  
  33. return h->inputStream_->Seek(offset,whence);
  34. }
  35.  
  36. ::AVIOContext *get_avio() { return ctx_; }
  37.  
  38. private:
  39. IInputStreamPtr inputStream_; // abstract stream interface,You can adapt it to TMemoryStream
  40. int buffer_size_;
  41. unsigned char * buffer_;
  42. ::AVIOContext * ctx_;
  43. };
  44.  
  45.  
  46. //// ..........
  47.  
  48. /// prepare input stream:
  49. IInputStreamPtr inputStream = MyCustomCreateInputStreamFromMemory();
  50.  
  51. my_iocontext_private priv_ctx(inputStream);
  52. AVFormatContext * ctx = ::avformat_alloc_context();
  53. ctx->pb = priv_ctx.get_avio();
  54.  
  55. int err = avformat_open_input(&ctx,"arbitrarytext",NULL);
  56. if (err < 0)
  57. return -1;
  58.  
  59. //// normal usage of ctx
  60. //// avformat_find_stream_info(ctx,NULL);
  61. //// av_read_frame(ctx,&pkt);
  62. //// etc..

猜你在找的Delphi相关文章