demuxer.cpp 873 B

123456789101112131415161718192021222324252627282930313233
  1. #include "demuxer.h"
  2. #include "ffmpeg.h"
  3. Demuxer::Demuxer(const std::string &file_name) {
  4. av_register_all();
  5. ffmpeg::check(avformat_open_input(
  6. &format_context_, file_name.c_str(), nullptr, nullptr));
  7. ffmpeg::check(avformat_find_stream_info(
  8. format_context_, nullptr));
  9. video_stream_index_ = ffmpeg::check(av_find_best_stream(
  10. format_context_, AVMEDIA_TYPE_VIDEO, -1, -1, nullptr, 0));
  11. }
  12. Demuxer::~Demuxer() {
  13. avformat_close_input(&format_context_);
  14. }
  15. AVCodecParameters* Demuxer::video_codec_parameters() {
  16. return format_context_->streams[video_stream_index_]->codecpar;
  17. }
  18. int Demuxer::video_stream_index() const {
  19. return video_stream_index_;
  20. }
  21. AVRational Demuxer::time_base() const {
  22. return format_context_->streams[video_stream_index_]->time_base;
  23. }
  24. bool Demuxer::operator()(AVPacket &packet) {
  25. return av_read_frame(format_context_, &packet) >= 0;
  26. }