av_muxer.h 1.2 KB

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