2 * Copyright (C) 2009 Edward Hervey <edward.hervey@collabora.co.uk>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
27 #include <gst/pbutils/pbutils.h>
29 static gboolean async = FALSE;
30 static gboolean silent = FALSE;
31 static gboolean show_toc = FALSE;
32 static gboolean verbose = FALSE;
42 my_g_string_append_printf (GString * str, int depth, const gchar * format, ...)
47 g_string_append (str, " ");
50 va_start (args, format);
51 g_string_append_vprintf (str, format, args);
56 gst_stream_audio_information_to_string (GstDiscovererStreamInfo * info,
59 GstDiscovererAudioInfo *audio_info;
64 const GstTagList *tags;
67 g_return_val_if_fail (info != NULL, NULL);
69 s = g_string_sized_new (len);
71 my_g_string_append_printf (s, depth, "Codec:\n");
72 caps = gst_discoverer_stream_info_get_caps (info);
73 tmp = gst_caps_to_string (caps);
74 gst_caps_unref (caps);
75 my_g_string_append_printf (s, depth, " %s\n", tmp);
78 my_g_string_append_printf (s, depth, "Additional info:\n");
79 if (gst_discoverer_stream_info_get_misc (info)) {
80 tmp = gst_structure_to_string (gst_discoverer_stream_info_get_misc (info));
81 my_g_string_append_printf (s, depth, " %s\n", tmp);
84 my_g_string_append_printf (s, depth, " None\n");
87 audio_info = (GstDiscovererAudioInfo *) info;
88 ctmp = gst_discoverer_audio_info_get_language (audio_info);
89 my_g_string_append_printf (s, depth, "Language: %s\n",
90 ctmp ? ctmp : "<unknown>");
91 my_g_string_append_printf (s, depth, "Channels: %u\n",
92 gst_discoverer_audio_info_get_channels (audio_info));
93 my_g_string_append_printf (s, depth, "Sample rate: %u\n",
94 gst_discoverer_audio_info_get_sample_rate (audio_info));
95 my_g_string_append_printf (s, depth, "Depth: %u\n",
96 gst_discoverer_audio_info_get_depth (audio_info));
98 my_g_string_append_printf (s, depth, "Bitrate: %u\n",
99 gst_discoverer_audio_info_get_bitrate (audio_info));
100 my_g_string_append_printf (s, depth, "Max bitrate: %u\n",
101 gst_discoverer_audio_info_get_max_bitrate (audio_info));
103 my_g_string_append_printf (s, depth, "Tags:\n");
104 tags = gst_discoverer_stream_info_get_tags (info);
106 tmp = gst_structure_to_string ((GstStructure *) tags);
107 my_g_string_append_printf (s, depth, " %s\n", tmp);
110 my_g_string_append_printf (s, depth, " None\n");
113 my_g_string_append_printf (s, depth, "\n");
115 return g_string_free (s, FALSE);
119 gst_stream_video_information_to_string (GstDiscovererStreamInfo * info,
122 GstDiscovererVideoInfo *video_info;
126 const GstStructure *misc;
127 const GstTagList *tags;
130 g_return_val_if_fail (info != NULL, NULL);
132 s = g_string_sized_new (len);
134 my_g_string_append_printf (s, depth, "Codec:\n");
135 caps = gst_discoverer_stream_info_get_caps (info);
136 tmp = gst_caps_to_string (caps);
137 gst_caps_unref (caps);
138 my_g_string_append_printf (s, depth, " %s\n", tmp);
141 my_g_string_append_printf (s, depth, "Additional info:\n");
142 misc = gst_discoverer_stream_info_get_misc (info);
144 tmp = gst_structure_to_string (misc);
145 my_g_string_append_printf (s, depth, " %s\n", tmp);
148 my_g_string_append_printf (s, depth, " None\n");
151 video_info = (GstDiscovererVideoInfo *) info;
152 my_g_string_append_printf (s, depth, "Width: %u\n",
153 gst_discoverer_video_info_get_width (video_info));
154 my_g_string_append_printf (s, depth, "Height: %u\n",
155 gst_discoverer_video_info_get_height (video_info));
156 my_g_string_append_printf (s, depth, "Depth: %u\n",
157 gst_discoverer_video_info_get_depth (video_info));
159 my_g_string_append_printf (s, depth, "Frame rate: %u/%u\n",
160 gst_discoverer_video_info_get_framerate_num (video_info),
161 gst_discoverer_video_info_get_framerate_denom (video_info));
163 my_g_string_append_printf (s, depth, "Pixel aspect ratio: %u/%u\n",
164 gst_discoverer_video_info_get_par_num (video_info),
165 gst_discoverer_video_info_get_par_denom (video_info));
167 my_g_string_append_printf (s, depth, "Interlaced: %s\n",
168 gst_discoverer_video_info_is_interlaced (video_info) ? "true" : "false");
170 my_g_string_append_printf (s, depth, "Bitrate: %u\n",
171 gst_discoverer_video_info_get_bitrate (video_info));
172 my_g_string_append_printf (s, depth, "Max bitrate: %u\n",
173 gst_discoverer_video_info_get_max_bitrate (video_info));
175 my_g_string_append_printf (s, depth, "Tags:\n");
176 tags = gst_discoverer_stream_info_get_tags (info);
178 tmp = gst_structure_to_string ((GstStructure *) tags);
179 my_g_string_append_printf (s, depth, " %s\n", tmp);
182 my_g_string_append_printf (s, depth, " None\n");
185 my_g_string_append_printf (s, depth, "\n");
187 return g_string_free (s, FALSE);
191 gst_stream_subtitle_information_to_string (GstDiscovererStreamInfo * info,
194 GstDiscovererSubtitleInfo *subtitle_info;
199 const GstTagList *tags;
202 g_return_val_if_fail (info != NULL, NULL);
204 s = g_string_sized_new (len);
206 my_g_string_append_printf (s, depth, "Codec:\n");
207 caps = gst_discoverer_stream_info_get_caps (info);
208 tmp = gst_caps_to_string (caps);
209 gst_caps_unref (caps);
210 my_g_string_append_printf (s, depth, " %s\n", tmp);
213 my_g_string_append_printf (s, depth, "Additional info:\n");
214 if (gst_discoverer_stream_info_get_misc (info)) {
215 tmp = gst_structure_to_string (gst_discoverer_stream_info_get_misc (info));
216 my_g_string_append_printf (s, depth, " %s\n", tmp);
219 my_g_string_append_printf (s, depth, " None\n");
222 subtitle_info = (GstDiscovererSubtitleInfo *) info;
223 ctmp = gst_discoverer_subtitle_info_get_language (subtitle_info);
224 my_g_string_append_printf (s, depth, "Language: %s\n",
225 ctmp ? ctmp : "<unknown>");
227 my_g_string_append_printf (s, depth, "Tags:\n");
228 tags = gst_discoverer_stream_info_get_tags (info);
230 tmp = gst_structure_to_string ((GstStructure *) tags);
231 my_g_string_append_printf (s, depth, " %s\n", tmp);
234 my_g_string_append_printf (s, depth, " None\n");
237 my_g_string_append_printf (s, depth, "\n");
239 return g_string_free (s, FALSE);
243 print_stream_info (GstDiscovererStreamInfo * info, void *depth)
248 caps = gst_discoverer_stream_info_get_caps (info);
251 if (gst_caps_is_fixed (caps) && !verbose)
252 desc = gst_pb_utils_get_codec_description (caps);
254 desc = gst_caps_to_string (caps);
255 gst_caps_unref (caps);
258 g_print ("%*s%s: %s\n", 2 * GPOINTER_TO_INT (depth), " ",
259 gst_discoverer_stream_info_get_stream_type_nick (info), desc);
267 if (GST_IS_DISCOVERER_AUDIO_INFO (info))
269 gst_stream_audio_information_to_string (info,
270 GPOINTER_TO_INT (depth) + 1);
271 else if (GST_IS_DISCOVERER_VIDEO_INFO (info))
273 gst_stream_video_information_to_string (info,
274 GPOINTER_TO_INT (depth) + 1);
275 else if (GST_IS_DISCOVERER_SUBTITLE_INFO (info))
277 gst_stream_subtitle_information_to_string (info,
278 GPOINTER_TO_INT (depth) + 1);
280 g_print ("%s", desc);
287 print_topology (GstDiscovererStreamInfo * info, gint depth)
289 GstDiscovererStreamInfo *next;
294 print_stream_info (info, GINT_TO_POINTER (depth));
296 next = gst_discoverer_stream_info_get_next (info);
298 print_topology (next, depth + 1);
299 gst_discoverer_stream_info_unref (next);
300 } else if (GST_IS_DISCOVERER_CONTAINER_INFO (info)) {
301 GList *tmp, *streams;
304 gst_discoverer_container_info_get_streams (GST_DISCOVERER_CONTAINER_INFO
306 for (tmp = streams; tmp; tmp = tmp->next) {
307 GstDiscovererStreamInfo *tmpinf = (GstDiscovererStreamInfo *) tmp->data;
308 print_topology (tmpinf, depth + 1);
310 gst_discoverer_stream_info_list_free (streams);
315 print_tag_each (GQuark field_id, const GValue * value, gpointer user_data)
317 gint tab = GPOINTER_TO_INT (user_data);
320 if (G_VALUE_HOLDS_STRING (value))
321 ser = g_value_dup_string (value);
322 else if (GST_VALUE_HOLDS_SAMPLE (value)) {
323 GstSample *smpl = gst_value_get_sample (value);
324 GstBuffer *buf = gst_sample_get_buffer (smpl);
325 GstCaps *caps = gst_sample_get_caps (smpl);
328 caps_str = caps ? gst_caps_to_string (caps) : g_strdup ("unknown");
330 g_strdup_printf ("<GstSample [%" G_GSIZE_FORMAT " bytes, type %s]>",
331 gst_buffer_get_size (buf), caps_str);
334 ser = gst_value_serialize (value);
336 g_print ("%*s%s: %s\n", tab, " ",
337 gst_tag_get_nick (g_quark_to_string (field_id)), ser);
344 print_tag_foreach (const GstTagList * tags, const gchar * tag,
349 gint depth = GPOINTER_TO_INT (user_data);
351 gst_tag_list_copy_value (&val, tags, tag);
353 if (G_VALUE_HOLDS_STRING (&val))
354 str = g_value_dup_string (&val);
356 str = gst_value_serialize (&val);
358 g_print ("%*s%s: %s\n", 2 * depth, " ", gst_tag_get_nick (tag), str);
361 g_value_unset (&val);
364 #define MAX_INDENT 40
367 print_toc_entry (gpointer data, gpointer user_data)
369 GstTocEntry *entry = (GstTocEntry *) data;
370 gint depth = GPOINTER_TO_INT (user_data);
371 guint indent = MIN (GPOINTER_TO_UINT (user_data), MAX_INDENT);
374 gst_toc_entry_get_start_stop (entry, &start, &stop);
375 g_print ("%*s%s: start: %" GST_TIME_FORMAT " stop: %" GST_TIME_FORMAT "\n",
376 depth, " ", gst_toc_entry_type_get_nick (entry->type),
377 GST_TIME_ARGS (start), GST_TIME_ARGS (stop));
381 if (entry->type == GST_TOC_ENTRY_TYPE_CHAPTER)
382 g_print ("%*sTags:\n", 2 * depth, " ");
383 gst_tag_list_foreach (entry->tags, print_tag_foreach,
384 GUINT_TO_POINTER (indent));
386 /* loop over sub-toc entries */
387 g_list_foreach (entry->subentries, print_toc_entry,
388 GUINT_TO_POINTER (indent));
392 print_properties (GstDiscovererInfo * info, gint tab)
394 const GstTagList *tags;
397 g_print ("%*sDuration: %" GST_TIME_FORMAT "\n", tab + 1, " ",
398 GST_TIME_ARGS (gst_discoverer_info_get_duration (info)));
399 g_print ("%*sSeekable: %s\n", tab + 1, " ",
400 (gst_discoverer_info_get_seekable (info) ? "yes" : "no"));
401 if ((tags = gst_discoverer_info_get_tags (info))) {
402 g_print ("%*sTags: \n", tab + 1, " ");
403 gst_structure_foreach ((const GstStructure *) tags, print_tag_each,
404 GINT_TO_POINTER (tab + 5));
406 if (show_toc && (toc = gst_discoverer_info_get_toc (info))) {
407 g_print ("%*sTOC: \n", tab + 1, " ");
408 g_list_foreach (toc->entries, print_toc_entry, GUINT_TO_POINTER (tab + 5));
413 print_info (GstDiscovererInfo * info, GError * err)
415 GstDiscovererResult result = gst_discoverer_info_get_result (info);
416 GstDiscovererStreamInfo *sinfo;
418 g_print ("Done discovering %s\n", gst_discoverer_info_get_uri (info));
420 case GST_DISCOVERER_OK:
424 case GST_DISCOVERER_URI_INVALID:
426 g_print ("URI is not valid\n");
429 case GST_DISCOVERER_ERROR:
431 g_print ("An error was encountered while discovering the file\n");
432 g_print (" %s\n", err->message);
435 case GST_DISCOVERER_TIMEOUT:
437 g_print ("Analyzing URI timed out\n");
440 case GST_DISCOVERER_BUSY:
442 g_print ("Discoverer was busy\n");
445 case GST_DISCOVERER_MISSING_PLUGINS:
447 g_print ("Missing plugins\n");
450 gst_structure_to_string (gst_discoverer_info_get_misc (info));
451 g_print (" (%s)\n", tmp);
458 if ((sinfo = gst_discoverer_info_get_stream_info (info))) {
459 g_print ("\nTopology:\n");
460 print_topology (sinfo, 1);
461 g_print ("\nProperties:\n");
462 print_properties (info, 1);
463 gst_discoverer_stream_info_unref (sinfo);
470 process_file (GstDiscoverer * dc, const gchar * filename)
475 GstDiscovererInfo *info;
477 if (!gst_uri_is_valid (filename)) {
478 /* Recurse into directories */
479 if ((dir = g_dir_open (filename, 0, NULL))) {
482 while ((entry = g_dir_read_name (dir))) {
484 path = g_strconcat (filename, G_DIR_SEPARATOR_S, entry, NULL);
485 process_file (dc, path);
493 if (!g_path_is_absolute (filename)) {
496 cur_dir = g_get_current_dir ();
497 path = g_build_filename (cur_dir, filename, NULL);
500 path = g_strdup (filename);
503 uri = g_filename_to_uri (path, NULL, &err);
508 g_warning ("Couldn't convert filename to URI: %s\n", err->message);
513 uri = g_strdup (filename);
516 if (async == FALSE) {
517 g_print ("Analyzing %s\n", uri);
518 info = gst_discoverer_discover_uri (dc, uri, &err);
519 print_info (info, err);
522 gst_discoverer_info_unref (info);
524 gst_discoverer_discover_uri_async (dc, uri);
531 _new_discovered_uri (GstDiscoverer * dc, GstDiscovererInfo * info, GError * err)
533 print_info (info, err);
537 _run_async (PrivStruct * ps)
541 for (i = 1; i < ps->argc; i++)
542 process_file (ps->dc, ps->argv[i]);
548 _discoverer_finished (GstDiscoverer * dc, GMainLoop * ml)
550 g_main_loop_quit (ml);
554 main (int argc, char **argv)
559 GOptionEntry options[] = {
560 {"async", 'a', 0, G_OPTION_ARG_NONE, &async,
561 "Run asynchronously", NULL},
562 {"silent", 's', 0, G_OPTION_ARG_NONE, &silent,
563 "Don't output the information structure", NULL},
564 {"timeout", 't', 0, G_OPTION_ARG_INT, &timeout,
565 "Specify timeout (in seconds, default 10)", "T"},
566 /* {"elem", 'e', 0, G_OPTION_ARG_NONE, &elem_seek, */
567 /* "Seek on elements instead of pads", NULL}, */
568 {"toc", 'c', 0, G_OPTION_ARG_NONE, &show_toc,
569 "Output TOC (chapters and editions)", NULL},
570 {"verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose,
571 "Verbose properties", NULL},
578 ("- discover files synchronously with GstDiscoverer");
579 g_option_context_add_main_entries (ctx, options, NULL);
580 g_option_context_add_group (ctx, gst_init_get_option_group ());
582 if (!g_option_context_parse (ctx, &argc, &argv, &err)) {
583 g_print ("Error initializing: %s\n", err->message);
587 g_option_context_free (ctx);
590 g_print ("usage: %s <uris>\n", argv[0]);
594 dc = gst_discoverer_new (timeout * GST_SECOND, &err);
595 if (G_UNLIKELY (dc == NULL)) {
596 g_print ("Error initializing: %s\n", err->message);
600 if (async == FALSE) {
602 for (i = 1; i < argc; i++)
603 process_file (dc, argv[i]);
605 PrivStruct *ps = g_new0 (PrivStruct, 1);
606 GMainLoop *ml = g_main_loop_new (NULL, FALSE);
612 /* adding uris will be started when the mainloop runs */
613 g_idle_add ((GSourceFunc) _run_async, ps);
615 /* connect signals */
616 g_signal_connect (dc, "discovered", G_CALLBACK (_new_discovered_uri), NULL);
617 g_signal_connect (dc, "finished", G_CALLBACK (_discoverer_finished), ml);
619 gst_discoverer_start (dc);
621 g_main_loop_run (ml);
623 gst_discoverer_stop (dc);
625 g_main_loop_unref (ml);