]> git.sesse.net Git - ffmpeg/blob - ffprobe.c
Implement cmdutils.c:read_file(), and use it in ffmpeg.c for reading
[ffmpeg] / ffprobe.c
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 #include "config.h"
23
24 #include "libavformat/avformat.h"
25 #include "libavcodec/avcodec.h"
26 #include "libavcodec/opt.h"
27 #include "libavutil/pixdesc.h"
28 #include "libavdevice/avdevice.h"
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
37 static int convert_tags                 = 0;
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;
48 static AVInputFormat *iformat = NULL;
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) {
74             index = (int) (log(val)/log(2)) / 10;
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
104 static const char *media_type_string(enum AVMediaType media_type)
105 {
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";
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];
122     AVMetadataTag *tag = NULL;
123     char a, b, c, d;
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
137         printf("codec_type=%s\n",         media_type_string(dec_ctx->codec_type));
138         printf("codec_time_base=%d/%d\n", dec_ctx->time_base.num, dec_ctx->time_base.den);
139
140         /* print AVI/FourCC tag */
141         a = dec_ctx->codec_tag     & 0xff;
142         b = dec_ctx->codec_tag>>8  & 0xff;
143         c = dec_ctx->codec_tag>>16 & 0xff;
144         d = dec_ctx->codec_tag>>24 & 0xff;
145         printf("codec_tag_string=");
146         if (isprint(a)) printf("%c", a); else printf("[%d]", a);
147         if (isprint(b)) printf("%c", b); else printf("[%d]", b);
148         if (isprint(c)) printf("%c", c); else printf("[%d]", c);
149         if (isprint(d)) printf("%c", d); else printf("[%d]", d);
150         printf("\ncodec_tag=0x%04x\n", dec_ctx->codec_tag);
151
152         switch (dec_ctx->codec_type) {
153         case AVMEDIA_TYPE_VIDEO:
154             printf("width=%d\n",                   dec_ctx->width);
155             printf("height=%d\n",                  dec_ctx->height);
156             printf("has_b_frames=%d\n",            dec_ctx->has_b_frames);
157             printf("sample_aspect_ratio=%d:%d\n",  dec_ctx->sample_aspect_ratio.num,
158                                                    dec_ctx->sample_aspect_ratio.den);
159             printf("display_aspect_ratio=%d:%d\n", dec_ctx->sample_aspect_ratio.num,
160                                                    dec_ctx->sample_aspect_ratio.den);
161             printf("pix_fmt=%s\n",                 dec_ctx->pix_fmt != PIX_FMT_NONE ?
162                    av_pix_fmt_descriptors[dec_ctx->pix_fmt].name : "unknown");
163             break;
164
165         case AVMEDIA_TYPE_AUDIO:
166             printf("sample_rate=%s\n",             value_string(val_str, sizeof(val_str),
167                                                                 dec_ctx->sample_rate,
168                                                                 unit_hertz_str));
169             printf("channels=%d\n",                dec_ctx->channels);
170             printf("bits_per_sample=%d\n",         av_get_bits_per_sample(dec_ctx->codec_id));
171             break;
172         }
173     } else {
174         printf("codec_type=unknown\n");
175     }
176
177     if (fmt_ctx->iformat->flags & AVFMT_SHOW_IDS)
178         printf("id=0x%x\n", stream->id);
179     printf("r_frame_rate=%d/%d\n",         stream->r_frame_rate.num,   stream->r_frame_rate.den);
180     printf("avg_frame_rate=%d/%d\n",       stream->avg_frame_rate.num, stream->avg_frame_rate.den);
181     printf("time_base=%d/%d\n",            stream->time_base.num,      stream->time_base.den);
182     if (stream->language[0])
183         printf("language=%s\n",            stream->language);
184     printf("start_time=%s\n",   time_value_string(val_str, sizeof(val_str), stream->start_time,
185                                                   &stream->time_base));
186     printf("duration=%s\n",     time_value_string(val_str, sizeof(val_str), stream->duration,
187                                                   &stream->time_base));
188
189     while ((tag = av_metadata_get(stream->metadata, "", tag, AV_METADATA_IGNORE_SUFFIX)))
190         printf("TAG:%s=%s\n", tag->key, tag->value);
191
192     printf("[/STREAM]\n");
193 }
194
195 static void show_format(AVFormatContext *fmt_ctx)
196 {
197     AVMetadataTag *tag = NULL;
198     char val_str[128];
199
200     printf("[FORMAT]\n");
201
202     printf("filename=%s\n",         fmt_ctx->filename);
203     printf("nb_streams=%d\n",       fmt_ctx->nb_streams);
204     printf("format_name=%s\n",      fmt_ctx->iformat->name);
205     printf("format_long_name=%s\n", fmt_ctx->iformat->long_name);
206     printf("start_time=%s\n",       time_value_string(val_str, sizeof(val_str), fmt_ctx->start_time,
207                                                       &AV_TIME_BASE_Q));
208     printf("duration=%s\n",         time_value_string(val_str, sizeof(val_str), fmt_ctx->duration,
209                                                       &AV_TIME_BASE_Q));
210     printf("size=%s\n",             value_string(val_str, sizeof(val_str), fmt_ctx->file_size,
211                                                  unit_byte_str));
212     printf("bit_rate=%s\n",         value_string(val_str, sizeof(val_str), fmt_ctx->bit_rate,
213                                                  unit_bit_per_second_str));
214
215     if (convert_tags)
216         av_metadata_conv(fmt_ctx, NULL, fmt_ctx->iformat->metadata_conv);
217     while ((tag = av_metadata_get(fmt_ctx->metadata, "", tag, AV_METADATA_IGNORE_SUFFIX)))
218         printf("TAG:%s=%s\n", tag->key, tag->value);
219
220     printf("[/FORMAT]\n");
221 }
222
223 static int open_input_file(AVFormatContext **fmt_ctx_ptr, const char *filename)
224 {
225     int err, i;
226     AVFormatContext *fmt_ctx;
227
228     fmt_ctx = avformat_alloc_context();
229
230     if ((err = av_open_input_file(&fmt_ctx, filename, iformat, 0, NULL)) < 0) {
231         print_error(filename, err);
232         return err;
233     }
234
235     /* fill the streams in the format context */
236     if ((err = av_find_stream_info(fmt_ctx)) < 0) {
237         print_error(filename, err);
238         return err;
239     }
240
241     dump_format(fmt_ctx, 0, filename, 0);
242
243     /* bind a decoder to each input stream */
244     for (i = 0; i < fmt_ctx->nb_streams; i++) {
245         AVStream *stream = fmt_ctx->streams[i];
246         AVCodec *codec;
247
248         if (!(codec = avcodec_find_decoder(stream->codec->codec_id))) {
249             fprintf(stderr, "Unsupported codec (id=%d) for input stream %d\n",
250                     stream->codec->codec_id, stream->index);
251         } else if (avcodec_open(stream->codec, codec) < 0) {
252             fprintf(stderr, "Error while opening codec for input stream %d\n",
253                     stream->index);
254         }
255     }
256
257     *fmt_ctx_ptr = fmt_ctx;
258     return 0;
259 }
260
261 static int probe_file(const char *filename)
262 {
263     AVFormatContext *fmt_ctx;
264     int ret, i;
265
266     if ((ret = open_input_file(&fmt_ctx, filename)))
267         return ret;
268
269     if (do_show_streams)
270         for (i = 0; i < fmt_ctx->nb_streams; i++)
271             show_stream(fmt_ctx, i);
272
273     if (do_show_format)
274         show_format(fmt_ctx);
275
276     av_close_input_file(fmt_ctx);
277     return 0;
278 }
279
280 static void show_usage(void)
281 {
282     printf("Simple multimedia streams analyzer\n");
283     printf("usage: ffprobe [OPTIONS] [INPUT_FILE]\n");
284     printf("\n");
285 }
286
287 static void opt_format(const char *arg)
288 {
289     iformat = av_find_input_format(arg);
290     if (!iformat) {
291         fprintf(stderr, "Unknown input format: %s\n", arg);
292         exit(1);
293     }
294 }
295
296 static void opt_input_file(const char *arg)
297 {
298     if (input_filename) {
299         fprintf(stderr, "Argument '%s' provided as input filename, but '%s' was already specified.\n",
300                 arg, input_filename);
301         exit(1);
302     }
303     if (!strcmp(arg, "-"))
304         arg = "pipe:";
305     input_filename = arg;
306 }
307
308 static void show_help(void)
309 {
310     show_usage();
311     show_help_options(options, "Main options:\n", 0, 0);
312     printf("\n");
313 }
314
315 static void opt_pretty(void)
316 {
317     show_value_unit              = 1;
318     use_value_prefix             = 1;
319     use_byte_value_binary_prefix = 1;
320     use_value_sexagesimal_format = 1;
321 }
322
323 static const OptionDef options[] = {
324 #include "cmdutils_common_opts.h"
325     { "convert_tags", OPT_BOOL, {(void*)&convert_tags}, "convert tag names to the FFmpeg generic tag names" },
326     { "f", HAS_ARG, {(void*)opt_format}, "force format", "format" },
327     { "unit", OPT_BOOL, {(void*)&show_value_unit}, "show unit of the displayed values" },
328     { "prefix", OPT_BOOL, {(void*)&use_value_prefix}, "use SI prefixes for the displayed values" },
329     { "byte_binary_prefix", OPT_BOOL, {(void*)&use_byte_value_binary_prefix},
330       "use binary prefixes for byte units" },
331     { "sexagesimal", OPT_BOOL,  {(void*)&use_value_sexagesimal_format},
332       "use sexagesimal format HOURS:MM:SS.MICROSECONDS for time units" },
333     { "pretty", 0, {(void*)&opt_pretty},
334       "prettify the format of displayed values, make it more human readable" },
335     { "show_format",  OPT_BOOL, {(void*)&do_show_format} , "show format/container info" },
336     { "show_streams", OPT_BOOL, {(void*)&do_show_streams}, "show streams info" },
337     { NULL, },
338 };
339
340 int main(int argc, char **argv)
341 {
342     av_register_all();
343 #if CONFIG_AVDEVICE
344     avdevice_register_all();
345 #endif
346
347     show_banner();
348     parse_options(argc, argv, options, opt_input_file);
349
350     if (!input_filename) {
351         show_usage();
352         fprintf(stderr, "You have to specify one input file.\n");
353         fprintf(stderr, "Use -h to get full help or, even better, run 'man ffprobe'.\n");
354         exit(1);
355     }
356
357     return probe_file(input_filename);
358 }