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;
51 const GstTagList *tags;
54 g_return_val_if_fail (info != NULL, NULL);
56 s = g_string_sized_new (len);
58 my_g_string_append_printf (s, "Codec:\n");
59 caps = gst_discoverer_stream_info_get_caps (info);
60 tmp = gst_caps_to_string (caps);
61 gst_caps_unref (caps);
62 my_g_string_append_printf (s, " %s\n", tmp);
65 my_g_string_append_printf (s, "Additional info:\n");
66 if (gst_discoverer_stream_info_get_misc (info)) {
67 tmp = gst_structure_to_string (gst_discoverer_stream_info_get_misc (info));
68 my_g_string_append_printf (s, " %s\n", tmp);
71 my_g_string_append_printf (s, " None\n");
74 audio_info = (GstDiscovererAudioInfo *) info;
75 my_g_string_append_printf (s, "Channels: %u\n",
76 gst_discoverer_audio_info_get_channels (audio_info));
77 my_g_string_append_printf (s, "Sample rate: %u\n",
78 gst_discoverer_audio_info_get_sample_rate (audio_info));
79 my_g_string_append_printf (s, "Depth: %u\n",
80 gst_discoverer_audio_info_get_depth (audio_info));
82 my_g_string_append_printf (s, "Bitrate: %u\n",
83 gst_discoverer_audio_info_get_bitrate (audio_info));
84 my_g_string_append_printf (s, "Max bitrate: %u\n",
85 gst_discoverer_audio_info_get_max_bitrate (audio_info));
87 my_g_string_append_printf (s, "Tags:\n");
88 tags = gst_discoverer_stream_info_get_tags (info);
90 tmp = gst_structure_to_string ((GstStructure *) tags);
91 my_g_string_append_printf (s, " %s\n", tmp);
94 my_g_string_append_printf (s, " None\n");
97 my_g_string_append_printf (s, "\n");
99 return g_string_free (s, FALSE);
103 gst_stream_video_information_to_string (GstDiscovererStreamInfo * info,
106 GstDiscovererVideoInfo *video_info;
110 const GstStructure *misc;
111 const GstTagList *tags;
114 g_return_val_if_fail (info != NULL, NULL);
116 s = g_string_sized_new (len);
118 my_g_string_append_printf (s, "Codec:\n");
119 caps = gst_discoverer_stream_info_get_caps (info);
120 tmp = gst_caps_to_string (caps);
121 gst_caps_unref (caps);
122 my_g_string_append_printf (s, " %s\n", tmp);
125 my_g_string_append_printf (s, "Additional info:\n");
126 misc = gst_discoverer_stream_info_get_misc (info);
128 tmp = gst_structure_to_string (misc);
129 my_g_string_append_printf (s, " %s\n", tmp);
132 my_g_string_append_printf (s, " None\n");
135 video_info = (GstDiscovererVideoInfo *) info;
136 my_g_string_append_printf (s, "Width: %u\n",
137 gst_discoverer_video_info_get_width (video_info));
138 my_g_string_append_printf (s, "Height: %u\n",
139 gst_discoverer_video_info_get_height (video_info));
140 my_g_string_append_printf (s, "Depth: %u\n",
141 gst_discoverer_video_info_get_depth (video_info));
143 my_g_string_append_printf (s, "Frame rate: %u/%u\n",
144 gst_discoverer_video_info_get_framerate_num (video_info),
145 gst_discoverer_video_info_get_framerate_denom (video_info));
147 my_g_string_append_printf (s, "Pixel aspect ratio: %u/%u\n",
148 gst_discoverer_video_info_get_par_num (video_info),
149 gst_discoverer_video_info_get_par_denom (video_info));
151 my_g_string_append_printf (s, "Interlaced: %s\n",
152 gst_discoverer_video_info_is_interlaced (video_info) ? "true" : "false");
154 my_g_string_append_printf (s, "Bitrate: %u\n",
155 gst_discoverer_video_info_get_bitrate (video_info));
156 my_g_string_append_printf (s, "Max bitrate: %u\n",
157 gst_discoverer_video_info_get_max_bitrate (video_info));
159 my_g_string_append_printf (s, "Tags:\n");
160 tags = gst_discoverer_stream_info_get_tags (info);
162 tmp = gst_structure_to_string ((GstStructure *) tags);
163 my_g_string_append_printf (s, " %s\n", tmp);
166 my_g_string_append_printf (s, " None\n");
169 my_g_string_append_printf (s, "\n");
171 return g_string_free (s, FALSE);
175 print_stream_info (GstDiscovererStreamInfo * info, void *depth)
180 caps = gst_discoverer_stream_info_get_caps (info);
183 if (gst_caps_is_fixed (caps) && !verbose)
184 desc = gst_pb_utils_get_codec_description (caps);
186 desc = gst_caps_to_string (caps);
187 gst_caps_unref (caps);
190 g_print ("%*s%s: %s\n", 2 * GPOINTER_TO_INT (depth), " ",
191 gst_discoverer_stream_info_get_stream_type_nick (info), desc);
199 if (GST_IS_DISCOVERER_AUDIO_INFO (info))
201 gst_stream_audio_information_to_string (info,
202 GPOINTER_TO_INT (depth) + 1);
203 else if (GST_IS_DISCOVERER_VIDEO_INFO (info))
205 gst_stream_video_information_to_string (info,
206 GPOINTER_TO_INT (depth) + 1);
208 g_print ("%s", desc);
215 print_topology (GstDiscovererStreamInfo * info, gint depth)
217 GstDiscovererStreamInfo *next;
222 print_stream_info (info, GINT_TO_POINTER (depth));
224 next = gst_discoverer_stream_info_get_next (info);
226 print_topology (next, depth + 1);
227 gst_discoverer_stream_info_unref (next);
228 } else if (GST_IS_DISCOVERER_CONTAINER_INFO (info)) {
229 GList *tmp, *streams;
232 gst_discoverer_container_info_get_streams (GST_DISCOVERER_CONTAINER_INFO
234 for (tmp = streams; tmp; tmp = tmp->next) {
235 GstDiscovererStreamInfo *tmpinf = (GstDiscovererStreamInfo *) tmp->data;
236 print_topology (tmpinf, depth + 1);
238 gst_discoverer_stream_info_list_free (streams);
243 print_tag_each (GQuark field_id, const GValue * value, gpointer user_data)
245 gint tab = GPOINTER_TO_INT (user_data);
248 if (G_VALUE_HOLDS_STRING (value))
249 ser = g_value_dup_string (value);
250 else if (GST_VALUE_HOLDS_BUFFER (value)) {
251 GstBuffer *buf = gst_value_get_buffer (value);
253 g_strdup_printf ("<GstBuffer [%" G_GSIZE_FORMAT " bytes]>",
254 gst_buffer_get_size (buf));
256 ser = gst_value_serialize (value);
258 g_print ("%*s%s: %s\n", tab, " ",
259 gst_tag_get_nick (g_quark_to_string (field_id)), ser);
266 print_properties (GstDiscovererInfo * info, gint tab)
268 const GstTagList *tags;
270 g_print ("%*sDuration: %" GST_TIME_FORMAT "\n", tab + 1, " ",
271 GST_TIME_ARGS (gst_discoverer_info_get_duration (info)));
272 g_print ("%*sSeekable: %s\n", tab + 1, " ",
273 (gst_discoverer_info_get_seekable (info) ? "yes" : "no"));
274 if ((tags = gst_discoverer_info_get_tags (info))) {
275 g_print ("%*sTags: \n", tab + 1, " ");
276 gst_structure_foreach ((const GstStructure *) tags, print_tag_each,
277 GINT_TO_POINTER (tab + 5));
282 print_info (GstDiscovererInfo * info, GError * err)
284 GstDiscovererResult result = gst_discoverer_info_get_result (info);
285 GstDiscovererStreamInfo *sinfo;
287 g_print ("Done discovering %s\n", gst_discoverer_info_get_uri (info));
289 case GST_DISCOVERER_OK:
293 case GST_DISCOVERER_URI_INVALID:
295 g_print ("URI is not valid\n");
298 case GST_DISCOVERER_ERROR:
300 g_print ("An error was encountered while discovering the file\n");
301 g_print (" %s\n", err->message);
304 case GST_DISCOVERER_TIMEOUT:
306 g_print ("Analyzing URI timed out\n");
309 case GST_DISCOVERER_BUSY:
311 g_print ("Discoverer was busy\n");
314 case GST_DISCOVERER_MISSING_PLUGINS:
316 g_print ("Missing plugins\n");
319 gst_structure_to_string (gst_discoverer_info_get_misc (info));
320 g_print (" (%s)\n", tmp);
327 if ((sinfo = gst_discoverer_info_get_stream_info (info))) {
328 g_print ("\nTopology:\n");
329 print_topology (sinfo, 1);
330 g_print ("\nProperties:\n");
331 print_properties (info, 1);
332 gst_discoverer_stream_info_unref (sinfo);
339 process_file (GstDiscoverer * dc, const gchar * filename)
344 GstDiscovererInfo *info;
345 GstStructure *st = NULL;
347 if (!gst_uri_is_valid (filename)) {
348 /* Recurse into directories */
349 if ((dir = g_dir_open (filename, 0, NULL))) {
352 while ((entry = g_dir_read_name (dir))) {
354 path = g_strconcat (filename, G_DIR_SEPARATOR_S, entry, NULL);
355 process_file (dc, path);
363 if (!g_path_is_absolute (filename)) {
366 cur_dir = g_get_current_dir ();
367 path = g_build_filename (cur_dir, filename, NULL);
370 path = g_strdup (filename);
373 uri = g_filename_to_uri (path, NULL, &err);
378 g_warning ("Couldn't convert filename to URI: %s\n", err->message);
383 uri = g_strdup (filename);
386 if (async == FALSE) {
387 g_print ("Analyzing %s\n", uri);
388 info = gst_discoverer_discover_uri (dc, uri, &err);
389 print_info (info, err);
390 gst_discoverer_info_unref (info);
392 gst_structure_free (st);
394 gst_discoverer_discover_uri_async (dc, uri);
401 _new_discovered_uri (GstDiscoverer * dc, GstDiscovererInfo * info, GError * err)
403 print_info (info, err);
407 _run_async (PrivStruct * ps)
411 for (i = 1; i < ps->argc; i++)
412 process_file (ps->dc, ps->argv[i]);
418 _discoverer_finished (GstDiscoverer * dc, GMainLoop * ml)
420 g_main_loop_quit (ml);
424 main (int argc, char **argv)
429 GOptionEntry options[] = {
430 {"async", 'a', 0, G_OPTION_ARG_NONE, &async,
431 "Run asynchronously", NULL},
432 {"silent", 's', 0, G_OPTION_ARG_NONE, &silent,
433 "Don't output the information structure", NULL},
434 {"timeout", 't', 0, G_OPTION_ARG_INT, &timeout,
435 "Specify timeout (in seconds, default 10)", "T"},
436 /* {"elem", 'e', 0, G_OPTION_ARG_NONE, &elem_seek, */
437 /* "Seek on elements instead of pads", NULL}, */
438 {"verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose,
439 "Verbose properties", NULL},
444 if (!g_thread_supported ())
445 g_thread_init (NULL);
449 ("- discover files synchronously with GstDiscoverer");
450 g_option_context_add_main_entries (ctx, options, NULL);
451 g_option_context_add_group (ctx, gst_init_get_option_group ());
453 if (!g_option_context_parse (ctx, &argc, &argv, &err)) {
454 g_print ("Error initializing: %s\n", err->message);
458 g_option_context_free (ctx);
461 g_print ("usage: %s <uris>\n", argv[0]);
465 dc = gst_discoverer_new (timeout * GST_SECOND, &err);
466 if (G_UNLIKELY (dc == NULL)) {
467 g_print ("Error initializing: %s\n", err->message);
471 if (async == FALSE) {
473 for (i = 1; i < argc; i++)
474 process_file (dc, argv[i]);
476 PrivStruct *ps = g_new0 (PrivStruct, 1);
477 GMainLoop *ml = g_main_loop_new (NULL, FALSE);
483 /* adding uris will be started when the mainloop runs */
484 g_idle_add ((GSourceFunc) _run_async, ps);
486 /* connect signals */
487 g_signal_connect (dc, "discovered", G_CALLBACK (_new_discovered_uri), NULL);
488 g_signal_connect (dc, "finished", G_CALLBACK (_discoverer_finished), ml);
490 gst_discoverer_start (dc);
492 g_main_loop_run (ml);
494 gst_discoverer_stop (dc);
496 g_main_loop_unref (ml);