2 * Copyright (C) 2003 Thomas Vander Stichele <thomas@apestaart.org>
3 * 2003 Benjamin Otte <in7y118@public.uni-hamburg.de>
4 * 2005 Andy Wingo <wingo@pobox.com>
6 * gst-typefind.c: Use GStreamer to find the type of a file
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
37 have_type_handler (GstElement * typefind, guint probability,
38 const GstCaps * caps, GstCaps ** p_caps)
41 *p_caps = gst_caps_copy (caps);
46 typefind_file (const gchar * filename)
48 GstStateChangeReturn sret;
57 if ((dir = g_dir_open (filename, 0, NULL))) {
60 while ((entry = g_dir_read_name (dir))) {
63 path = g_strconcat (filename, G_DIR_SEPARATOR_S, entry, NULL);
72 pipeline = gst_pipeline_new ("pipeline");
74 source = gst_element_factory_make ("filesrc", "source");
75 g_assert (GST_IS_ELEMENT (source));
76 typefind = gst_element_factory_make ("typefind", "typefind");
77 g_assert (GST_IS_ELEMENT (typefind));
78 fakesink = gst_element_factory_make ("fakesink", "fakesink");
79 g_assert (GST_IS_ELEMENT (typefind));
81 gst_bin_add_many (GST_BIN (pipeline), source, typefind, fakesink, NULL);
82 gst_element_link_many (source, typefind, fakesink, NULL);
84 g_signal_connect (G_OBJECT (typefind), "have-type",
85 G_CALLBACK (have_type_handler), &caps);
87 g_object_set (source, "location", filename, NULL);
89 GST_DEBUG ("Starting typefinding for %s", filename);
91 /* typefind will only commit to PAUSED if it actually finds a type;
92 * otherwise the state change fails */
93 gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_PAUSED);
95 /* wait until state change either completes or fails */
96 sret = gst_element_get_state (GST_ELEMENT (pipeline), &state, NULL, -1);
99 case GST_STATE_CHANGE_FAILURE:{
104 bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
105 msg = gst_bus_poll (bus, GST_MESSAGE_ERROR, 0);
106 gst_object_unref (bus);
109 gst_message_parse_error (msg, &err, NULL);
110 g_printerr ("%s - FAILED: %s\n", filename, err->message);
112 gst_message_unref (msg);
114 g_printerr ("%s - FAILED: unknown error\n", filename);
118 case GST_STATE_CHANGE_SUCCESS:{
122 caps_str = gst_caps_to_string (caps);
123 g_print ("%s - %s\n", filename, caps_str);
125 gst_caps_unref (caps);
127 g_print ("%s - %s\n", filename, "No type found");
132 g_assert_not_reached ();
135 gst_element_set_state (pipeline, GST_STATE_NULL);
136 gst_object_unref (pipeline);
140 main (int argc, char *argv[])
142 gchar **filenames = NULL;
146 GOptionEntry options[] = {
147 GST_TOOLS_GOPTION_VERSION,
148 {G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &filenames, NULL},
153 bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
154 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
155 textdomain (GETTEXT_PACKAGE);
158 ctx = g_option_context_new ("gst-typefind");
159 g_option_context_add_main_entries (ctx, options, GETTEXT_PACKAGE);
160 g_option_context_add_group (ctx, gst_init_get_option_group ());
161 if (!g_option_context_parse (ctx, &argc, &argv, &err)) {
162 g_print ("Error initializing: %s\n", GST_STR_NULL (err->message));
165 g_option_context_free (ctx);
167 gst_tools_print_version ("gst-typefind");
169 if (filenames == NULL || *filenames == NULL) {
170 g_print ("Please give a filename to typefind\n\n");
174 num = g_strv_length (filenames);
176 for (i = 0; i < num; ++i) {
177 typefind_file (filenames[i]);
180 g_strfreev (filenames);