@@ -21,6 +21,10 @@ FFmpegDemux::FFmpegDemux(std::string input_url)
2121 , m_video_stream_index(-1 )
2222 , m_video_stream(nullptr )
2323 , m_format_context(nullptr )
24+ , m_codec_id(-1 )
25+ , m_width(-1 )
26+ , m_height(-1 )
27+ , m_fps(-1.0 )
2428{
2529}
2630
@@ -33,32 +37,53 @@ FFmpegDemux::~FFmpegDemux()
3337
3438bool FFmpegDemux::setup () {
3539 int code = 0 ;
36- do {
37- AVDictionary *options = 0 ;
38- av_dict_set (&options, " rtsp_transport" , " tcp" , 0 );
39-
40- code = avformat_open_input (&m_format_context, m_input_url.c_str (), NULL , &options);
41- if (code < 0 ) {
42- SPDLOG_ERROR (" avformat_open_input error, code: {}, msg: {}, m_input_url: {}" , code, ffmpeg_error_str (code), m_input_url);
43- return false ;
44- }
4540
46- code = avformat_find_stream_info (m_format_context, NULL );
47- if (code < 0 ) {
48- SPDLOG_ERROR (" avformat_find_stream_info error, code: {}, msg: {}, m_input_url: {}" , code, ffmpeg_error_str (code), m_input_url);
49- return false ;
50- }
41+ AVDictionary *options = 0 ;
42+ av_dict_set (&options, " rtsp_transport" , " tcp" , 0 );
5143
52- code = av_find_best_stream (m_format_context, AVMEDIA_TYPE_VIDEO, -1 , -1 , NULL , 0 );
53- if (code < 0 ) {
54- SPDLOG_ERROR (" av_find_best_stream error, code: {}, msg: {}, m_input_url: {}" , code, ffmpeg_error_str (code), m_input_url);
55- return false ;
56- }
44+ code = avformat_open_input (&m_format_context, m_input_url.c_str (), NULL , &options);
45+ if (code < 0 ) {
46+ SPDLOG_ERROR (" avformat_open_input error, code: {}, msg: {}, m_input_url: {}" , code, ffmpeg_error_str (code), m_input_url);
47+ return false ;
48+ }
49+
50+ code = avformat_find_stream_info (m_format_context, NULL );
51+ if (code < 0 ) {
52+ SPDLOG_ERROR (" avformat_find_stream_info error, code: {}, msg: {}, m_input_url: {}" , code, ffmpeg_error_str (code), m_input_url);
53+ return false ;
54+ }
55+
56+ code = av_find_best_stream (m_format_context, AVMEDIA_TYPE_VIDEO, -1 , -1 , NULL , 0 );
57+ if (code < 0 ) {
58+ SPDLOG_ERROR (" av_find_best_stream error, code: {}, msg: {}, m_input_url: {}" , code, ffmpeg_error_str (code), m_input_url);
59+ return false ;
60+ }
5761
58- m_video_stream_index = code;
59- m_video_stream = m_format_context->streams [m_video_stream_index];
62+ m_video_stream_index = code;
63+ m_video_stream = m_format_context->streams [m_video_stream_index];
6064
61- } while (false );
65+ AVCodecParameters *codec_params = m_video_stream->codecpar ;
66+ m_codec_id = (int )codec_params->codec_id ;
67+ m_width = codec_params->width ;
68+ m_height = codec_params->height ;
69+
70+ if (m_video_stream->avg_frame_rate .num > 0 && m_video_stream->avg_frame_rate .den > 0 ) {
71+ // fps
72+ m_fps = av_q2d (m_video_stream->avg_frame_rate );
73+ }
74+ else if (m_video_stream->r_frame_rate .num > 0 && m_video_stream->r_frame_rate .den > 0 ) {
75+ // tbr
76+ m_fps = av_q2d (m_video_stream->r_frame_rate );
77+ }
78+ else if (m_video_stream->time_base .num > 0 && m_video_stream->time_base .den > 0 ) {
79+ // tbn
80+ m_fps = 1.0 / av_q2d (m_video_stream->time_base );
81+ }
82+ else {
83+ // default
84+ m_fps = 25.0 ;
85+ SPDLOG_WARN (" can't find fps from frame_rate and timebase, set default to 25.0" );
86+ }
6287
6388 return true ;
6489}
@@ -95,3 +120,45 @@ FFmpegPacket FFmpegDemux::read_frame() {
95120
96121 return packet;
97122}
123+
124+
125+ std::vector<FFmpegPacket> FFmpegDemux::read_some_frames (int limit_packets)
126+ {
127+ std::vector<FFmpegPacket> vec;
128+
129+ for (int i = 0 ; i < limit_packets; i++) {
130+ FFmpegPacket packet = read_frame ();
131+ if (packet.is_null ()) {
132+ break ;
133+ }
134+
135+ vec.push_back (packet);
136+ }
137+
138+ return vec;
139+ }
140+
141+
142+ int FFmpegDemux::codec_id ()
143+ {
144+ return m_codec_id;
145+ }
146+
147+
148+ int FFmpegDemux::width ()
149+ {
150+ return m_width;
151+ }
152+
153+
154+ int FFmpegDemux::height ()
155+ {
156+ return m_height;
157+ }
158+
159+
160+ double FFmpegDemux::fps ()
161+ {
162+ return m_fps;
163+ }
164+
0 commit comments