]> git.sesse.net Git - ffmpeg/blobdiff - ffprobe.c
avutil: Add missing test programs to Makefile.
[ffmpeg] / ffprobe.c
index 9a5951a6568632f70f4e5bd3840239c29b0b25c9..edda454cde79eebe49ec821581dfa99affb0768d 100644 (file)
--- a/ffprobe.c
+++ b/ffprobe.c
@@ -1,35 +1,39 @@
 /*
- * FFprobe : Simple Media Prober based on the FFmpeg libraries
+ * ffprobe : Simple Media Prober based on the Libav libraries
  * Copyright (c) 2007-2010 Stefano Sabatini
  *
- * This file is part of FFmpeg.
+ * This file is part of Libav.
  *
- * FFmpeg is free software; you can redistribute it and/or
+ * Libav is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
  * version 2.1 of the License, or (at your option) any later version.
  *
- * FFmpeg is distributed in the hope that it will be useful,
+ * Libav is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
- * License along with FFmpeg; if not, write to the Free Software
+ * License along with Libav; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
-#undef HAVE_AV_CONFIG_H
+#include "config.h"
+
 #include "libavformat/avformat.h"
 #include "libavcodec/avcodec.h"
-#include "libavcodec/opt.h"
+#include "libavutil/opt.h"
 #include "libavutil/pixdesc.h"
+#include "libavutil/dict.h"
+#include "libavdevice/avdevice.h"
 #include "cmdutils.h"
 
-const char program_name[] = "FFprobe";
+const char program_name[] = "ffprobe";
 const int program_birth_year = 2007;
 
 static int do_show_format  = 0;
+static int do_show_packets = 0;
 static int do_show_streams = 0;
 
 static int show_value_unit              = 0;
@@ -42,6 +46,7 @@ static const OptionDef options[];
 
 /* FFprobe context */
 static const char *input_filename;
+static AVInputFormat *iformat = NULL;
 
 static const char *binary_unit_prefixes [] = { "", "Ki", "Mi", "Gi", "Ti", "Pi" };
 static const char *decimal_unit_prefixes[] = { "", "K" , "M" , "G" , "T" , "P"  };
@@ -97,26 +102,67 @@ static char *time_value_string(char *buf, int buf_size, int64_t val, const AVRat
     return buf;
 }
 
