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., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
27 #include <gst/pbutils/pbutils.h>
29 static gboolean async = FALSE;
30 static gboolean show_toc = FALSE;
31 static gboolean verbose = FALSE;
41 my_g_string_append_printf (GString * str, int depth, const gchar * format, ...)
46 g_string_append (str, " ");
49 va_start (args, format);
50 g_string_append_vprintf (str, format, args);
55 gst_stream_audio_information_to_string (GstDiscovererStreamInfo * info,
58 GstDiscovererAudioInfo *audio_info;
63 const GstTagList *tags;
66 g_return_val_if_fail (info != NULL, NULL);
68 s = g_string_sized_new (len);
70 my_g_string_append_printf (s, depth, "Codec:\n");
71 caps = gst_discoverer_stream_info_get_caps (info);
72 tmp = gst_caps_to_string (caps);
73 gst_caps_unref (caps);
74 my_g_string_append_printf (s, depth, " %s\n", tmp);
77 my_g_string_append_printf (s, depth, "Additional info:\n");
78 if (gst_discoverer_stream_info_get_misc (info)) {
79 tmp = gst_structure_to_string (gst_discoverer_stream_info_get_misc (info));
80 my_g_string_append_printf (s, depth, " %s\n", tmp);
83 my_g_string_append_printf (s, depth, " None\n");
86 my_g_string_append_printf (s, depth, "Stream ID: %s\n",
87 gst_discoverer_stream_info_get_stream_id (info));
89 audio_info = (GstDiscovererAudioInfo *) info;
90 ctmp = gst_discoverer_audio_info_get_language (audio_info);
91 my_g_string_append_printf (s, depth, "Language: %s\n",
92 ctmp ? ctmp : "<unknown>");
93 my_g_string_append_printf (s, depth, "Channels: %u\n",
94 gst_discoverer_audio_info_get_channels (audio_info));
95 my_g_string_append_printf (s, depth, "Sample rate: %u\n",
96 gst_discoverer_audio_info_get_sample_rate (audio_info));
97 my_g_string_append_printf (s, depth, "Depth: %u\n",
98 gst_discoverer_audio_info_get_depth (audio_info));
100 my_g_string_append_printf (s, depth, "Bitrate: %u\n",
101 gst_discoverer_audio_info_get_bitrate (audio_info));
102 my_g_string_append_printf (s, depth, "Max bitrate: %u\n",
103 gst_discoverer_audio_info_get_max_bitrate (audio_info));
105 my_g_string_append_printf (s, depth, "Tags:\n");
106 tags = gst_discoverer_stream_info_get_tags (info);
108 tmp = gst_tag_list_to_string (tags);
109 my_g_string_append_printf (s, depth, " %s\n", tmp);
112 my_g_string_append_printf (s, depth, " None\n");
115 my_g_string_append_printf (s, depth, "\n");
117 return g_string_free (s, FALSE);
121 gst_stream_video_information_to_string (GstDiscovererStreamInfo * info,
124 GstDiscovererVideoInfo *video_info;
128 const GstStructure *misc;
129 const GstTagList *tags;
132 g_return_val_if_fail (info != NULL, NULL);
134 s = g_string_sized_new (len);
136 my_g_string_append_printf (s, depth, "Codec:\n");
137 caps = gst_discoverer_stream_info_get_caps (info);
138 tmp = gst_caps_to_string (caps);
139 gst_caps_unref (caps);
140 my_g_string_append_printf (s, depth, " %s\n", tmp);
143 my_g_string_append_printf (s, depth, "Additional info:\n");
144 misc = gst_discoverer_stream_info_get_misc (info);
146 tmp = gst_structure_to_string (misc);
147 my_g_string_append_printf (s, depth, " %s\n", tmp);
150 my_g_string_append_printf (s, depth, " None\n");
153 my_g_string_append_printf (s, depth, "Stream ID: %s\n",
154 gst_discoverer_stream_info_get_stream_id (info));
156 video_info = (GstDiscovererVideoInfo *) info;
157 my_g_string_append_printf (s, depth, "Width: %u\n",
158 gst_discoverer_video_info_get_width (video_info));
159 my_g_string_append_printf (s, depth, "Height: %u\n",
160 gst_discoverer_video_info_get_height (video_info));
161 my_g_string_append_printf (s, depth, "Depth: %u\n",
162 gst_discoverer_video_info_get_depth (video_info));
164 my_g_string_append_printf (s, depth, "Frame rate: %u/%u\n",
165 gst_discoverer_video_info_get_framerate_num (video_info),
166 gst_discoverer_video_info_get_framerate_denom (video_info));
168 my_g_string_append_printf (s, depth, "Pixel aspect ratio: %u/%u\n",
169 gst_discoverer_video_info_get_par_num (video_info),
170 gst_discoverer_video_info_get_par_denom (video_info));
172 my_g_string_append_printf (s, depth, "Interlaced: %s\n",
173 gst_discoverer_video_info_is_interlaced (video_info) ? "true" : "false");
175 my_g_string_append_printf (s, depth, "Bitrate: %u\n",
176 gst_discoverer_video_info_get_bitrate (video_info));
177 my_g_string_append_printf (s, depth, "Max bitrate: %u\n",
178 gst_discoverer_video_info_get_max_bitrate (video_info));
180 my_g_string_append_printf (s, depth, "Tags:\n");
181 tags = gst_discoverer_stream_info_get_tags (info);
183 tmp = gst_tag_list_to_string (tags);
184 my_g_string_append_printf (s, depth, " %s\n", tmp);
187 my_g_string_append_printf (s, depth, " None\n");
190 my_g_string_append_printf (s, depth, "\n");
192 return g_string_free (s, FALSE);
196 gst_stream_subtitle_information_to_string (GstDiscovererStreamInfo * info,
199 GstDiscovererSubtitleInfo *subtitle_info;
204 const GstTagList *tags;
207 g_return_val_if_fail (info != NULL, NULL);
209 s = g_string_sized_new (len);
211 my_g_string_append_printf (s, depth, "Codec:\n");
212 caps = gst_discoverer_stream_info_get_caps (info);
213 tmp = gst_caps_to_string (caps);
214 gst_caps_unref (caps);
215 my_g_string_append_printf (s, depth, " %s\n", tmp);
218 my_g_string_append_printf (s, depth, "Additional info:\n");
219 if (gst_discoverer_stream_info_get_misc (info)) {
220 tmp = gst_structure_to_string (gst_discoverer_stream_info_get_misc (info));
221 my_g_string_append_printf (s, depth, " %s\n", tmp);
224 my_g_string_append_printf (s, depth, " None\n");
227 my_g_string_append_printf (s, depth, "Stream ID: %s\n",
228 gst_discoverer_stream_info_get_stream_id (info));
230 subtitle_info = (GstDiscovererSubtitleInfo *) info;
231 ctmp = gst_discoverer_subtitle_info_get_language (subtitle_info);
232 my_g_string_append_printf (s, depth, "Language: %s\n",
233 ctmp ? ctmp : "<unknown>");
235 my_g_string_append_printf (s, depth, "Tags:\n");
236 tags = gst_discoverer_stream_info_get_tags (info);
238 tmp = gst_tag_list_to_string (tags);
239 my_g_string_append_printf (s, depth, " %s\n", tmp);
242 my_g_string_append_printf (s, depth, " None\n");
245 my_g_string_append_printf (s, depth, "\n");
247 return g_string_free (s, FALSE);
251 print_stream_info (GstDiscovererStreamInfo * info, void *depth)
256 caps = gst_discoverer_stream_info_get_caps (info);
259 if (gst_caps_is_fixed (caps) && !verbose)
260 desc = gst_pb_utils_get_codec_description (caps);
262 desc = gst_caps_to_string (caps);
263 gst_caps_unref (caps);
266 g_print ("%*s%s: %s\n", 2 * GPOINTER_TO_INT (depth), " ",
267 gst_discoverer_stream_info_get_stream_type_nick (info), desc);
275 if (GST_IS_DISCOVERER_AUDIO_INFO (info))
277 gst_stream_audio_information_to_string (info,
278 GPOINTER_TO_INT (depth) + 1);
279 else if (GST_IS_DISCOVERER_VIDEO_INFO (info))
281 gst_stream_video_information_to_string (info,
282 GPOINTER_TO_INT (depth) + 1);
283 else if (GST_IS_DISCOVERER_SUBTITLE_INFO (info))
285 gst_stream_subtitle_information_to_string (info,
286 GPOINTER_TO_INT (depth) + 1);
288 g_print ("%s", desc);
295 print_topology (GstDiscovererStreamInfo * info, gint depth)
297 GstDiscovererStreamInfo *next;
302 print_stream_info (info, GINT_TO_POINTER (depth));
304 next = gst_discoverer_stream_info_get_next (info);
306 print_topology (next, depth + 1);
307 gst_discoverer_stream_info_unref (next);
308 } else if (GST_IS_DISCOVERER_CONTAINER_INFO (info)) {
309 GList *tmp, *streams;
312 gst_discoverer_container_info_get_streams (GST_DISCOVERER_CONTAINER_INFO
314 for (tmp = streams; tmp; tmp = tmp->next) {
315 GstDiscovererStreamInfo *tmpinf = (GstDiscovererStreamInfo *) tmp->data;
316 print_topology (tmpinf, depth + 1);
318 gst_discoverer_stream_info_list_free (streams);
323 print_tag_foreach (const GstTagList * tags, const gchar * tag,
328 gint depth = GPOINTER_TO_INT (user_data);
330 gst_tag_list_copy_value (&val, tags, tag);
332 if (G_VALUE_HOLDS_STRING (&val))
333 str = g_value_dup_string (&val);
335 str = gst_value_serialize (&val);
337 g_print ("%*s%s: %s\n", 2 * depth, " ", gst_tag_get_nick (tag), str);
340 g_value_unset (&val);
343 #define MAX_INDENT 40
346 print_toc_entry (gpointer data, gpointer user_data)
348 GstTocEntry *entry = (GstTocEntry *) data;
349 gint depth = GPOINTER_TO_INT (user_data);
350 guint indent = MIN (GPOINTER_TO_UINT (user_data), MAX_INDENT);
355 gst_toc_entry_get_start_stop_times (entry, &start, &stop);
356 g_print ("%*s%s: start: %" GST_TIME_FORMAT " stop: %" GST_TIME_FORMAT "\n",
358 gst_toc_entry_type_get_nick (gst_toc_entry_get_entry_type (entry)),
359 GST_TIME_ARGS (start), GST_TIME_ARGS (stop));
363 tags = gst_toc_entry_get_tags (entry);
365 g_print ("%*sTags:\n", 2 * depth, " ");
366 gst_tag_list_foreach (tags, print_tag_foreach, GUINT_TO_POINTER (indent));
369 /* loop over sub-toc entries */
370 subentries = gst_toc_entry_get_sub_entries (entry);
371 g_list_foreach (subentries, print_toc_entry, GUINT_TO_POINTER (indent));
375 print_properties (GstDiscovererInfo * info, gint tab)
377 const GstTagList *tags;
380 g_print ("%*sDuration: %" GST_TIME_FORMAT "\n", tab + 1, " ",
381 GST_TIME_ARGS (gst_discoverer_info_get_duration (info)));
382 g_print ("%*sSeekable: %s\n", tab + 1, " ",
383 (gst_discoverer_info_get_seekable (info) ? "yes" : "no"));
384 if ((tags = gst_discoverer_info_get_tags (info))) {
385 g_print ("%*sTags: \n", tab + 1, " ");
386 gst_tag_list_foreach (tags, print_tag_foreach, GUINT_TO_POINTER (tab + 2));
388 if (show_toc && (toc = gst_discoverer_info_get_toc (info))) {
391 g_print ("%*sTOC: \n", tab + 1, " ");
392 entries = gst_toc_get_entries (toc);
393 g_list_foreach (entries, print_toc_entry, GUINT_TO_POINTER (tab + 5));
398 print_info (GstDiscovererInfo * info, GError * err)
400 GstDiscovererResult result = gst_discoverer_info_get_result (info);
401 GstDiscovererStreamInfo *sinfo;
403 g_print ("Done discovering %s\n", gst_discoverer_info_get_uri (info));
405 case GST_DISCOVERER_OK:
409 case GST_DISCOVERER_URI_INVALID:
411 g_print ("URI is not valid\n");
414 case GST_DISCOVERER_ERROR:
416 g_print ("An error was encountered while discovering the file\n");
417 g_print (" %s\n", err->message);
420 case GST_DISCOVERER_TIMEOUT:
422 g_print ("Analyzing URI timed out\n");
425 case GST_DISCOVERER_BUSY:
427 g_print ("Discoverer was busy\n");
430 case GST_DISCOVERER_MISSING_PLUGINS:
432 g_print ("Missing plugins\n");
435 gst_structure_to_string (gst_discoverer_info_get_misc (info));
436 g_print (" (%s)\n", tmp);
443 if ((sinfo = gst_discoverer_info_get_stream_info (info))) {
444 g_print ("\nTopology:\n");
445 print_topology (sinfo, 1);
446 g_print ("\nProperties:\n");
447 print_properties (info, 1);
448 gst_discoverer_stream_info_unref (sinfo);
455 process_file (GstDiscoverer * dc, const gchar * filename)
460 GstDiscovererInfo *info;
462 if (!gst_uri_is_valid (filename)) {
463 /* Recurse into directories */
464 if ((dir = g_dir_open (filename, 0, NULL))) {
467 while ((entry = g_dir_read_name (dir))) {
469 path = g_strconcat (filename, G_DIR_SEPARATOR_S, entry, NULL);
470 process_file (dc, path);
478 if (!g_path_is_absolute (filename)) {
481 cur_dir = g_get_current_dir ();
482 path = g_build_filename (cur_dir, filename, NULL);
485 path = g_strdup (filename);
488 uri = g_filename_to_uri (path, NULL, &err);
493 g_warning ("Couldn't convert filename to URI: %s\n", err->message);
498 uri = g_strdup (filename);
501 if (async == FALSE) {
502 g_print ("Analyzing %s\n", uri);
503 info = gst_discoverer_discover_uri (dc, uri, &err);
504 print_info (info, err);
507 gst_discoverer_info_unref (info);
509 gst_discoverer_discover_uri_async (dc, uri);
516 _new_discovered_uri (GstDiscoverer * dc, GstDiscovererInfo * info, GError * err)
518 print_info (info, err);
522 _run_async (PrivStruct * ps)
526 for (i = 1; i < ps->argc; i++)
527 process_file (ps->dc, ps->argv[i]);
533 _discoverer_finished (GstDiscoverer * dc, GMainLoop * ml)
535 g_main_loop_quit (ml);
539 main (int argc, char **argv)
544 GOptionEntry options[] = {
545 {"async", 'a', 0, G_OPTION_ARG_NONE, &async,
546 "Run asynchronously", NULL},
547 {"timeout", 't', 0, G_OPTION_ARG_INT, &timeout,
548 "Specify timeout (in seconds, default 10)", "T"},
549 /* {"elem", 'e', 0, G_OPTION_ARG_NONE, &elem_seek, */
550 /* "Seek on elements instead of pads", NULL}, */
551 {"toc", 'c', 0, G_OPTION_ARG_NONE, &show_toc,
552 "Output TOC (chapters and editions)", NULL},
553 {"verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose,
554 "Verbose properties", NULL},
561 ("- discover files synchronously with GstDiscoverer");
562 g_option_context_add_main_entries (ctx, options, NULL);
563 g_option_context_add_group (ctx, gst_init_get_option_group ());
565 if (!g_option_context_parse (ctx, &argc, &argv, &err)) {
566 g_print ("Error initializing: %s\n", err->message);
570 g_option_context_free (ctx);
573 g_print ("usage: %s <uris>\n", argv[0]);
577 dc = gst_discoverer_new (timeout * GST_SECOND, &err);
578 if (G_UNLIKELY (dc == NULL)) {
579 g_print ("Error initializing: %s\n", err->message);
583 if (async == FALSE) {
585 for (i = 1; i < argc; i++)
586 process_file (dc, argv[i]);
588 PrivStruct *ps = g_new0 (PrivStruct, 1);
589 GMainLoop *ml = g_main_loop_new (NULL, FALSE);
595 /* adding uris will be started when the mainloop runs */
596 g_idle_add ((GSourceFunc) _run_async, ps);
598 /* connect signals */
599 g_signal_connect (dc, "discovered", G_CALLBACK (_new_discovered_uri), NULL);
600 g_signal_connect (dc, "finished", G_CALLBACK (_discoverer_finished), ml);
602 gst_discoverer_start (dc);
604 g_main_loop_run (ml);
606 gst_discoverer_stop (dc);
608 g_main_loop_unref (ml);