1
2
3
4
5
6
7
8
9
10
11
|
AVFormatContext *ic = avformat_alloc_context();
if
(avformat_open_input(&ic, url, NULL, NULL) < 0) {
LOGE(
"could not open source %s"
, url);
return
-1;
}
if
(avformat_find_stream_info(ic, NULL) < 0) {
LOGE(
"could not find stream information"
);
return
-1;
}
|
1
|
av_dump_format(ic, 0, ic->filename, 0);
|
1
2
3
4
5
6
7
|
-
struct
AVInputFormat *iformat;
// 记录了封装格式信息
- unsigned
int
nb_streams;
// 记录了该 URL 中包含有几路流
- AVStream **streams;
// 一个结构体数组,每个对象记录了一路流的详细信息
- int64_t start_time;
// 第一帧的时间戳
- int64_t duration;
// 码流的总时长
-
int
bit_rate;
// 码流的总码率,bps
- AVDictionary *metadata;
// 一些文件信息头,key/value 字符串
|
1
2
3
4
5
|
int
video_stream_idx = av_find_best_stream(ic, AVMEDIA_TYPE_VIDEO, -1, -1, NULL, 0);
AVStream video_stream = ic->streams[video_stream_idx];
int
audio_stream_idx = av_find_best_stream(ic, AVMEDIA_TYPE_AUDIO, -1, -1, NULL, 0);
AVStream audio_stream = ic->streams[audio_stream_idx];
|
1
2
3
4
5
6
|
- AVCodecContext *codec;
// 记录了该码流的编码信息
- int64_t start_time;
// 第一帧的时间戳
- int64_t duration;
// 该码流的时长
- int64_t nb_frames;
// 该码流的总帧数
- AVDictionary *metadata;
// 一些文件信息头,key/value 字符串
- AVRational avg_frame_rate;
// 平均帧率
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
-
const
struct
AVCodec *codec;
// 编码的详细信息
-
enum
AVCodecID codec_id;
// 编码类型
-
int
bit_rate;
// 平均码率
/* video only */
-
int
width, height;
// 图像的宽高尺寸,码流中不一定存在该信息,会由解码后覆盖
-
enum
AVPixelFormat pix_fmt;
// 原始图像的格式,码流中不一定存在该信息,会由解码后覆盖
/* audio only */
-
int
sample_rate;
// 音频的采样率
-
int
channels;
// 音频的通道数
-
enum
AVSampleFormat sample_fmt;
// 音频的格式,位宽
-
int
frame_size;
// 每个音频帧的 sample 个数
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
#include <libavutil/log.h>
#define LOGD(format, ...) av_log(NULL, AV_LOG_DEBUG, format, ##__VA_ARGS__);
int
ff_dump_stream_info(
char
* url)
{
AVFormatContext *ic = avformat_alloc_context();
if
(avformat_open_input(&ic, url, NULL, NULL) < 0) {
LOGD(
"could not open source %s"
, url);
return
-1;
}
if
(avformat_find_stream_info(ic, NULL) < 0) {
LOGD(
"could not find stream information"
);
return
-1;
}
LOGD(
"---------- dumping stream info ----------"
);
LOGD(
"input format: %s"
, ic->iformat->name);
LOGD(
"nb_streams: %d"
, ic->nb_streams);
int64_t start_time = ic->start_time / AV_TIME_BASE;
LOGD(
"start_time: %lld"
, start_time);
int64_t duration = ic->duration / AV_TIME_BASE;
LOGD(
"duration: %lld s"
, duration);
int
video_stream_idx = av_find_best_stream(ic, AVMEDIA_TYPE_VIDEO, -1, -1, NULL, 0);
if
(video_stream_idx >= 0) {
AVStream *video_stream = ic->streams[video_stream_idx];
LOGD(
"video nb_frames: %lld"
, video_stream->nb_frames);
LOGD(
"video codec_id: %d"
, video_stream->codec->codec_id);
LOGD(
"video codec_name: %s"
, avcodec_get_name(video_stream->codec->codec_id));
LOGD(
"video width x height: %d x %d"
, video_stream->codec->width, video_stream->codec->height);
LOGD(
"video pix_fmt: %d"
, video_stream->codec->pix_fmt);
LOGD(
"video bitrate %lld kb/s"
, (int64_t) video_stream->codec->bit_rate / 1000);
LOGD(
"video avg_frame_rate: %d fps"
, video_stream->avg_frame_rate.num/video_stream->avg_frame_rate.den);
}
int
audio_stream_idx = av_find_best_stream(ic, AVMEDIA_TYPE_AUDIO, -1, -1, NULL, 0);
if
(audio_stream_idx >= 0) {
AVStream *audio_stream = ic->streams[audio_stream_idx];
LOGD(
"audio codec_id: %d"
, audio_stream->codec->codec_id);
LOGD(
"audio codec_name: %s"
, avcodec_get_name(audio_stream->codec->codec_id));
LOGD(
"audio sample_rate: %d"
, audio_stream->codec->sample_rate);
LOGD(
"audio channels: %d"
, audio_stream->codec->channels);
LOGD(
"audio sample_fmt: %d"
, audio_stream->codec->sample_fmt);
LOGD(
"audio frame_size: %d"
, audio_stream->codec->frame_size);
LOGD(
"audio nb_frames: %lld"
, audio_stream->nb_frames);
LOGD(
"audio bitrate %lld kb/s"
, (int64_t) audio_stream->codec->bit_rate / 1000);
}
LOGD(
"---------- dumping stream info ----------"
);
avformat_close_input(&ic);
}
|