Commit | Line | Data |
---|---|---|
336ce917 SS |
1 | /* |
2 | * FFprobe : Simple Media Prober based on the FFmpeg libraries | |
3 | * Copyright (c) 2007-2010 Stefano Sabatini | |
4 | * | |
5 | * This file is part of FFmpeg. | |
6 | * | |
7 | * FFmpeg is free software; you can redistribute it and/or | |
8 | * modify it under the terms of the GNU Lesser General Public | |
9 | * License as published by the Free Software Foundation; either | |
10 | * version 2.1 of the License, or (at your option) any later version. | |
11 | * | |
12 | * FFmpeg is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 | * Lesser General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU Lesser General Public | |
18 | * License along with FFmpeg; if not, write to the Free Software | |
19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
20 | */ | |
21 | ||
6ce98ea4 SS |
22 | #include "config.h" |
23 | ||
336ce917 SS |
24 | #include "libavformat/avformat.h" |
25 | #include "libavcodec/avcodec.h" | |
26 | #include "libavcodec/opt.h" | |
27 | #include "libavutil/pixdesc.h" | |
6ce98ea4 | 28 | #include "libavdevice/avdevice.h" |
336ce917 SS |
29 | #include "cmdutils.h" |
30 | ||
31 | const char program_name[] = "FFprobe"; | |
32 | const int program_birth_year = 2007; | |
33 | ||
34 | static int do_show_format = 0; | |
35 | static int do_show_streams = 0; | |
36 | ||
4bfe67da | 37 | static int convert_tags = 0; |
336ce917 SS |
38 | static int show_value_unit = 0; |
39 | static int use_value_prefix = 0; | |
40 | static int use_byte_value_binary_prefix = 0; | |
41 | static int use_value_sexagesimal_format = 0; | |
42 | ||
43 | /* globals */ | |
44 | static const OptionDef options[]; | |
45 | ||
46 | /* FFprobe context */ | |
47 | static const char *input_filename; | |
1be784a2 | 48 | static AVInputFormat *iformat = NULL; |
336ce917 SS |
49 | |
50 | static const char *binary_unit_prefixes [] = { "", "Ki", "Mi", "Gi", "Ti", "Pi" }; | |
51 | static const char *decimal_unit_prefixes[] = { "", "K" , "M" , "G" , "T" , "P" }; | |
52 | ||
53 | static const char *unit_second_str = "s" ; | |
54 | static const char *unit_hertz_str = "Hz" ; | |
55 | static const char *unit_byte_str = "byte" ; | |
56 | static const char *unit_bit_per_second_str = "bit/s"; | |
57 | ||
58 | static char *value_string(char *buf, int buf_size, double val, const char *unit) | |
59 | { | |
60 | if (unit == unit_second_str && use_value_sexagesimal_format) { | |
61 | double secs; | |
62 | int hours, mins; | |
63 | secs = val; | |
64 | mins = (int)secs / 60; | |
65 | secs = secs - mins * 60; | |
66 | hours = mins / 60; | |
67 | mins %= 60; | |
68 | snprintf(buf, buf_size, "%d:%02d:%09.6f", hours, mins, secs); | |
69 | } else if (use_value_prefix) { | |
70 | const char *prefix_string; | |
71 | int index; | |
72 | ||
73 | if (unit == unit_byte_str && use_byte_value_binary_prefix) { | |
f1b267dd | 74 | index = (int) (log(val)/log(2)) / 10; |
336ce917 SS |
75 | index = av_clip(index, 0, FF_ARRAY_ELEMS(binary_unit_prefixes) -1); |
76 | val /= pow(2, index*10); | |
77 | prefix_string = binary_unit_prefixes[index]; | |
78 | } else { | |
79 | index = (int) (log10(val)) / 3; | |
80 | index = av_clip(index, 0, FF_ARRAY_ELEMS(decimal_unit_prefixes) -1); | |
81 | val /= pow(10, index*3); | |
82 | prefix_string = decimal_unit_prefixes[index]; | |
83 | } | |
84 | ||
85 | snprintf(buf, buf_size, "%.3f %s%s", val, prefix_string, show_value_unit ? unit : ""); | |
86 | } else { | |
87 | snprintf(buf, buf_size, "%f %s", val, show_value_unit ? unit : ""); | |
88 | } | |
89 | ||
90 | return buf; | |
91 | } | |
92 | ||
93 | static char *time_value_string(char *buf, int buf_size, int64_t val, const AVRational *time_base) | |
94 | { | |
95 | if (val == AV_NOPTS_VALUE) { | |
96 | snprintf(buf, buf_size, "N/A"); | |
97 | } else { | |
98 | value_string(buf, buf_size, val * av_q2d(*time_base), unit_second_str); | |
99 | } | |
100 | ||
101 | return buf; | |
102 | } | |
103 | ||
72415b2a | 104 | static const char *media_type_string(enum AVMediaType media_type) |
336ce917 | 105 | { |
72415b2a SS |
106 | switch (media_type) { |
107 | case AVMEDIA_TYPE_VIDEO: return "video"; | |
108 | case AVMEDIA_TYPE_AUDIO: return "audio"; | |
109 | case AVMEDIA_TYPE_DATA: return "data"; | |
110 | case AVMEDIA_TYPE_SUBTITLE: return "subtitle"; | |
111 | case AVMEDIA_TYPE_ATTACHMENT: return "attachment"; | |
336ce917 SS |
112 | default: return "unknown"; |
113 | } | |
114 | } | |
115 | ||
116 | static void show_stream(AVFormatContext *fmt_ctx, int stream_idx) | |
117 | { | |
118 | AVStream *stream = fmt_ctx->streams[stream_idx]; | |
119 | AVCodecContext *dec_ctx; | |
120 | AVCodec *dec; | |
121 | char val_str[128]; | |
efe8bb09 | 122 | AVMetadataTag *tag = NULL; |
d21b2278 | 123 | AVRational display_aspect_ratio; |
336ce917 SS |
124 | |
125 | printf("[STREAM]\n"); | |
126 | ||
127 | printf("index=%d\n", stream->index); | |
128 | ||
129 | if ((dec_ctx = stream->codec)) { | |
130 | if ((dec = dec_ctx->codec)) { | |
131 | printf("codec_name=%s\n", dec->name); | |
132 | printf("codec_long_name=%s\n", dec->long_name); | |
133 | } else { | |
134 | printf("codec_name=unknown\n"); | |
135 | } | |
136 | ||
72415b2a | 137 | printf("codec_type=%s\n", media_type_string(dec_ctx->codec_type)); |
336ce917 SS |
138 | printf("codec_time_base=%d/%d\n", dec_ctx->time_base.num, dec_ctx->time_base.den); |
139 | ||
140 | /* print AVI/FourCC tag */ | |
7e566bbe SS |
141 | av_get_codec_tag_string(val_str, sizeof(val_str), dec_ctx->codec_tag); |
142 | printf("codec_tag_string=%s\n", val_str); | |
143 | printf("codec_tag=0x%04x\n", dec_ctx->codec_tag); | |
336ce917 SS |
144 | |
145 | switch (dec_ctx->codec_type) { | |
72415b2a | 146 | case AVMEDIA_TYPE_VIDEO: |
336ce917 SS |
147 | printf("width=%d\n", dec_ctx->width); |
148 | printf("height=%d\n", dec_ctx->height); | |
149 | printf("has_b_frames=%d\n", dec_ctx->has_b_frames); | |
8280e2bd | 150 | if (dec_ctx->sample_aspect_ratio.num) { |
441881b4 SS |
151 | printf("sample_aspect_ratio=%d:%d\n", dec_ctx->sample_aspect_ratio.num, |
152 | dec_ctx->sample_aspect_ratio.den); | |
153 | av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den, | |
154 | dec_ctx->width * dec_ctx->sample_aspect_ratio.num, | |
155 | dec_ctx->height * dec_ctx->sample_aspect_ratio.den, | |
156 | 1024*1024); | |
157 | printf("display_aspect_ratio=%d:%d\n", display_aspect_ratio.num, | |
158 | display_aspect_ratio.den); | |
8280e2bd | 159 | } |
336ce917 SS |
160 | printf("pix_fmt=%s\n", dec_ctx->pix_fmt != PIX_FMT_NONE ? |
161 | av_pix_fmt_descriptors[dec_ctx->pix_fmt].name : "unknown"); | |
162 | break; | |
163 | ||
72415b2a | 164 | case AVMEDIA_TYPE_AUDIO: |
336ce917 SS |
165 | printf("sample_rate=%s\n", value_string(val_str, sizeof(val_str), |
166 | dec_ctx->sample_rate, | |
167 | unit_hertz_str)); | |
168 | printf("channels=%d\n", dec_ctx->channels); | |
169 | printf("bits_per_sample=%d\n", av_get_bits_per_sample(dec_ctx->codec_id)); | |
170 | break; | |
171 | } | |
172 | } else { | |
173 | printf("codec_type=unknown\n"); | |
174 | } | |
175 | ||
176 | if (fmt_ctx->iformat->flags & AVFMT_SHOW_IDS) | |
177 | printf("id=0x%x\n", stream->id); | |
178 | printf("r_frame_rate=%d/%d\n", stream->r_frame_rate.num, stream->r_frame_rate.den); | |
179 | printf("avg_frame_rate=%d/%d\n", stream->avg_frame_rate.num, stream->avg_frame_rate.den); | |
180 | printf("time_base=%d/%d\n", stream->time_base.num, stream->time_base.den); | |
181 | if (stream->language[0]) | |
182 | printf("language=%s\n", stream->language); | |
183 | printf("start_time=%s\n", time_value_string(val_str, sizeof(val_str), stream->start_time, | |
184 | &stream->time_base)); | |
185 | printf("duration=%s\n", time_value_string(val_str, sizeof(val_str), stream->duration, | |
186 | &stream->time_base)); | |
0a6a282a RK |
187 | if (stream->nb_frames) |
188 | printf("nb_frames=%"PRId64"\n", stream->nb_frames); | |
336ce917 SS |
189 | |
190 | while ((tag = av_metadata_get(stream->metadata, "", tag, AV_METADATA_IGNORE_SUFFIX))) | |
b6a41853 | 191 | printf("TAG:%s=%s\n", tag->key, tag->value); |
336ce917 SS |
192 | |
193 | printf("[/STREAM]\n"); | |
194 | } | |
195 | ||
196 | static void show_format(AVFormatContext *fmt_ctx) | |
197 | { | |
198 | AVMetadataTag *tag = NULL; | |
199 | char val_str[128]; | |
200 | ||
201 | printf("[FORMAT]\n"); | |
202 | ||
203 | printf("filename=%s\n", fmt_ctx->filename); | |
204 | printf("nb_streams=%d\n", fmt_ctx->nb_streams); | |
205 | printf("format_name=%s\n", fmt_ctx->iformat->name); | |
206 | printf("format_long_name=%s\n", fmt_ctx->iformat->long_name); | |
207 | printf("start_time=%s\n", time_value_string(val_str, sizeof(val_str), fmt_ctx->start_time, | |
208 | &AV_TIME_BASE_Q)); | |
209 | printf("duration=%s\n", time_value_string(val_str, sizeof(val_str), fmt_ctx->duration, | |
210 | &AV_TIME_BASE_Q)); | |
211 | printf("size=%s\n", value_string(val_str, sizeof(val_str), fmt_ctx->file_size, | |
212 | unit_byte_str)); | |
213 | printf("bit_rate=%s\n", value_string(val_str, sizeof(val_str), fmt_ctx->bit_rate, | |
214 | unit_bit_per_second_str)); | |
215 | ||
4bfe67da SS |
216 | if (convert_tags) |
217 | av_metadata_conv(fmt_ctx, NULL, fmt_ctx->iformat->metadata_conv); | |
336ce917 SS |
218 | while ((tag = av_metadata_get(fmt_ctx->metadata, "", tag, AV_METADATA_IGNORE_SUFFIX))) |
219 | printf("TAG:%s=%s\n", tag->key, tag->value); | |
220 | ||
221 | printf("[/FORMAT]\n"); | |
222 | } | |
223 | ||
224 | static int open_input_file(AVFormatContext **fmt_ctx_ptr, const char *filename) | |
225 | { | |
226 | int err, i; | |
227 | AVFormatContext *fmt_ctx; | |
228 | ||
229 | fmt_ctx = avformat_alloc_context(); | |
230 | ||
1be784a2 | 231 | if ((err = av_open_input_file(&fmt_ctx, filename, iformat, 0, NULL)) < 0) { |
336ce917 SS |
232 | print_error(filename, err); |
233 | return err; | |
234 | } | |
235 | ||
236 | /* fill the streams in the format context */ | |
237 | if ((err = av_find_stream_info(fmt_ctx)) < 0) { | |
238 | print_error(filename, err); | |
239 | return err; | |
240 | } | |
241 | ||
242 | dump_format(fmt_ctx, 0, filename, 0); | |
243 | ||
244 | /* bind a decoder to each input stream */ | |
245 | for (i = 0; i < fmt_ctx->nb_streams; i++) { | |
246 | AVStream *stream = fmt_ctx->streams[i]; | |
247 | AVCodec *codec; | |
248 | ||
249 | if (!(codec = avcodec_find_decoder(stream->codec->codec_id))) { | |
250 | fprintf(stderr, "Unsupported codec (id=%d) for input stream %d\n", | |
251 | stream->codec->codec_id, stream->index); | |
252 | } else if (avcodec_open(stream->codec, codec) < 0) { | |
253 | fprintf(stderr, "Error while opening codec for input stream %d\n", | |
254 | stream->index); | |
255 | } | |
256 | } | |
257 | ||
258 | *fmt_ctx_ptr = fmt_ctx; | |
259 | return 0; | |
260 | } | |
261 | ||
262 | static int probe_file(const char *filename) | |
263 | { | |
264 | AVFormatContext *fmt_ctx; | |
265 | int ret, i; | |
266 | ||
267 | if ((ret = open_input_file(&fmt_ctx, filename))) | |
268 | return ret; | |
269 | ||
270 | if (do_show_streams) | |
271 | for (i = 0; i < fmt_ctx->nb_streams; i++) | |
272 | show_stream(fmt_ctx, i); | |
273 | ||
274 | if (do_show_format) | |
275 | show_format(fmt_ctx); | |
276 | ||
277 | av_close_input_file(fmt_ctx); | |
278 | return 0; | |
279 | } | |
280 | ||
281 | static void show_usage(void) | |
282 | { | |
283 | printf("Simple multimedia streams analyzer\n"); | |
284 | printf("usage: ffprobe [OPTIONS] [INPUT_FILE]\n"); | |
285 | printf("\n"); | |
286 | } | |
287 | ||
1be784a2 SS |
288 | static void opt_format(const char *arg) |
289 | { | |
290 | iformat = av_find_input_format(arg); | |
291 | if (!iformat) { | |
292 | fprintf(stderr, "Unknown input format: %s\n", arg); | |
293 | exit(1); | |
294 | } | |
295 | } | |
296 | ||
912dd63e | 297 | static void opt_input_file(const char *arg) |
336ce917 | 298 | { |
c8c0ac6b | 299 | if (input_filename) { |
58621b8d SS |
300 | fprintf(stderr, "Argument '%s' provided as input filename, but '%s' was already specified.\n", |
301 | arg, input_filename); | |
c8c0ac6b RP |
302 | exit(1); |
303 | } | |
912dd63e SS |
304 | if (!strcmp(arg, "-")) |
305 | arg = "pipe:"; | |
306 | input_filename = arg; | |
336ce917 SS |
307 | } |
308 | ||
309 | static void show_help(void) | |
310 | { | |
311 | show_usage(); | |
312 | show_help_options(options, "Main options:\n", 0, 0); | |
313 | printf("\n"); | |
314 | } | |
315 | ||
316 | static void opt_pretty(void) | |
317 | { | |
318 | show_value_unit = 1; | |
319 | use_value_prefix = 1; | |
320 | use_byte_value_binary_prefix = 1; | |
321 | use_value_sexagesimal_format = 1; | |
322 | } | |
323 | ||
324 | static const OptionDef options[] = { | |
325 | #include "cmdutils_common_opts.h" | |
4bfe67da | 326 | { "convert_tags", OPT_BOOL, {(void*)&convert_tags}, "convert tag names to the FFmpeg generic tag names" }, |
1be784a2 | 327 | { "f", HAS_ARG, {(void*)opt_format}, "force format", "format" }, |
818636ec SS |
328 | { "unit", OPT_BOOL, {(void*)&show_value_unit}, "show unit of the displayed values" }, |
329 | { "prefix", OPT_BOOL, {(void*)&use_value_prefix}, "use SI prefixes for the displayed values" }, | |
336ce917 SS |
330 | { "byte_binary_prefix", OPT_BOOL, {(void*)&use_byte_value_binary_prefix}, |
331 | "use binary prefixes for byte units" }, | |
332 | { "sexagesimal", OPT_BOOL, {(void*)&use_value_sexagesimal_format}, | |
333 | "use sexagesimal format HOURS:MM:SS.MICROSECONDS for time units" }, | |
334 | { "pretty", 0, {(void*)&opt_pretty}, | |
335 | "prettify the format of displayed values, make it more human readable" }, | |
336 | { "show_format", OPT_BOOL, {(void*)&do_show_format} , "show format/container info" }, | |
818636ec | 337 | { "show_streams", OPT_BOOL, {(void*)&do_show_streams}, "show streams info" }, |
336ce917 SS |
338 | { NULL, }, |
339 | }; | |
340 | ||
341 | int main(int argc, char **argv) | |
342 | { | |
343 | av_register_all(); | |
6ce98ea4 SS |
344 | #if CONFIG_AVDEVICE |
345 | avdevice_register_all(); | |
346 | #endif | |
336ce917 SS |
347 | |
348 | show_banner(); | |
349 | parse_options(argc, argv, options, opt_input_file); | |
350 | ||
351 | if (!input_filename) { | |
352 | show_usage(); | |
353 | fprintf(stderr, "You have to specify one input file.\n"); | |
354 | fprintf(stderr, "Use -h to get full help or, even better, run 'man ffprobe'.\n"); | |
355 | exit(1); | |
356 | } | |
357 | ||
358 | return probe_file(input_filename); | |
359 | } |