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.
35 have_type_handler (GstElement * typefind, guint probability,
36 const GstCaps * caps, GstCaps ** p_caps)
39 *p_caps = gst_caps_copy (caps);
44 typefind_file (const gchar * filename)
46 GstStateChangeReturn sret;
55 if ((dir = g_dir_open (filename, 0, NULL))) {
58 while ((entry = g_dir_read_name (dir))) {
61 path = g_strconcat (filename, G_DIR_SEPARATOR_S, entry, NULL);
70 pipeline = gst_pipeline_new ("pipeline");
72 source = gst_element_factory_make ("filesrc", "source");
73 g_assert (GST_IS_ELEMENT (source));
74 typefind = gst_element_factory_make ("typefind", "typefind");
75 g_assert (GST_IS_ELEMENT (typefind));
76 fakesink = gst_element_factory_make ("fakesink", "fakesink");
77 g_assert (GST_IS_ELEMENT (typefind));
79 gst_bin_add_many (GST_BIN (pipeline), source, typefind, fakesink, NULL);
80 gst_element_link_many (source, typefind, fakesink, NULL);
82 g_signal_connect (G_OBJECT (typefind), "have-type",
83 G_CALLBACK (have_type_handler), &caps);
85 g_object_set (source, "location", filename, NULL);
87 GST_DEBUG ("Starting typefinding for %s", filename);
89 /* typefind will only commit to PAUSED if it actually finds a type;
90 * otherwise the state change fails */
91 gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_PAUSED);
93 /* wait until state change either completes or fails */
94 sret = gst_element_get_state (GST_ELEMENT (pipeline), &state, NULL, -1);
97 case GST_STATE_CHANGE_FAILURE:{
102 bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
103 msg = gst_bus_poll (bus, GST_MESSAGE_ERROR, 0);
104 gst_object_unref (bus);
107 gst_message_parse_error (msg, &err, NULL);
108 g_printerr ("%s - FAILED: %s\n", filename, err->message);
110 gst_message_unref (msg);
112 g_printerr ("%s - FAILED: unknown error\n", filename);
116 case GST_STATE_CHANGE_SUCCESS:{
120 caps_str = gst_caps_to_string (caps);
121 g_print ("%s - %s\n", filename, caps_str);
123 gst_caps_unref (caps);
125 g_print ("%s - %s\n", filename, "No type found");
130 g_assert_not_reached ();
133 gst_element_set_state (pipeline, GST_STATE_NULL);
134 gst_object_unref (pipeline);
138 main (int argc, char *argv[])
140 gchar **filenames = NULL;
144 GOptionEntry options[] = {
145 GST_TOOLS_GOPTION_VERSION,
146 {G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &filenames, NULL},
151 bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
152 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
153 textdomain (GETTEXT_PACKAGE);
156 #if !GLIB_CHECK_VERSION (2, 31, 0)
157 g_thread_init (NULL);
160 gst_tools_set_prgname ("gst-typefind");
162 ctx = g_option_context_new ("FILES");
163 g_option_context_add_main_entries (ctx, options, GETTEXT_PACKAGE);
164 g_option_context_add_group (ctx, gst_init_get_option_group ());
165 if (!g_option_context_parse (ctx, &argc, &argv, &err)) {
166 g_print ("Error initializing: %s\n", GST_STR_NULL (err->message));
169 g_option_context_free (ctx);
171 gst_tools_print_version ("gst-typefind");
173 if (filenames == NULL || *filenames == NULL) {
174 g_print ("Please give a filename to typefind\n\n");
178 num = g_strv_length (filenames);
180 for (i = 0; i < num; ++i) {
181 typefind_file (filenames[i]);
184 g_strfreev (filenames);