av_muxer.h 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. #ifndef __AV_MUXER_H__
  2. #define __AV_MUXER_H__
  3. #include "encoder/audio_encoder.h"
  4. #include "encoder/video_encoder.h"
  5. class AvMuxer {
  6. public:
  7. struct Info {
  8. MediaType type;
  9. AbstractEncoder* encoder = nullptr;
  10. AVStream* stream = nullptr;
  11. int streamIndex = -1;
  12. int fps = 30;
  13. uint64_t pts = 0;
  14. bool isEnd = false;
  15. bool isEncodeOverload = false;
  16. };
  17. ~AvMuxer()
  18. {
  19. Close();
  20. }
  21. bool Open(std::string_view filePath, std::string_view format = "mp4");
  22. bool WriteHeader();
  23. // 返回值为创建的流的索引 ,-1表示创建失败
  24. int AddVideoStream(const Encoder<MediaType::VIDEO>::Param& param);
  25. int AddAudioStream(const Encoder<MediaType::AUDIO>::Param& param);
  26. bool Write(AVFrame* frame, int streamIndex, bool isEnd = false);
  27. void Close();
  28. AVCodecContext* GetCodecCtx(int streamIndex);
  29. bool IsEncodeOverload() const;
  30. private:
  31. std::mutex _mtx;
  32. bool _isOpenFile = false;
  33. bool _AddStream(Info& info);
  34. bool _CheckTime(double time);
  35. std::vector<Info> _infos;
  36. AVFormatContext* _fmtCtx = nullptr;
  37. std::string _filePath;
  38. };
  39. #endif