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 verbose = FALSE;
40 #define my_g_string_append_printf(str, format, ...) \
41 g_string_append_printf (str, "%*s" format, 2*depth, " ", ##__VA_ARGS__)
44 gst_stream_audio_information_to_string (GstDiscovererStreamInfo * info,
47 GstDiscovererAudioInfo *audio_info;
52 const GstTagList *tags;
55 g_return_val_if_fail (info != NULL, NULL);
57 s = g_string_sized_new (len);
59 my_g_string_append_printf (s, "Codec:\n");
60 caps = gst_discoverer_stream_info_get_caps (info);
61 tmp = gst_caps_to_string (caps);
62 gst_caps_unref (caps);
63 my_g_string_append_printf (s, " %s\n", tmp);
66 my_g_string_append_printf (s, "Additional info:\n");
67 if (gst_discoverer_stream_info_get_misc (info)) {
68 tmp = gst_structure_to_string (gst_discoverer_stream_info_get_misc (info));
69 my_g_string_append_printf (s, " %s\n", tmp);
72 my_g_string_append_printf (s, " None\n");
75 audio_info = (GstDiscovererAudioInfo *) info;
76 ctmp = gst_discoverer_audio_info_get_language (audio_info);
77 my_g_string_append_printf (s, "Language: %s\n", ctmp ? ctmp : "<unknown>");
78 my_g_string_append_printf (s, "Channels: %u\n",
79 gst_discoverer_audio_info_get_channels (audio_info));
80 my_g_string_append_printf (s, "Sample rate: %u\n",
81 gst_discoverer_audio_info_get_sample_rate (audio_info));
82 my_g_string_append_printf (s, "Depth: %u\n",
83 gst_discoverer_audio_info_get_depth (audio_info));
85 my_g_string_append_printf (s, "Bitrate: %u\n",
86 gst_discoverer_audio_info_get_bitrate (audio_info));
87 my_g_string_append_printf (s, "Max bitrate: %u\n",
88 gst_discoverer_audio_info_get_max_bitrate (audio_info));
90 my_g_string_append_printf (s, "Tags:\n");
91 tags = gst_discoverer_stream_info_get_tags (info);
93 tmp = gst_structure_to_string ((GstStructure *) tags);
94 my_g_string_append_printf (s, " %s\n", tmp);
97 my_g_string_append_printf (s, " None\n");
100 my_g_string_append_printf (s, "\n");
102 return g_string_free (s, FALSE);
106 gst_stream_video_information_to_string (GstDiscovererStreamInfo * info,
109 GstDiscovererVideoInfo *video_info;
113 const GstStructure *misc;
114 const GstTagList *tags;
117 g_return_val_if_fail (info != NULL, NULL);
119 s = g_string_sized_new (len);
121 my_g_string_append_printf (s, "Codec:\n");
122 caps = gst_discoverer_stream_info_get_caps (info);
123 tmp = gst_caps_to_string (caps);
124 gst_caps_unref (caps);
125 my_g_string_append_printf (s, " %s\n", tmp);
128 my_g_string_append_printf (s, "Additional info:\n");
129 misc = gst_discoverer_stream_info_get_misc (info);
131 tmp = gst_structure_to_string (misc);
132 my_g_string_append_printf (s, " %s\n", tmp);
135 my_g_string_append_printf (s, " None\n");
138 video_info = (GstDiscovererVideoInfo *) info;
139 my_g_string_append_printf (s, "Width: %u\n",
140 gst_discoverer_video_info_get_width (video_info));
141 my_g_string_append_printf (s, "Height: %u\n",
142 gst_discoverer_video_info_get_height (video_info));
143 my_g_string_append_printf (s, "Depth: %u\n",
144 gst_discoverer_video_info_get_depth (video_info));
146 my_g_string_append_printf (s, "Frame rate: %u/%u\n",
147 gst_discoverer_video_info_get_framerate_num (video_info),
148 gst_discoverer_video_info_get_framerate_denom (video_info));
150 my_g_string_append_printf (s, "Pixel aspect ratio: %u/%u\n",
151 gst_discoverer_video_info_get_par_num (video_info),
152 gst_discoverer_video_info_get_par_denom (video_info));
154 my_g_string_append_printf (s, "Interlaced: %s\n",
155 gst_discoverer_video_info_is_interlaced (video_info) ? "true" : "false");
157 my_g_string_append_printf (s, "Bitrate: %u\n",
158 gst_discoverer_video_info_get_bitrate (video_info));
159 my_g_string_append_printf (s, "Max bitrate: %u\n",
160 gst_discoverer_video_info_get_max_bitrate (video_info));
162 my_g_string_append_printf (s, "Tags:\n");
163 tags = gst_discoverer_stream_info_get_tags (info);
165 tmp = gst_structure_to_string ((GstStructure *) tags);
166 my_g_string_append_printf (s, " %s\n", tmp);
169 my_g_string_append_printf (s, " None\n");
172 my_g_string_append_printf (s, "\n");
174 return g_string_free (s, FALSE);
178 gst_stream_subtitle_information_to_string (GstDiscovererStreamInfo * info,
181 GstDiscovererSubtitleInfo *subtitle_info;
186 const GstTagList *tags;
189 g_return_val_if_fail (info != NULL, NULL);
191 s = g_string_sized_new (len);
193 my_g_string_append_printf (s, "Codec:\n");
194 caps = gst_discoverer_stream_info_get_caps (info);
195 tmp = gst_caps_to_string (caps);
196 gst_caps_unref (caps);
197 my_g_string_append_printf (s, " %s\n", tmp);
200 my_g_string_append_printf (s, "Additional info:\n");
201 if (gst_discoverer_stream_info_get_misc (info)) {
202 tmp = gst_structure_to_string (gst_discoverer_stream_info_get_misc (info));
203 my_g_string_append_printf (s, " %s\n", tmp);
206 my_g_string_append_printf (s, " None\n");
209 subtitle_info = (GstDiscovererSubtitleInfo *) info;
210 ctmp = gst_discoverer_subtitle_info_get_language (subtitle_info);
211 my_g_string_append_printf (s, "Language: %s\n", ctmp ? ctmp : "<unknown>");
213 my_g_string_append_printf (s, "Tags:\n");
214 tags = gst_discoverer_stream_info_get_tags (info);
216 tmp = gst_structure_to_string ((GstStructure *) tags);
217 my_g_string_append_printf (s, " %s\n", tmp);
220 my_g_string_append_printf (s, " None\n");
223 my_g_string_append_printf (s, "\n");
225 return g_string_free (s, FALSE);
229 print_stream_info (GstDiscovererStreamInfo * info, void *depth)
234 caps = gst_discoverer_stream_info_get_caps (info);
237 if (gst_caps_is_fixed (caps) && !verbose)
238 desc = gst_pb_utils_get_codec_description (caps);
240 desc = gst_caps_to_string (caps);
241 gst_caps_unref (caps);
244 g_print ("%*s%s: %s\n", 2 * GPOINTER_TO_INT (depth), " ",
245 gst_discoverer_stream_info_get_stream_type_nick (info), desc);
253 if (GST_IS_DISCOVERER_AUDIO_INFO (info))
255 gst_stream_audio_information_to_string (info,
256 GPOINTER_TO_INT (depth) + 1);
257 else if (GST_IS_DISCOVERER_VIDEO_INFO (info))
259 gst_stream_video_information_to_string (info,
260 GPOINTER_TO_INT (depth) + 1);
261 else if (GST_IS_DISCOVERER_SUBTITLE_INFO (info))
263 gst_stream_subtitle_information_to_string (info,
264 GPOINTER_TO_INT (depth) + 1);
266 g_print ("%s", desc);
273 print_topology (GstDiscovererStreamInfo * info, gint depth)
275 GstDiscovererStreamInfo *next;
280 print_stream_info (info, GINT_TO_POINTER (depth));
282 next = gst_discoverer_stream_info_get_next (info);
284 print_topology (next, depth + 1);
285 gst_discoverer_stream_info_unref (next);
286 } else if (GST_IS_DISCOVERER_CONTAINER_INFO (info)) {
287 GList *tmp, *streams;
290 gst_discoverer_container_info_get_streams (GST_DISCOVERER_CONTAINER_INFO
292 for (tmp = streams; tmp; tmp = tmp->next) {
293 GstDiscovererStreamInfo *tmpinf = (GstDiscovererStreamInfo *) tmp->data;
294 print_topology (tmpinf, depth + 1);
296 gst_discoverer_stream_info_list_free (streams);
301 print_tag_each (GQuark field_id, const GValue * value, gpointer user_data)
303 gint tab = GPOINTER_TO_INT (user_data);
306 if (G_VALUE_HOLDS_STRING (value))
307 ser = g_value_dup_string (value);
308 else if (GST_VALUE_HOLDS_BUFFER (value)) {
309 GstBuffer *buf = gst_value_get_buffer (value);
311 g_strdup_printf ("<GstBuffer [%" G_GSIZE_FORMAT " bytes]>",
312 gst_buffer_get_size (buf));
314 ser = gst_value_serialize (value);
316 g_print ("%*s%s: %s\n", tab, " ",
317 gst_tag_get_nick (g_quark_to_string (field_id)), ser);
324 print_properties (GstDiscovererInfo * info, gint tab)
326 const GstTagList *tags;
328 g_print ("%*sDuration: %" GST_TIME_FORMAT "\n", tab + 1, " ",
329 GST_TIME_ARGS (gst_discoverer_info_get_duration (info)));
330 g_print ("%*sSeekable: %s\n", tab + 1, " ",
331 (gst_discoverer_info_get_seekable (info) ? "yes" : "no"));
332 if ((tags = gst_discoverer_info_get_tags (info))) {
333 g_print ("%*sTags: \n", tab + 1, " ");
334 gst_structure_foreach ((const GstStructure *) tags, print_tag_each,
335 GINT_TO_POINTER (tab + 5));
340 print_info (GstDiscovererInfo * info, GError * err)
342 GstDiscovererResult result = gst_discoverer_info_get_result (info);
343 GstDiscovererStreamInfo *sinfo;
345 g_print ("Done discovering %s\n", gst_discoverer_info_get_uri (info));
347 case GST_DISCOVERER_OK:
351 case GST_DISCOVERER_URI_INVALID:
353 g_print ("URI is not valid\n");
356 case GST_DISCOVERER_ERROR:
358 g_print ("An error was encountered while discovering the file\n");
359 g_print (" %s\n", err->message);
362 case GST_DISCOVERER_TIMEOUT:
364 g_print ("Analyzing URI timed out\n");
367 case GST_DISCOVERER_BUSY:
369 g_print ("Discoverer was busy\n");
372 case GST_DISCOVERER_MISSING_PLUGINS:
374 g_print ("Missing plugins\n");
377 gst_structure_to_string (gst_discoverer_info_get_misc (info));
378 g_print (" (%s)\n", tmp);
385 if ((sinfo = gst_discoverer_info_get_stream_info (info))) {
386 g_print ("\nTopology:\n");
387 print_topology (sinfo, 1);
388 g_print ("\nProperties:\n");
389 print_properties (info, 1);
390 gst_discoverer_stream_info_unref (sinfo);
397 process_file (GstDiscoverer * dc, const gchar * filename)
402 GstDiscovererInfo *info;
403 GstStructure *st = NULL;
405 if (!gst_uri_is_valid (filename)) {
406 /* Recurse into directories */
407 if ((dir = g_dir_open (filename, 0, NULL))) {
410 while ((entry = g_dir_read_name (dir))) {
412 path = g_strconcat (filename, G_DIR_SEPARATOR_S, entry, NULL);
413 process_file (dc, path);
421 if (!g_path_is_absolute (filename)) {
424 cur_dir = g_get_current_dir ();
425 path = g_build_filename (cur_dir, filename, NULL);
428 path = g_strdup (filename);
431 uri = g_filename_to_uri (path, NULL, &err);
436 g_warning ("Couldn't convert filename to URI: %s\n", err->message);
441 uri = g_strdup (filename);
444 if (async == FALSE) {
445 g_print ("Analyzing %s\n", uri);
446 info = gst_discoverer_discover_uri (dc, uri, &err);
447 print_info (info, err);
448 gst_discoverer_info_unref (info);
450 gst_structure_free (st);
452 gst_discoverer_discover_uri_async (dc, uri);
459 _new_discovered_uri (GstDiscoverer * dc, GstDiscovererInfo * info, GError * err)
461 print_info (info, err);
465 _run_async (PrivStruct * ps)
469 for (i = 1; i < ps->argc; i++)
470 process_file (ps->dc, ps->argv[i]);
476 _discoverer_finished (GstDiscoverer * dc, GMainLoop * ml)
478 g_main_loop_quit (ml);
482 main (int argc, char **argv)
487 GOptionEntry options[] = {
488 {"async", 'a', 0, G_OPTION_ARG_NONE, &async,
489 "Run asynchronously", NULL},
490 {"silent", 's', 0, G_OPTION_ARG_NONE, &silent,
491 "Don't output the information structure", NULL},
492 {"timeout", 't', 0, G_OPTION_ARG_INT, &timeout,
493 "Specify timeout (in seconds, default 10)", "T"},
494 /* {"elem", 'e', 0, G_OPTION_ARG_NONE, &elem_seek, */
495 /* "Seek on elements instead of pads", NULL}, */
496 {"verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose,
497 "Verbose properties", NULL},
502 if (!g_thread_supported ())
503 g_thread_init (NULL);
507 ("- discover files synchronously with GstDiscoverer");
508 g_option_context_add_main_entries (ctx, options, NULL);
509 g_option_context_add_group (ctx, gst_init_get_option_group ());
511 if (!g_option_context_parse (ctx, &argc, &argv, &err)) {
512 g_print ("Error initializing: %s\n", err->message);
516 g_option_context_free (ctx);
519 g_print ("usage: %s <uris>\n", argv[0]);
523 dc = gst_discoverer_new (timeout * GST_SECOND, &err);
524 if (G_UNLIKELY (dc == NULL)) {
525 g_print ("Error initializing: %s\n", err->message);
529 if (async == FALSE) {
531 for (i = 1; i < argc; i++)
532 process_file (dc, argv[i]);
534 PrivStruct *ps = g_new0 (PrivStruct, 1);
535 GMainLoop *ml = g_main_loop_new (NULL, FALSE);
541 /* adding uris will be started when the mainloop runs */
542 g_idle_add ((GSourceFunc) _run_async, ps);
544 /* connect signals */
545 g_signal_connect (dc, "discovered", G_CALLBACK (_new_discovered_uri), NULL);
546 g_signal_connect (dc, "finished", G_CALLBACK (_discoverer_finished), ml);
548 gst_discoverer_start (dc);
550 g_main_loop_run (ml);
552 gst_discoverer_stop (dc);
554 g_main_loop_unref (ml);