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 | ||
22 | #undef HAVE_AV_CONFIG_H | |
23 | #include "libavformat/avformat.h" | |
24 | #include "libavcodec/avcodec.h" | |
25 | #include "libavcodec/opt.h" | |
26 | #include "libavutil/pixdesc.h" | |
27 | #include "cmdutils.h" | |
28 | ||
29 | const char program_name[] = "FFprobe"; | |
30 | const int program_birth_year = 2007; | |
31 | ||
32 | static int do_show_format = 0; | |
33 | static int do_show_streams = 0; | |
34 | ||
35 | static int show_value_unit = 0; | |
36 | static int use_value_prefix = 0; | |
37 | static int use_byte_value_binary_prefix = 0; | |
38 | static int use_value_sexagesimal_format = 0; | |
39 | ||
40 | /* globals */ | |
41 | static const OptionDef options[]; | |
42 | ||
43 | /* FFprobe context */ | |
44 | static const char *input_filename; | |
1be784a2 | 45 | static AVInputFormat *iformat = NULL; |
336ce917 SS |
46 | |
47 | static const char *binary_unit_prefixes [] = { "", "Ki", "Mi", "Gi", "Ti", "Pi" }; | |
48 | static const char *decimal_unit_prefixes[] = { "", "K" , "M" , "G" , "T" , "P" }; | |
49 | ||
50 | static const char *unit_second_str = "s" ; | |
51 | static const char *unit_hertz_str = "Hz" ; | |
52 | static const char *unit_byte_str = "byte" ; | |
53 | static const char *unit_bit_per_second_str = "bit/s"; | |
54 | ||
55 | static char *value_string(char *buf, int buf_size, double val, const char *unit) | |
56 | { | |
57 | if (unit == unit_second_str && use_value_sexagesimal_format) { | |
58 | double secs; | |
59 | int hours, mins; | |
60 | secs = val; | |
61 | mins = (int)secs / 60; | |
62 | secs = secs - mins * 60; | |
63 | hours = mins / 60; | |
64 | mins %= 60; | |
65 | snprintf(buf, buf_size, "%d:%02d:%09.6f", hours, mins, secs); | |
66 | } else if (use_value_prefix) { | |
67 | const char *prefix_string; | |
68 | int index; | |
69 | ||
70 | if (unit == unit_byte_str && use_byte_value_binary_prefix) { | |
f1b267dd | 71 | index = (int) (log(val)/log(2)) / 10; |
336ce917 SS |
72 | index = av_clip(index, 0, FF_ARRAY_ELEMS(binary_unit_prefixes) -1); |
73 | val /= pow(2, index*10); | |
74 | prefix_string = binary_unit_prefixes[index]; | |
75 | } else { | |
76 | index = (int) (log10(val)) / 3; | |
77 | index = av_clip(index, 0, FF_ARRAY_ELEMS(decimal_unit_prefixes) -1); | |
78 | val /= pow(10, index*3); | |
79 | prefix_string = decimal_unit_prefixes[index]; | |
80 | } | |
81 | ||
82 | snprintf(buf, buf_size, "%.3f %s%s", val, prefix_string, show_value_unit ? unit : ""); | |
83 | } else { | |
84 | snprintf(buf, buf_size, "%f %s", val, show_value_unit ? unit : ""); | |
85 | } | |
86 | ||
87 | return buf; | |
88 | } | |
89 | ||
90 | static char *time_value_string(char *buf, int buf_size, int64_t val, const AVRational *time_base) | |
91 | { | |
92 | if (val == AV_NOPTS_VALUE) { | |
93 | snprintf(buf, buf_size, "N/A"); | |
94 | } else { | |
95 | value_string(buf, buf_size, val * av_q2d(*time_base), unit_second_str); | |
96 | } | |
97 | ||
98 | return buf; | |
99 | } | |
100 | ||
101 | static const char *codec_type_string(enum CodecType codec_type) | |
102 | { | |
103 | switch (codec_type) { | |
104 | case CODEC_TYPE_VIDEO: return "video"; | |
105 | case CODEC_TYPE_AUDIO: return "audio"; | |
106 | case CODEC_TYPE_DATA: return "data"; | |
107 | case CODEC_TYPE_SUBTITLE: return "subtitle"; | |
108 | case CODEC_TYPE_ATTACHMENT: return "attachment"; | |
109 | default: return "unknown"; | |
110 | } | |
111 | } | |
112 | ||
113 | static void show_stream(AVFormatContext *fmt_ctx, int stream_idx) | |
114 | { | |
115 | AVStream *stream = fmt_ctx->streams[stream_idx]; | |
116 | AVCodecContext *dec_ctx; | |
117 | AVCodec *dec; | |
118 | char val_str[128]; | |
119 | AVMetadataTag *tag; | |
120 | char a, b, c, d; | |
121 | ||
122 | printf("[STREAM]\n"); | |
123 | ||
124 | printf("index=%d\n", stream->index); | |
125 | ||
126 | if ((dec_ctx = stream->codec)) { | |
127 | if ((dec = dec_ctx->codec)) { | |
128 | printf("codec_name=%s\n", dec->name); | |
129 | printf("codec_long_name=%s\n", dec->long_name); | |
130 | } else { | |
131 | printf("codec_name=unknown\n"); | |
132 | } | |
133 | ||
134 | printf("codec_type=%s\n", codec_type_string(dec_ctx->codec_type)); | |
135 | printf("codec_time_base=%d/%d\n", dec_ctx->time_base.num, dec_ctx->time_base.den); | |
136 | ||
137 | /* print AVI/FourCC tag */ | |
138 | a = dec_ctx->codec_tag & 0xff; | |
139 | b = dec_ctx->codec_tag>>8 & 0xff; | |
140 | c = dec_ctx->codec_tag>>16 & 0xff; | |
141 | d = dec_ctx->codec_tag>>24 & 0xff; | |
142 | printf("codec_tag_string="); | |
143 | if (isprint(a)) printf("%c", a); else printf("[%d]", a); | |
144 | if (isprint(b)) printf("%c", b); else printf("[%d]", b); | |
145 | if (isprint(c)) printf("%c", c); else printf("[%d]", c); | |
146 | if (isprint(d)) printf("%c", d); else printf("[%d]", d); | |
147 | printf("\ncodec_tag=0x%04x\n", dec_ctx->codec_tag); | |
148 | ||
149 | switch (dec_ctx->codec_type) { | |
150 | case CODEC_TYPE_VIDEO: | |
151 | printf("width=%d\n", dec_ctx->width); | |
152 | printf("height=%d\n", dec_ctx->height); | |
153 | printf("has_b_frames=%d\n", dec_ctx->has_b_frames); | |
154 | printf("sample_aspect_ratio=%d:%d\n", dec_ctx->sample_aspect_ratio.num, | |
155 | dec_ctx->sample_aspect_ratio.den); | |
156 | printf("display_aspect_ratio=%d:%d\n", dec_ctx->sample_aspect_ratio.num, | |
157 | dec_ctx->sample_aspect_ratio.den); | |
158 | printf("pix_fmt=%s\n", dec_ctx->pix_fmt != PIX_FMT_NONE ? | |
159 | av_pix_fmt_descriptors[dec_ctx->pix_fmt].name : "unknown"); | |
160 | break; | |
161 | ||
162 | case CODEC_TYPE_AUDIO: | |
163 | printf("sample_rate=%s\n", value_string(val_str, sizeof(val_str), | |
164 | dec_ctx->sample_rate, | |
165 | unit_hertz_str)); | |
166 | printf("channels=%d\n", dec_ctx->channels); | |
167 | printf("bits_per_sample=%d\n", av_get_bits_per_sample(dec_ctx->codec_id)); | |
168 | break; | |
169 | } | |
170 | } else { | |
171 | printf("codec_type=unknown\n"); | |
172 | } | |
173 | ||
174 | if (fmt_ctx->iformat->flags & AVFMT_SHOW_IDS) | |
175 | printf("id=0x%x\n", stream->id); | |
176 | printf("r_frame_rate=%d/%d\n", stream->r_frame_rate.num, stream->r_frame_rate.den); | |
177 | printf("avg_frame_rate=%d/%d\n", stream->avg_frame_rate.num, stream->avg_frame_rate.den); | |
178 | printf("time_base=%d/%d\n", stream->time_base.num, stream->time_base.den); | |
179 | if (stream->language[0]) | |
180 | printf("language=%s\n", stream->language); | |
181 | printf("start_time=%s\n", time_value_string(val_str, sizeof(val_str), stream->start_time, | |
182 | &stream->time_base)); | |
183 | printf("duration=%s\n", time_value_string(val_str, sizeof(val_str), stream->duration, | |
184 | &stream->time_base)); | |
185 | ||
186 | while ((tag = av_metadata_get(stream->metadata, "", tag, AV_METADATA_IGNORE_SUFFIX))) | |
b6a41853 | 187 | printf("TAG:%s=%s\n", tag->key, tag->value); |
336ce917 SS |
188 | |
189 | printf("[/STREAM]\n"); | |
190 | } | |
191 | ||
192 | static void show_format(AVFormatContext *fmt_ctx) | |
193 | { | |
194 | AVMetadataTag *tag = NULL; | |
195 | char val_str[128]; | |
196 | ||
197 | printf("[FORMAT]\n"); | |
198 | ||
199 | printf("filename=%s\n", fmt_ctx->filename); | |
200 | printf("nb_streams=%d\n", fmt_ctx->nb_streams); | |
201 | printf("format_name=%s\n", fmt_ctx->iformat->name); | |
202 | printf("format_long_name=%s\n", fmt_ctx->iformat->long_name); | |
203 | printf("start_time=%s\n", time_value_string(val_str, sizeof(val_str), fmt_ctx->start_time, | |
204 | &AV_TIME_BASE_Q)); | |
205 | printf("duration=%s\n", time_value_string(val_str, sizeof(val_str), fmt_ctx->duration, | |
206 | &AV_TIME_BASE_Q)); | |
207 | printf("size=%s\n", value_string(val_str, sizeof(val_str), fmt_ctx->file_size, | |
208 | unit_byte_str)); | |
209 | printf("bit_rate=%s\n", value_string(val_str, sizeof(val_str), fmt_ctx->bit_rate, | |
210 | unit_bit_per_second_str)); | |
211 | ||
212 | while ((tag = av_metadata_get(fmt_ctx->metadata, "", tag, AV_METADATA_IGNORE_SUFFIX))) | |
213 | printf("TAG:%s=%s\n", tag->key, tag->value); | |
214 | ||
215 | printf("[/FORMAT]\n"); | |
216 | } | |
217 | ||
218 | static int open_input_file(AVFormatContext **fmt_ctx_ptr, const char *filename) | |
219 | { | |
220 | int err, i; | |
221 | AVFormatContext *fmt_ctx; | |
222 | ||
223 | fmt_ctx = avformat_alloc_context(); | |
224 | ||
1be784a2 | 225 | if ((err = av_open_input_file(&fmt_ctx, filename, iformat, 0, NULL)) < 0) { |
336ce917 SS |
226 | print_error(filename, err); |
227 | return err; | |
228 | } | |
229 | ||
230 | /* fill the streams in the format context */ | |
231 | if ((err = av_find_stream_info(fmt_ctx)) < 0) { | |
232 | print_error(filename, err); | |
233 | return err; | |
234 | } | |
235 | ||
236 | dump_format(fmt_ctx, 0, filename, 0); | |
237 | ||
238 | /* bind a decoder to each input stream */ | |
239 | for (i = 0; i < fmt_ctx->nb_streams; i++) { | |
240 | AVStream *stream = fmt_ctx->streams[i]; | |
241 | AVCodec *codec; | |
242 | ||
243 | if (!(codec = avcodec_find_decoder(stream->codec->codec_id))) { | |
244 | fprintf(stderr, "Unsupported codec (id=%d) for input stream %d\n", | |
245 | stream->codec->codec_id, stream->index); | |
246 | } else if (avcodec_open(stream->codec, codec) < 0) { | |
247 | fprintf(stderr, "Error while opening codec for input stream %d\n", | |
248 | stream->index); | |
249 | } | |
250 | } | |
251 | ||
252 | *fmt_ctx_ptr = fmt_ctx; | |
253 | return 0; | |
254 | } | |
255 | ||
256 | static int probe_file(const char *filename) | |
257 | { | |
258 | AVFormatContext *fmt_ctx; | |
259 | int ret, i; | |
260 | ||
261 | if ((ret = open_input_file(&fmt_ctx, filename))) | |
262 | return ret; | |
263 | ||
264 | if (do_show_streams) | |
265 | for (i = 0; i < fmt_ctx->nb_streams; i++) | |
266 | show_stream(fmt_ctx, i); | |
267 | ||
268 | if (do_show_format) | |
269 | show_format(fmt_ctx); | |
270 | ||
271 | av_close_input_file(fmt_ctx); | |
272 | return 0; | |
273 | } | |
274 | ||
275 | static void show_usage(void) | |
276 | { | |
277 | printf("Simple multimedia streams analyzer\n"); | |
278 | printf("usage: ffprobe [OPTIONS] [INPUT_FILE]\n"); | |
279 | printf("\n"); | |
280 | } | |
281 | ||
1be784a2 SS |
282 | static void opt_format(const char *arg) |
283 | { | |
284 | iformat = av_find_input_format(arg); | |
285 | if (!iformat) { | |
286 | fprintf(stderr, "Unknown input format: %s\n", arg); | |
287 | exit(1); | |
288 | } | |
289 | } | |
290 | ||
912dd63e | 291 | static void opt_input_file(const char *arg) |
336ce917 | 292 | { |
c8c0ac6b | 293 | if (input_filename) { |
912dd63e | 294 | fprintf(stderr, "Input filename already specified: %s\n", arg); |
c8c0ac6b RP |
295 | exit(1); |
296 | } | |
912dd63e SS |
297 | if (!strcmp(arg, "-")) |
298 | arg = "pipe:"; | |
299 | input_filename = arg; | |
336ce917 SS |
300 | } |
301 | ||
302 | static void show_help(void) | |
303 | { | |
304 | show_usage(); | |
305 | show_help_options(options, "Main options:\n", 0, 0); | |
306 | printf("\n"); | |
307 | } | |
308 | ||
309 | static void opt_pretty(void) | |
310 | { | |
311 | show_value_unit = 1; | |
312 | use_value_prefix = 1; | |
313 | use_byte_value_binary_prefix = 1; | |
314 | use_value_sexagesimal_format = 1; | |
315 | } | |
316 | ||
317 | static const OptionDef options[] = { | |
318 | #include "cmdutils_common_opts.h" | |
1be784a2 | 319 | { "f", HAS_ARG, {(void*)opt_format}, "force format", "format" }, |
336ce917 SS |
320 | { "unit", OPT_BOOL, {(void*)&show_value_unit}, "show unit of the displayed values" }, |
321 | { "prefix", OPT_BOOL, {(void*)&use_value_prefix}, "use SI prefixes for the displayed values" }, | |
322 | { "byte_binary_prefix", OPT_BOOL, {(void*)&use_byte_value_binary_prefix}, | |
323 | "use binary prefixes for byte units" }, | |
324 | { "sexagesimal", OPT_BOOL, {(void*)&use_value_sexagesimal_format}, | |
325 | "use sexagesimal format HOURS:MM:SS.MICROSECONDS for time units" }, | |
326 | { "pretty", 0, {(void*)&opt_pretty}, | |
327 | "prettify the format of displayed values, make it more human readable" }, | |
328 | { "show_format", OPT_BOOL, {(void*)&do_show_format} , "show format/container info" }, | |
329 | { "show_streams", OPT_BOOL, {(void*)&do_show_streams}, "show streams info" }, | |
330 | { NULL, }, | |
331 | }; | |
332 | ||
333 | int main(int argc, char **argv) | |
334 | { | |
335 | av_register_all(); | |
336 | ||
337 | show_banner(); | |
338 | parse_options(argc, argv, options, opt_input_file); | |
339 | ||
340 | if (!input_filename) { | |
341 | show_usage(); | |
342 | fprintf(stderr, "You have to specify one input file.\n"); | |
343 | fprintf(stderr, "Use -h to get full help or, even better, run 'man ffprobe'.\n"); | |
344 | exit(1); | |
345 | } | |
346 | ||
347 | return probe_file(input_filename); | |
348 | } |