-static const char *codec_type_string(enum CodecType codec_type)
+static char *ts_value_string (char *buf, int buf_size, int64_t ts)
+{
+    if (ts == AV_NOPTS_VALUE) {
+        snprintf(buf, buf_size, "N/A");
+    } else {
+        snprintf(buf, buf_size, "%"PRId64, ts);
+    }
+
+    return buf;
+}
+
+static const char *media_type_string(enum AVMediaType media_type)
 {
-    switch (codec_type) {
-    case CODEC_TYPE_VIDEO:        return "video";
-    case CODEC_TYPE_AUDIO:        return "audio";
-    case CODEC_TYPE_DATA:         return "data";
-    case CODEC_TYPE_SUBTITLE:     return "subtitle";
-    case CODEC_TYPE_ATTACHMENT:   return "attachment";
+    switch (media_type) {
+    case AVMEDIA_TYPE_VIDEO:      return "video";
+    case AVMEDIA_TYPE_AUDIO:      return "audio";
+    case AVMEDIA_TYPE_DATA:       return "data";
+    case AVMEDIA_TYPE_SUBTITLE:   return "subtitle";
+    case AVMEDIA_TYPE_ATTACHMENT: return "attachment";
     default:                      return "unknown";
     }
 }
 
+static void show_packet(AVFormatContext *fmt_ctx, AVPacket *pkt)
+{
+    char val_str[128];
+    AVStream *st = fmt_ctx->streams[pkt->stream_index];
+
+    printf("[PACKET]\n");
+    printf("codec_type=%s\n"   , media_type_string(st->codec->codec_type));
+    printf("stream_index=%d\n" , pkt->stream_index);
+    printf("pts=%s\n"          , ts_value_string  (val_str, sizeof(val_str), pkt->pts));
+    printf("pts_time=%s\n"     , time_value_string(val_str, sizeof(val_str), pkt->pts, &st->time_base));
+    printf("dts=%s\n"          , ts_value_string  (val_str, sizeof(val_str), pkt->dts));
+    printf("dts_time=%s\n"     , time_value_string(val_str, sizeof(val_str), pkt->dts, &st->time_base));
+    printf("duration=%s\n"     , ts_value_string  (val_str, sizeof(val_str), pkt->duration));
+    printf("duration_time=%s\n", time_value_string(val_str, sizeof(val_str), pkt->duration, &st->time_base));
+    printf("size=%s\n"         , value_string     (val_str, sizeof(val_str), pkt->size, unit_byte_str));
+    printf("pos=%"PRId64"\n"   , pkt->pos);
+    printf("flags=%c\n"        , pkt->flags & AV_PKT_FLAG_KEY ? 'K' : '_');
+    printf("[/PACKET]\n");
+}
+
+static void show_packets(AVFormatContext *fmt_ctx)
+{
+    AVPacket pkt;
+
+    av_init_packet(&pkt);
+
+    while (!av_read_frame(fmt_ctx, &pkt))
+        show_packet(fmt_ctx, &pkt);
+}
+
 static void show_stream(AVFormatContext *fmt_ctx, int stream_idx)
 {
     AVStream *stream = fmt_ctx->streams[stream_idx];
     AVCodecContext *dec_ctx;
     AVCodec *dec;
     char val_str[128];
-    AVMetadataTag *tag;
-    char a, b, c, d;
+    AVDictionaryEntry *tag = NULL;
+    AVRational display_aspect_ratio;
 
     printf("[STREAM]\n");
 
@@ -130,35 +176,34 @@ static void show_stream(AVFormatContext *fmt_ctx, int stream_idx)
             printf("codec_name=unknown\n");
         }
 
-        printf("codec_type=%s\n",         codec_type_string(dec_ctx->codec_type));
+        printf("codec_type=%s\n",         media_type_string(dec_ctx->codec_type));
         printf("codec_time_base=%d/%d\n", dec_ctx->time_base.num, dec_ctx->time_base.den);
 
         /* print AVI/FourCC tag */
-        a = dec_ctx->codec_tag     & 0xff;
-        b = dec_ctx->codec_tag>>8  & 0xff;
-        c = dec_ctx->codec_tag>>16 & 0xff;
-        d = dec_ctx->codec_tag>>24 & 0xff;
-        printf("codec_tag_string=");
-        if (isprint(a)) printf("%c", a); else printf("[%d]", a);
-        if (isprint(b)) printf("%c", b); else printf("[%d]", b);
-        if (isprint(c)) printf("%c", c); else printf("[%d]", c);
-        if (isprint(d)) printf("%c", d); else printf("[%d]", d);
-        printf("\ncodec_tag=0x%04x\n", dec_ctx->codec_tag);
+        av_get_codec_tag_string(val_str, sizeof(val_str), dec_ctx->codec_tag);
+        printf("codec_tag_string=%s\n", val_str);
+        printf("codec_tag=0x%04x\n", dec_ctx->codec_tag);
 
         switch (dec_ctx->codec_type) {
-        case CODEC_TYPE_VIDEO:
+        case AVMEDIA_TYPE_VIDEO:
             printf("width=%d\n",                   dec_ctx->width);
             printf("height=%d\n",                  dec_ctx->height);
             printf("has_b_frames=%d\n",            dec_ctx->has_b_frames);
-            printf("sample_aspect_ratio=%d:%d\n",  dec_ctx->sample_aspect_ratio.num,
-                                                   dec_ctx->sample_aspect_ratio.den);
-            printf("display_aspect_ratio=%d:%d\n", dec_ctx->sample_aspect_ratio.num,
-                                                   dec_ctx->sample_aspect_ratio.den);
+            if (dec_ctx->sample_aspect_ratio.num) {
+                printf("sample_aspect_ratio=%d:%d\n", dec_ctx->sample_aspect_ratio.num,
+                                                      dec_ctx->sample_aspect_ratio.den);
+                av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
+                          dec_ctx->width  * dec_ctx->sample_aspect_ratio.num,
+                          dec_ctx->height * dec_ctx->sample_aspect_ratio.den,
+                          1024*1024);
+                printf("display_aspect_ratio=%d:%d\n", display_aspect_ratio.num,
+                                                       display_aspect_ratio.den);
+            }
             printf("pix_fmt=%s\n",                 dec_ctx->pix_fmt != PIX_FMT_NONE ?
                    av_pix_fmt_descriptors[dec_ctx->pix_fmt].name : "unknown");
             break;
 
-        case CODEC_TYPE_AUDIO:
+        case AVMEDIA_TYPE_AUDIO:
             printf("sample_rate=%s\n",             value_string(val_str, sizeof(val_str),
                                                                 dec_ctx->sample_rate,
                                                                 unit_hertz_str));
@@ -175,22 +220,22 @@ static void show_stream(AVFormatContext *fmt_ctx, int stream_idx)
     printf("r_frame_rate=%d/%d\n",         stream->r_frame_rate.num,   stream->r_frame_rate.den);
     printf("avg_frame_rate=%d/%d\n",       stream->avg_frame_rate.num, stream->avg_frame_rate.den);
     printf("time_base=%d/%d\n",            stream->time_base.num,      stream->time_base.den);
-    if (stream->language[0])
-        printf("language=%s\n",            stream->language);
     printf("start_time=%s\n",   time_value_string(val_str, sizeof(val_str), stream->start_time,
                                                   &stream->time_base));
     printf("duration=%s\n",     time_value_string(val_str, sizeof(val_str), stream->duration,
                                                   &stream->time_base));
+    if (stream->nb_frames)
+        printf("nb_frames=%"PRId64"\n",    stream->nb_frames);
 
-    while ((tag = av_metadata_get(stream->metadata, "", tag, AV_METADATA_IGNORE_SUFFIX)))
-        printf("%s=%s\n", tag->key, tag->value);
+    while ((tag = av_dict_get(stream->metadata, "", tag, AV_DICT_IGNORE_SUFFIX)))
+        printf("TAG:%s=%s\n", tag->key, tag->value);
 
     printf("[/STREAM]\n");
 }
 
 static void show_format(AVFormatContext *fmt_ctx)
 {
-    AVMetadataTag *tag = NULL;
+    AVDictionaryEntry *tag = NULL;
     char val_str[128];
 
     printf("[FORMAT]\n");
@@ -208,7 +253,7 @@ static void show_format(AVFormatContext *fmt_ctx)
     printf("bit_rate=%s\n",         value_string(val_str, sizeof(val_str), fmt_ctx->bit_rate,
                                                  unit_bit_per_second_str));
 
-    while ((tag = av_metadata_get(fmt_ctx->metadata, "", tag, AV_METADATA_IGNORE_SUFFIX)))
+    while ((tag = av_dict_get(fmt_ctx->metadata, "", tag, AV_DICT_IGNORE_SUFFIX)))
         printf("TAG:%s=%s\n", tag->key, tag->value);
 
     printf("[/FORMAT]\n");
@@ -217,14 +262,18 @@ static void show_format(AVFormatContext *fmt_ctx)
 static int open_input_file(AVFormatContext **fmt_ctx_ptr, const char *filename)
 {
     int err, i;
-    AVFormatContext *fmt_ctx;
-
-    fmt_ctx = avformat_alloc_context();
+    AVFormatContext *fmt_ctx = NULL;
+    AVDictionaryEntry *t;
 
-    if ((err = av_open_input_file(&fmt_ctx, filename, NULL, 0, NULL)) < 0) {
+    if ((err = avformat_open_input(&fmt_ctx, filename, iformat, &format_opts)) < 0) {
         print_error(filename, err);
         return err;
     }
+    if ((t = av_dict_get(format_opts, "", NULL, AV_DICT_IGNORE_SUFFIX))) {
+        av_log(NULL, AV_LOG_ERROR, "Option %s not found.\n", t->key);
+        return AVERROR_OPTION_NOT_FOUND;
+    }
+
 
     /* fill the streams in the format context */
     if ((err = av_find_stream_info(fmt_ctx)) < 0) {
@@ -232,7 +281,7 @@ static int open_input_file(AVFormatContext **fmt_ctx_ptr, const char *filename)
         return err;
     }
 
-    dump_format(fmt_ctx, 0, filename, 0);
+    av_dump_format(fmt_ctx, 0, filename, 0);
 
     /* bind a decoder to each input stream */
     for (i = 0; i < fmt_ctx->nb_streams; i++) {
@@ -240,7 +289,7 @@ static int open_input_file(AVFormatContext **fmt_ctx_ptr, const char *filename)
         AVCodec *codec;
 
         if (!(codec = avcodec_find_decoder(stream->codec->codec_id))) {
-            fprintf(stderr, "Unsupported codec (id=%d) for input stream %d\n",
+            fprintf(stderr, "Unsupported codec with id %d for input stream %d\n",
                     stream->codec->codec_id, stream->index);
         } else if (avcodec_open(stream->codec, codec) < 0) {
             fprintf(stderr, "Error while opening codec for input stream %d\n",
@@ -260,6 +309,9 @@ static int probe_file(const char *filename)
     if ((ret = open_input_file(&fmt_ctx, filename)))
         return ret;
 
+    if (do_show_packets)
+        show_packets(fmt_ctx);
+
     if (do_show_streams)
         for (i = 0; i < fmt_ctx->nb_streams; i++)
             show_stream(fmt_ctx, i);
@@ -278,18 +330,36 @@ static void show_usage(void)
     printf("\n");
 }
 
-static void opt_input_file(const char *filename)
+static int opt_format(const char *opt, const char *arg)
 {
-    if (!strcmp(filename, "-"))
-        filename = "pipe:";
-    input_filename = filename;
+    iformat = av_find_input_format(arg);
+    if (!iformat) {
+        fprintf(stderr, "Unknown input format: %s\n", arg);
+        return AVERROR(EINVAL);
+    }
+    return 0;
+}
+
+static void opt_input_file(const char *arg)
+{
+    if (input_filename) {
+        fprintf(stderr, "Argument '%s' provided as input filename, but '%s' was already specified.\n",
+                arg, input_filename);
+        exit(1);
+    }
+    if (!strcmp(arg, "-"))
+        arg = "pipe:";
+    input_filename = arg;
 }
 
 static void show_help(void)
 {
+    av_log_set_callback(log_callback_help);
     show_usage();
     show_help_options(options, "Main options:\n", 0, 0);
     printf("\n");
+    av_opt_show2(avformat_opts, NULL,
+                 AV_OPT_FLAG_DECODING_PARAM, 0);
 }
 
 static void opt_pretty(void)
@@ -302,8 +372,9 @@ static void opt_pretty(void)
 
 static const OptionDef options[] = {
 #include "cmdutils_common_opts.h"
-    { "unit",          OPT_BOOL, {(void*)&show_value_unit},   "show unit of the displayed values" },
-    { "prefix",        OPT_BOOL, {(void*)&use_value_prefix}, "use SI prefixes for the displayed values"  },
+    { "f", HAS_ARG, {(void*)opt_format}, "force format", "format" },
+    { "unit", OPT_BOOL, {(void*)&show_value_unit}, "show unit of the displayed values" },
+    { "prefix", OPT_BOOL, {(void*)&use_value_prefix}, "use SI prefixes for the displayed values" },
     { "byte_binary_prefix", OPT_BOOL, {(void*)&use_byte_value_binary_prefix},
       "use binary prefixes for byte units" },
     { "sexagesimal", OPT_BOOL,  {(void*)&use_value_sexagesimal_format},
@@ -311,13 +382,23 @@ static const OptionDef options[] = {
     { "pretty", 0, {(void*)&opt_pretty},
       "prettify the format of displayed values, make it more human readable" },
     { "show_format",  OPT_BOOL, {(void*)&do_show_format} , "show format/container info" },
-    { "show_streams", OPT_BOOL, {(void*)&do_show_streams}, "show streams info"          },
+    { "show_packets", OPT_BOOL, {(void*)&do_show_packets}, "show packets info" },
+    { "show_streams", OPT_BOOL, {(void*)&do_show_streams}, "show streams info" },
+    { "default", HAS_ARG | OPT_AUDIO | OPT_VIDEO | OPT_EXPERT, {(void*)opt_default}, "generic catch all option", "" },
     { NULL, },
 };
 
 int main(int argc, char **argv)
 {
+    int ret;
+
     av_register_all();
+    init_opts();
+#if CONFIG_AVDEVICE
+    avdevice_register_all();
+#endif
+
+    avformat_opts = avformat_alloc_context();
 
     show_banner();
     parse_options(argc, argv, options, opt_input_file);
@@ -329,5 +410,9 @@ int main(int argc, char **argv)
         exit(1);
     }
 
-    return probe_file(input_filename);
+    ret = probe_file(input_filename);
+
+    av_free(avformat_opts);
+
+    return ret;
 }