Statistics
| Branch: | Revision:

ffmpeg / ffprobe.c @ f1b267dd

History | View | Annotate | Download (11.8 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
#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;
45

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

    
49
static const char *unit_second_str          = "s"    ;
50
static const char *unit_hertz_str           = "Hz"   ;
51
static const char *unit_byte_str            = "byte" ;
52
static const char *unit_bit_per_second_str  = "bit/s";
53

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

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

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

    
86
    return buf;
87
}
88

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

    
97
    return buf;
98
}
99

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

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

    
121
    printf("[STREAM]\n");
122

    
123
    printf("index=%d\n",        stream->index);
124

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

    
133
        printf("codec_type=%s\n",         codec_type_string(dec_ctx->codec_type));
134
        printf("codec_time_base=%d/%d\n", dec_ctx->time_base.num, dec_ctx->time_base.den);
135

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

    
148
        switch (dec_ctx->codec_type) {
149
        case CODEC_TYPE_VIDEO:
150
            printf("width=%d\n",                   dec_ctx->width);
151
            printf("height=%d\n",                  dec_ctx->height);
152
            printf("has_b_frames=%d\n",            dec_ctx->has_b_frames);
153
            printf("sample_aspect_ratio=%d:%d\n",  dec_ctx->sample_aspect_ratio.num,
154
                                                   dec_ctx->sample_aspect_ratio.den);
155
            printf("display_aspect_ratio=%d:%d\n", dec_ctx->sample_aspect_ratio.num,
156
                                                   dec_ctx->sample_aspect_ratio.den);
157
            printf("pix_fmt=%s\n",                 dec_ctx->pix_fmt != PIX_FMT_NONE ?
158
                   av_pix_fmt_descriptors[dec_ctx->pix_fmt].name : "unknown");
159
            break;
160

    
161
        case CODEC_TYPE_AUDIO:
162
            printf("sample_rate=%s\n",             value_string(val_str, sizeof(val_str),
163
                                                                dec_ctx->sample_rate,
164
                                                                unit_hertz_str));
165
            printf("channels=%d\n",                dec_ctx->channels);
166
            printf("bits_per_sample=%d\n",         av_get_bits_per_sample(dec_ctx->codec_id));
167
            break;
168
        }
169
    } else {
170
        printf("codec_type=unknown\n");
171
    }
172

    
173
    if (fmt_ctx->iformat->flags & AVFMT_SHOW_IDS)
174
        printf("id=0x%x\n", stream->id);
175
    printf("r_frame_rate=%d/%d\n",         stream->r_frame_rate.num,   stream->r_frame_rate.den);
176
    printf("avg_frame_rate=%d/%d\n",       stream->avg_frame_rate.num, stream->avg_frame_rate.den);
177
    printf("time_base=%d/%d\n",            stream->time_base.num,      stream->time_base.den);
178
    if (stream->language[0])
179
        printf("language=%s\n",            stream->language);
180
    printf("start_time=%s\n",   time_value_string(val_str, sizeof(val_str), stream->start_time,
181
                                                  &stream->time_base));
182
    printf("duration=%s\n",     time_value_string(val_str, sizeof(val_str), stream->duration,
183
                                                  &stream->time_base));
184

    
185
    while ((tag = av_metadata_get(stream->metadata, "", tag, AV_METADATA_IGNORE_SUFFIX)))
186
        printf("%s=%s\n", tag->key, tag->value);
187

    
188
    printf("[/STREAM]\n");
189
}
190

    
191
static void show_format(AVFormatContext *fmt_ctx)
192
{
193
    AVMetadataTag *tag = NULL;
194
    char val_str[128];
195

    
196
    printf("[FORMAT]\n");
197

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

    
211
    while ((tag = av_metadata_get(fmt_ctx->metadata, "", tag, AV_METADATA_IGNORE_SUFFIX)))
212
        printf("TAG:%s=%s\n", tag->key, tag->value);
213

    
214
    printf("[/FORMAT]\n");
215
}
216

    
217
static int open_input_file(AVFormatContext **fmt_ctx_ptr, const char *filename)
218
{
219
    int err, i;
220
    AVFormatContext *fmt_ctx;
221

    
222
    fmt_ctx = avformat_alloc_context();
223

    
224
    if ((err = av_open_input_file(&fmt_ctx, filename, NULL, 0, NULL)) < 0) {
225
        print_error(filename, err);
226
        return err;
227
    }
228

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

    
235
    dump_format(fmt_ctx, 0, filename, 0);
236

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

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

    
251
    *fmt_ctx_ptr = fmt_ctx;
252
    return 0;
253
}
254

    
255
static int probe_file(const char *filename)
256
{
257
    AVFormatContext *fmt_ctx;
258
    int ret, i;
259

    
260
    if ((ret = open_input_file(&fmt_ctx, filename)))
261
        return ret;
262

    
263
    if (do_show_streams)
264
        for (i = 0; i < fmt_ctx->nb_streams; i++)
265
            show_stream(fmt_ctx, i);
266

    
267
    if (do_show_format)
268
        show_format(fmt_ctx);
269

    
270
    av_close_input_file(fmt_ctx);
271
    return 0;
272
}
273

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

    
281
static void opt_input_file(const char *filename)
282
{
283
    if (!strcmp(filename, "-"))
284
        filename = "pipe:";
285
    input_filename = filename;
286
}
287

    
288
static void show_help(void)
289
{
290
    show_usage();
291
    show_help_options(options, "Main options:\n", 0, 0);
292
    printf("\n");
293
}
294

    
295
static void opt_pretty(void)
296
{
297
    show_value_unit              = 1;
298
    use_value_prefix             = 1;
299
    use_byte_value_binary_prefix = 1;
300
    use_value_sexagesimal_format = 1;
301
}
302

    
303
static const OptionDef options[] = {
304
#include "cmdutils_common_opts.h"
305
    { "unit",          OPT_BOOL, {(void*)&show_value_unit},   "show unit of the displayed values" },
306
    { "prefix",        OPT_BOOL, {(void*)&use_value_prefix}, "use SI prefixes for the displayed values"  },
307
    { "byte_binary_prefix", OPT_BOOL, {(void*)&use_byte_value_binary_prefix},
308
      "use binary prefixes for byte units" },
309
    { "sexagesimal", OPT_BOOL,  {(void*)&use_value_sexagesimal_format},
310
      "use sexagesimal format HOURS:MM:SS.MICROSECONDS for time units" },
311
    { "pretty", 0, {(void*)&opt_pretty},
312
      "prettify the format of displayed values, make it more human readable" },
313
    { "show_format",  OPT_BOOL, {(void*)&do_show_format} , "show format/container info" },
314
    { "show_streams", OPT_BOOL, {(void*)&do_show_streams}, "show streams info"          },
315
    { NULL, },
316
};
317

    
318
int main(int argc, char **argv)
319
{
320
    av_register_all();
321

    
322
    show_banner();
323
    parse_options(argc, argv, options, opt_input_file);
324

    
325
    if (!input_filename) {
326
        show_usage();
327
        fprintf(stderr, "You have to specify one input file.\n");
328
        fprintf(stderr, "Use -h to get full help or, even better, run 'man ffprobe'.\n");
329
        exit(1);
330
    }
331

    
332
    return probe_file(input_filename);
333
}