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;
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 audio_info = (GstDiscovererAudioInfo *) info;
87 ctmp = gst_discoverer_audio_info_get_language (audio_info);
88 my_g_string_append_printf (s, depth, "Language: %s\n",
89 ctmp ? ctmp : "<unknown>");
90 my_g_string_append_printf (s, depth, "Channels: %u\n",
91 gst_discoverer_audio_info_get_channels (audio_info));
92 my_g_string_append_printf (s, depth, "Sample rate: %u\n",
93 gst_discoverer_audio_info_get_sample_rate (audio_info));
94 my_g_string_append_printf (s, depth, "Depth: %u\n",
95 gst_discoverer_audio_info_get_depth (audio_info));
97 my_g_string_append_printf (s, depth, "Bitrate: %u\n",
98 gst_discoverer_audio_info_get_bitrate (audio_info));
99 my_g_string_append_printf (s, depth, "Max bitrate: %u\n",
100 gst_discoverer_audio_info_get_max_bitrate (audio_info));
102 my_g_string_append_printf (s, depth, "Tags:\n");
103 tags = gst_discoverer_stream_info_get_tags (info);
105 tmp = gst_structure_to_string ((GstStructure *) tags);
106 my_g_string_append_printf (s, depth, " %s\n", tmp);
109 my_g_string_append_printf (s, depth, " None\n");
112 my_g_string_append_printf (s, depth, "\n");
114 return g_string_free (s, FALSE);
118 gst_stream_video_information_to_string (GstDiscovererStreamInfo * info,
121 GstDiscovererVideoInfo *video_info;
125 const GstStructure *misc;
126 const GstTagList *tags;
129 g_return_val_if_fail (info != NULL, NULL);
131 s = g_string_sized_new (len);
133 my_g_string_append_printf (s, depth, "Codec:\n");
134 caps = gst_discoverer_stream_info_get_caps (info);
135 tmp = gst_caps_to_string (caps);
136 gst_caps_unref (caps);
137 my_g_string_append_printf (s, depth, " %s\n", tmp);
140 my_g_string_append_printf (s, depth, "Additional info:\n");
141 misc = gst_discoverer_stream_info_get_misc (info);
143 tmp = gst_structure_to_string (misc);
144 my_g_string_append_printf (s, depth, " %s\n", tmp);
147 my_g_string_append_printf (s, depth, " None\n");
150 video_info = (GstDiscovererVideoInfo *) info;
151 my_g_string_append_printf (s, depth, "Width: %u\n",
152 gst_discoverer_video_info_get_width (video_info));
153 my_g_string_append_printf (s, depth, "Height: %u\n",
154 gst_discoverer_video_info_get_height (video_info));
155 my_g_string_append_printf (s, depth, "Depth: %u\n",
156 gst_discoverer_video_info_get_depth (video_info));
158 my_g_string_append_printf (s, depth, "Frame rate: %u/%u\n",
159 gst_discoverer_video_info_get_framerate_num (video_info),
160 gst_discoverer_video_info_get_framerate_denom (video_info));
162 my_g_string_append_printf (s, depth, "Pixel aspect ratio: %u/%u\n",
163 gst_discoverer_video_info_get_par_num (video_info),
164 gst_discoverer_video_info_get_par_denom (video_info));
166 my_g_string_append_printf (s, depth, "Interlaced: %s\n",
167 gst_discoverer_video_info_is_interlaced (video_info) ? "true" : "false");
169 my_g_string_append_printf (s, depth, "Bitrate: %u\n",
170 gst_discoverer_video_info_get_bitrate (video_info));
171 my_g_string_append_printf (s, depth, "Max bitrate: %u\n",
172 gst_discoverer_video_info_get_max_bitrate (video_info));
174 my_g_string_append_printf (s, depth, "Tags:\n");
175 tags = gst_discoverer_stream_info_get_tags (info);
177 tmp = gst_structure_to_string ((GstStructure *) tags);
178 my_g_string_append_printf (s, depth, " %s\n", tmp);
181 my_g_string_append_printf (s, depth, " None\n");
184 my_g_string_append_printf (s, depth, "\n");
186 return g_string_free (s, FALSE);
190 gst_stream_subtitle_information_to_string (GstDiscovererStreamInfo * info,
193 GstDiscovererSubtitleInfo *subtitle_info;
198 const GstTagList *tags;
201 g_return_val_if_fail (info != NULL, NULL);
203 s = g_string_sized_new (len);
205 my_g_string_append_printf (s, depth, "Codec:\n");
206 caps = gst_discoverer_stream_info_get_caps (info);
207 tmp = gst_caps_to_string (caps);
208 gst_caps_unref (caps);
209 my_g_string_append_printf (s, depth, " %s\n", tmp);
212 my_g_string_append_printf (s, depth, "Additional info:\n");
213 if (gst_discoverer_stream_info_get_misc (info)) {
214 tmp = gst_structure_to_string (gst_discoverer_stream_info_get_misc (info));
215 my_g_string_append_printf (s, depth, " %s\n", tmp);
218 my_g_string_append_printf (s, depth, " None\n");
221 subtitle_info = (GstDiscovererSubtitleInfo *) info;
222 ctmp = gst_discoverer_subtitle_info_get_language (subtitle_info);
223 my_g_string_append_printf (s, depth, "Language: %s\n",
224 ctmp ? ctmp : "<unknown>");
226 my_g_string_append_printf (s, depth, "Tags:\n");
227 tags = gst_discoverer_stream_info_get_tags (info);
229 tmp = gst_structure_to_string ((GstStructure *) tags);
230 my_g_string_append_printf (s, depth, " %s\n", tmp);
233 my_g_string_append_printf (s, depth, " None\n");
236 my_g_string_append_printf (s, depth, "\n");
238 return g_string_free (s, FALSE);
242 print_stream_info (GstDiscovererStreamInfo * info, void *depth)
247 caps = gst_discoverer_stream_info_get_caps (info);
250 if (gst_caps_is_fixed (caps) && !verbose)
251 desc = gst_pb_utils_get_codec_description (caps);
253 desc = gst_caps_to_string (caps);
254 gst_caps_unref (caps);
257 g_print ("%*s%s: %s\n", 2 * GPOINTER_TO_INT (depth), " ",
258 gst_discoverer_stream_info_get_stream_type_nick (info), desc);
266 if (GST_IS_DISCOVERER_AUDIO_INFO (info))
268 gst_stream_audio_information_to_string (info,
269 GPOINTER_TO_INT (depth) + 1);
270 else if (GST_IS_DISCOVERER_VIDEO_INFO (info))
272 gst_stream_video_information_to_string (info,
273 GPOINTER_TO_INT (depth) + 1);
274 else if (GST_IS_DISCOVERER_SUBTITLE_INFO (info))
276 gst_stream_subtitle_information_to_string (info,
277 GPOINTER_TO_INT (depth) + 1);
279 g_print ("%s", desc);
286 print_topology (GstDiscovererStreamInfo * info, gint depth)
288 GstDiscovererStreamInfo *next;
293 print_stream_info (info, GINT_TO_POINTER (depth));
295 next = gst_discoverer_stream_info_get_next (info);
297 print_topology (next, depth + 1);
298 gst_discoverer_stream_info_unref (next);
299 } else if (GST_IS_DISCOVERER_CONTAINER_INFO (info)) {
300 GList *tmp, *streams;
303 gst_discoverer_container_info_get_streams (GST_DISCOVERER_CONTAINER_INFO
305 for (tmp = streams; tmp; tmp = tmp->next) {
306 GstDiscovererStreamInfo *tmpinf = (GstDiscovererStreamInfo *) tmp->data;
307 print_topology (tmpinf, depth + 1);
309 gst_discoverer_stream_info_list_free (streams);
314 print_tag_each (GQuark field_id, const GValue * value, gpointer user_data)
316 gint tab = GPOINTER_TO_INT (user_data);
319 if (G_VALUE_HOLDS_STRING (value))
320 ser = g_value_dup_string (value);
321 else if (GST_VALUE_HOLDS_BUFFER (value)) {
322 GstSample *smpl = gst_value_get_sample (value);
323 GstBuffer *buf = gst_sample_get_buffer (smpl);
324 GstCaps *caps = gst_sample_get_caps (smpl);
327 caps_str = caps ? gst_caps_to_string (caps) : g_strdup ("unknown");
329 g_strdup_printf ("<GstSample [%" G_GSIZE_FORMAT " bytes, type %s]>",
330 gst_buffer_get_size (buf), caps_str);
333 ser = gst_value_serialize (value);
335 g_print ("%*s%s: %s\n", tab, " ",
336 gst_tag_get_nick (g_quark_to_string (field_id)), ser);
343 print_properties (GstDiscovererInfo * info, gint tab)
345 const GstTagList *tags;
347 g_print ("%*sDuration: %" GST_TIME_FORMAT "\n", tab + 1, " ",
348 GST_TIME_ARGS (gst_discoverer_info_get_duration (info)));
349 g_print ("%*sSeekable: %s\n", tab + 1, " ",
350 (gst_discoverer_info_get_seekable (info) ? "yes" : "no"));
351 if ((tags = gst_discoverer_info_get_tags (info))) {
352 g_print ("%*sTags: \n", tab + 1, " ");
353 gst_structure_foreach ((const GstStructure *) tags, print_tag_each,
354 GINT_TO_POINTER (tab + 5));
359 print_info (GstDiscovererInfo * info, GError * err)
361 GstDiscovererResult result = gst_discoverer_info_get_result (info);
362 GstDiscovererStreamInfo *sinfo;
364 g_print ("Done discovering %s\n", gst_discoverer_info_get_uri (info));
366 case GST_DISCOVERER_OK:
370 case GST_DISCOVERER_URI_INVALID:
372 g_print ("URI is not valid\n");
375 case GST_DISCOVERER_ERROR:
377 g_print ("An error was encountered while discovering the file\n");
378 g_print (" %s\n", err->message);
381 case GST_DISCOVERER_TIMEOUT:
383 g_print ("Analyzing URI timed out\n");
386 case GST_DISCOVERER_BUSY:
388 g_print ("Discoverer was busy\n");
391 case GST_DISCOVERER_MISSING_PLUGINS:
393 g_print ("Missing plugins\n");
396 gst_structure_to_string (gst_discoverer_info_get_misc (info));
397 g_print (" (%s)\n", tmp);
404 if ((sinfo = gst_discoverer_info_get_stream_info (info))) {
405 g_print ("\nTopology:\n");
406 print_topology (sinfo, 1);
407 g_print ("\nProperties:\n");
408 print_properties (info, 1);
409 gst_discoverer_stream_info_unref (sinfo);
416 process_file (GstDiscoverer * dc, const gchar * filename)
421 GstDiscovererInfo *info;
423 if (!gst_uri_is_valid (filename)) {
424 /* Recurse into directories */
425 if ((dir = g_dir_open (filename, 0, NULL))) {
428 while ((entry = g_dir_read_name (dir))) {
430 path = g_strconcat (filename, G_DIR_SEPARATOR_S, entry, NULL);
431 process_file (dc, path);
439 if (!g_path_is_absolute (filename)) {
442 cur_dir = g_get_current_dir ();
443 path = g_build_filename (cur_dir, filename, NULL);
446 path = g_strdup (filename);
449 uri = g_filename_to_uri (path, NULL, &err);
454 g_warning ("Couldn't convert filename to URI: %s\n", err->message);
459 uri = g_strdup (filename);
462 if (async == FALSE) {
463 g_print ("Analyzing %s\n", uri);
464 info = gst_discoverer_discover_uri (dc, uri, &err);
465 print_info (info, err);
468 gst_discoverer_info_unref (info);
470 gst_discoverer_discover_uri_async (dc, uri);
477 _new_discovered_uri (GstDiscoverer * dc, GstDiscovererInfo * info, GError * err)
479 print_info (info, err);
483 _run_async (PrivStruct * ps)
487 for (i = 1; i < ps->argc; i++)
488 process_file (ps->dc, ps->argv[i]);
494 _discoverer_finished (GstDiscoverer * dc, GMainLoop * ml)
496 g_main_loop_quit (ml);
500 main (int argc, char **argv)
505 GOptionEntry options[] = {
506 {"async", 'a', 0, G_OPTION_ARG_NONE, &async,
507 "Run asynchronously", NULL},
508 {"silent", 's', 0, G_OPTION_ARG_NONE, &silent,
509 "Don't output the information structure", NULL},
510 {"timeout", 't', 0, G_OPTION_ARG_INT, &timeout,
511 "Specify timeout (in seconds, default 10)", "T"},
512 /* {"elem", 'e', 0, G_OPTION_ARG_NONE, &elem_seek, */
513 /* "Seek on elements instead of pads", NULL}, */
514 {"verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose,
515 "Verbose properties", NULL},
522 ("- discover files synchronously with GstDiscoverer");
523 g_option_context_add_main_entries (ctx, options, NULL);
524 g_option_context_add_group (ctx, gst_init_get_option_group ());
526 if (!g_option_context_parse (ctx, &argc, &argv, &err)) {
527 g_print ("Error initializing: %s\n", err->message);
531 g_option_context_free (ctx);
534 g_print ("usage: %s <uris>\n", argv[0]);
538 dc = gst_discoverer_new (timeout * GST_SECOND, &err);
539 if (G_UNLIKELY (dc == NULL)) {
540 g_print ("Error initializing: %s\n", err->message);
544 if (async == FALSE) {
546 for (i = 1; i < argc; i++)
547 process_file (dc, argv[i]);
549 PrivStruct *ps = g_new0 (PrivStruct, 1);
550 GMainLoop *ml = g_main_loop_new (NULL, FALSE);
556 /* adding uris will be started when the mainloop runs */
557 g_idle_add ((GSourceFunc) _run_async, ps);
559 /* connect signals */
560 g_signal_connect (dc, "discovered", G_CALLBACK (_new_discovered_uri), NULL);
561 g_signal_connect (dc, "finished", G_CALLBACK (_discoverer_finished), ml);
563 gst_discoverer_start (dc);
565 g_main_loop_run (ml);
567 gst_discoverer_stop (dc);
569 g_main_loop_unref (ml);