Statistics
| Branch: | Revision:

ffmpeg / ffprobe.c @ 6580d5e3

History | View | Annotate | Download (12.3 KB)

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 show_value_unit              = 0;
38
static int use_value_prefix             = 0;
39
static int use_byte_value_binary_prefix = 0;
40
static int use_value_sexagesimal_format = 0;
41

    
42
/* globals */
43
static const OptionDef options[];
44

    
45
/* FFprobe context */
46
static const char *input_filename;
47
static AVInputFormat *iformat = NULL;
48

    
49
static const char *binary_unit_prefixes [] = { "", "Ki", "Mi", "Gi", "Ti", "Pi" };
50
static const char *decimal_unit_prefixes[] = { "", "K" , "M" , "G" , "T" , "P"  };
51

    
52
static const char *unit_second_str          = "s"    ;
53
static const char *unit_hertz_str           = "Hz"   ;
54
static const char *unit_byte_str            = "byte" ;
55
static const char *unit_bit_per_second_str  = "bit/s";
56

    
57
static char *value_string(char *buf, int buf_size, double val, const char *unit)
58
{
59
    if (unit == unit_second_str && use_value_sexagesimal_format) {
60
        double secs;
61
        int hours, mins;
62
        secs  = val;
63
        mins  = (int)secs / 60;
64
        secs  = secs - mins * 60;
65
        hours = mins / 60;
66
        mins %= 60;
67
        snprintf(buf, buf_size, "%d:%02d:%09.6f", hours, mins, secs);
68
    } else if (use_value_prefix) {
69
        const char *prefix_string;
70
        int index;
71

    
72
        if (unit == unit_byte_str && use_byte_value_binary_prefix) {
73
            index = (int) (log(val)/log(2)) / 10;
74
            index = av_clip(index, 0, FF_ARRAY_ELEMS(binary_unit_prefixes) -1);
75
            val /= pow(2, index*10);
76
            prefix_string = binary_unit_prefixes[index];
77
        } else {
78
            index = (int) (log10(val)) / 3;
79
            index = av_clip(index, 0, FF_ARRAY_ELEMS(decimal_unit_prefixes) -1);
80
            val /= pow(10, index*3);
81
            prefix_string = decimal_unit_prefixes[index];
82
        }
83

    
84
        snprintf(buf, buf_size, "%.3f %s%s", val, prefix_string, show_value_unit ? unit : "");
85
    } else {
86
        snprintf(buf, buf_size, "%f %s", val, show_value_unit ? unit : "");
87
    }
88

    
89
    return buf;
90
}
91

    
92
static char *time_value_string(char *buf, int buf_size, int64_t val, const AVRational *time_base)
93
{
94
    if (val == AV_NOPTS_VALUE) {
95
        snprintf(buf, buf_size, "N/A");
96
    } else {
97
        value_string(buf, buf_size, val * av_q2d(*time_base), unit_second_str);
98
    }
99

    
100
    return buf;
101
}
102

    
103
static const char *codec_type_string(enum CodecType codec_type)
104
{
105
    switch (codec_type) {
106
    case CODEC_TYPE_VIDEO:        return "video";
107
    case CODEC_TYPE_AUDIO:        return "audio";
108
    case CODEC_TYPE_DATA:         return "data";
109
    case CODEC_TYPE_SUBTITLE:     return "subtitle";
110
    case CODEC_TYPE_ATTACHMENT:   return "attachment";
111
    default:                      return "unknown";
112
    }
113
}
114

    
115
static void show_stream(AVFormatContext *fmt_ctx, int stream_idx)
116
{
117
    AVStream *stream = fmt_ctx->streams[stream_idx];
118
    AVCodecContext *dec_ctx;
119
    AVCodec *dec;
120
    char val_str[128];
121
    AVMetadataTag *tag;
122
    char a, b, c, d;
123

    
124
    printf("[STREAM]\n");
125

    
126
    printf("index=%d\n",        stream->index);
127

    
128
    if ((dec_ctx = stream->codec)) {
129
        if ((dec = dec_ctx->codec)) {
130
            printf("codec_name=%s\n",         dec->name);
131
            printf("codec_long_name=%s\n",    dec->long_name);
132
        } else {
133
            printf("codec_name=unknown\n");
134
        }
135

    
136
        printf("codec_type=%s\n",         codec_type_string(dec_ctx->codec_type));
137
        printf("codec_time_base=%d/%d\n", dec_ctx->time_base.num, dec_ctx->time_base.den);
138

    
139
        /* print AVI/FourCC tag */
140
        a = dec_ctx->codec_tag     & 0xff;
141
        b = dec_ctx->codec_tag>>8  & 0xff;
142
        c = dec_ctx->codec_tag>>16 & 0xff;
143
        d = dec_ctx->codec_tag>>24 & 0xff;
144
        printf("codec_tag_string=");
145
        if (isprint(a)) printf("%c", a); else printf("[%d]", a);
146
        if (isprint(b)) printf("%c", b); else printf("[%d]", b);
147
        if (isprint(c)) printf("%c", c); else printf("[%d]", c);
148
        if (isprint(d)) printf("%c", d); else printf("[%d]", d);
149
        printf("\ncodec_tag=0x%04x\n", dec_ctx->codec_tag);
150

    
151
        switch (dec_ctx->codec_type) {
152
        case CODEC_TYPE_VIDEO:
153
            printf("width=%d\n",                   dec_ctx->width);
154
            printf("height=%d\n",                  dec_ctx->height);
155
            printf("has_b_frames=%d\n",            dec_ctx->has_b_frames);
156
            printf("sample_aspect_ratio=%d:%d\n",  dec_ctx->sample_aspect_ratio.num,
157
                                                   dec_ctx->sample_aspect_ratio.den);
158
            printf("display_aspect_ratio=%d:%d\n", dec_ctx->sample_aspect_ratio.num,
159
                                                   dec_ctx->sample_aspect_ratio.den);
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

    
164
        case CODEC_TYPE_AUDIO:
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));
187

    
188
    while ((tag = av_metadata_get(stream->metadata, "", tag, AV_METADATA_IGNORE_SUFFIX)))
189
        printf("TAG:%s=%s\n", tag->key, tag->value);
190

    
191
    printf("[/STREAM]\n");
192
}
193

    
194
static void show_format(AVFormatContext *fmt_ctx)
195
{
196
    AVMetadataTag *tag = NULL;
197
    char val_str[128];
198

    
199
    printf("[FORMAT]\n");
200

    
201
    printf("filename=%s\n",         fmt_ctx->filename);
202
    printf("nb_streams=%d\n",       fmt_ctx->nb_streams);
203
    printf("format_name=%s\n",      fmt_ctx->iformat->name);
204
    printf("format_long_name=%s\n", fmt_ctx->iformat->long_name);
205
    printf("start_time=%s\n",       time_value_string(val_str, sizeof(val_str), fmt_ctx->start_time,
206
                                                      &AV_TIME_BASE_Q));
207
    printf("duration=%s\n",         time_value_string(val_str, sizeof(val_str), fmt_ctx->duration,
208
                                                      &AV_TIME_BASE_Q));
209
    printf("size=%s\n",             value_string(val_str, sizeof(val_str), fmt_ctx->file_size,
210
                                                 unit_byte_str));
211
    printf("bit_rate=%s\n",         value_string(val_str, sizeof(val_str), fmt_ctx->bit_rate,
212
                                                 unit_bit_per_second_str));
213

    
214
    while ((tag = av_metadata_get(fmt_ctx->metadata, "", tag, AV_METADATA_IGNORE_SUFFIX)))
215
        printf("TAG:%s=%s\n", tag->key, tag->value);
216

    
217
    printf("[/FORMAT]\n");
218
}
219

    
220
static int open_input_file(AVFormatContext **fmt_ctx_ptr, const char *filename)
221
{
222
    int err, i;
223
    AVFormatContext *fmt_ctx;
224

    
225
    fmt_ctx = avformat_alloc_context();
226

    
227
    if ((err = av_open_input_file(&fmt_ctx, filename, iformat, 0, NULL)) < 0) {
228
        print_error(filename, err);
229
        return err;
230
    }
231

    
232
    /* fill the streams in the format context */
233
    if ((err = av_find_stream_info(fmt_ctx)) < 0) {
234
        print_error(filename, err);
235
        return err;
236
    }
237

    
238
    dump_format(fmt_ctx, 0, filename, 0);
239

    
240
    /* bind a decoder to each input stream */
241
    for (i = 0; i < fmt_ctx->nb_streams; i++) {
242
        AVStream *stream = fmt_ctx->streams[i];
243
        AVCodec *codec;
244

    
245
        if (!(codec = avcodec_find_decoder(stream->codec->codec_id))) {
246
            fprintf(stderr, "Unsupported codec (id=%d) for input stream %d\n",
247
                    stream->codec->codec_id, stream->index);
248
        } else if (avcodec_open(stream->codec, codec) < 0) {
249
            fprintf(stderr, "Error while opening codec for input stream %d\n",
250
                    stream->index);
251
        }
252
    }
253

    
254
    *fmt_ctx_ptr = fmt_ctx;
255
    return 0;
256
}
257

    
258
static int probe_file(const char *filename)
259
{
260
    AVFormatContext *fmt_ctx;
261
    int ret, i;
262

    
263
    if ((ret = open_input_file(&fmt_ctx, filename)))
264
        return ret;
265

    
266
    if (do_show_streams)
267
        for (i = 0; i < fmt_ctx->nb_streams; i++)
268
            show_stream(fmt_ctx, i);
269

    
270
    if (do_show_format)
271
        show_format(fmt_ctx);
272

    
273
    av_close_input_file(fmt_ctx);
274
    return 0;
275
}
276

    
277
static void show_usage(void)
278
{
279
    printf("Simple multimedia streams analyzer\n");
280
    printf("usage: ffprobe [OPTIONS] [INPUT_FILE]\n");
281
    printf("\n");
282
}
283

    
284
static void opt_format(const char *arg)
285
{
286
    iformat = av_find_input_format(arg);
287
    if (!iformat) {
288
        fprintf(stderr, "Unknown input format: %s\n", arg);
289
        exit(1);
290
    }
291
}
292

    
293
static void opt_input_file(const char *arg)
294
{
295
    if (input_filename) {
296
        fprintf(stderr, "Argument '%s' provided as input filename, but '%s' was already specified.\n",
297
                arg, input_filename);
298
        exit(1);
299
    }
300
    if (!strcmp(arg, "-"))
301
        arg = "pipe:";
302
    input_filename = arg;
303
}
304

    
305
static void show_help(void)
306
{
307
    show_usage();
308
    show_help_options(options, "Main options:\n", 0, 0);
309
    printf("\n");
310
}
311

    
312
static void opt_pretty(void)
313
{
314
    show_value_unit              = 1;
315
    use_value_prefix             = 1;
316
    use_byte_value_binary_prefix = 1;
317
    use_value_sexagesimal_format = 1;
318
}
319

    
320
static const OptionDef options[] = {
321
#include "cmdutils_common_opts.h"
322
    { "f", HAS_ARG, {(void*)opt_format}, "force format", "format" },
323
    { "unit", OPT_BOOL, {(void*)&show_value_unit}, "show unit of the displayed values" },
324
    { "prefix", OPT_BOOL, {(void*)&use_value_prefix}, "use SI prefixes for the displayed values" },
325
    { "byte_binary_prefix", OPT_BOOL, {(void*)&use_byte_value_binary_prefix},
326
      "use binary prefixes for byte units" },
327
    { "sexagesimal", OPT_BOOL,  {(void*)&use_value_sexagesimal_format},
328
      "use sexagesimal format HOURS:MM:SS.MICROSECONDS for time units" },
329
    { "pretty", 0, {(void*)&opt_pretty},
330
      "prettify the format of displayed values, make it more human readable" },
331
    { "show_format",  OPT_BOOL, {(void*)&do_show_format} , "show format/container info" },
332
    { "show_streams", OPT_BOOL, {(void*)&do_show_streams}, "show streams info" },
333
    { NULL, },
334
};
335

    
336
int main(int argc, char **argv)
337
{
338
    av_register_all();
339
#if CONFIG_AVDEVICE
340
    avdevice_register_all();
341
#endif
342

    
343
    show_banner();
344
    parse_options(argc, argv, options, opt_input_file);
345

    
346
    if (!input_filename) {
347
        show_usage();
348
        fprintf(stderr, "You have to specify one input file.\n");
349
        fprintf(stderr, "Use -h to get full help or, even better, run 'man ffprobe'.\n");
350
        exit(1);
351
    }
352

    
353
    return probe_file(input_filename);
354
}