|
1 /* GStreamer |
|
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> |
|
5 * |
|
6 * gst-typefind.c: Use GStreamer to find the type of a file |
|
7 * |
|
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. |
|
12 * |
|
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. |
|
17 * |
|
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. |
|
22 */ |
|
23 |
|
24 |
|
25 #ifdef HAVE_CONFIG_H |
|
26 # include "config.h" |
|
27 #endif |
|
28 |
|
29 #include <string.h> |
|
30 #include <locale.h> |
|
31 |
|
32 #include "tools.h" |
|
33 |
|
34 static void |
|
35 have_type_handler (GstElement * typefind, guint probability, |
|
36 const GstCaps * caps, GstCaps ** p_caps) |
|
37 { |
|
38 if (p_caps) { |
|
39 *p_caps = gst_caps_copy (caps); |
|
40 } |
|
41 } |
|
42 |
|
43 static void |
|
44 typefind_file (const gchar * filename) |
|
45 { |
|
46 GstStateChangeReturn sret; |
|
47 GstElement *pipeline; |
|
48 GstElement *source; |
|
49 GstElement *typefind; |
|
50 GstElement *fakesink; |
|
51 GstState state; |
|
52 GstCaps *caps = NULL; |
|
53 GDir *dir; |
|
54 |
|
55 if ((dir = g_dir_open (filename, 0, NULL))) { |
|
56 const gchar *entry; |
|
57 |
|
58 while ((entry = g_dir_read_name (dir))) { |
|
59 gchar *path; |
|
60 |
|
61 path = g_strconcat (filename, G_DIR_SEPARATOR_S, entry, NULL); |
|
62 typefind_file (path); |
|
63 g_free (path); |
|
64 } |
|
65 |
|
66 g_dir_close (dir); |
|
67 return; |
|
68 } |
|
69 |
|
70 pipeline = gst_pipeline_new ("pipeline"); |
|
71 |
|
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)); |
|
78 |
|
79 gst_bin_add_many (GST_BIN (pipeline), source, typefind, fakesink, NULL); |
|
80 gst_element_link_many (source, typefind, fakesink, NULL); |
|
81 |
|
82 g_signal_connect (G_OBJECT (typefind), "have-type", |
|
83 G_CALLBACK (have_type_handler), &caps); |
|
84 |
|
85 g_object_set (source, "location", filename, NULL); |
|
86 |
|
87 GST_DEBUG ("Starting typefinding for %s", filename); |
|
88 |
|
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); |
|
92 |
|
93 /* wait until state change either completes or fails */ |
|
94 sret = gst_element_get_state (GST_ELEMENT (pipeline), &state, NULL, -1); |
|
95 |
|
96 switch (sret) { |
|
97 case GST_STATE_CHANGE_FAILURE:{ |
|
98 GstMessage *msg; |
|
99 GstBus *bus; |
|
100 GError *err = NULL; |
|
101 |
|
102 bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline)); |
|
103 msg = gst_bus_poll (bus, GST_MESSAGE_ERROR, 0); |
|
104 gst_object_unref (bus); |
|
105 |
|
106 if (msg) { |
|
107 gst_message_parse_error (msg, &err, NULL); |
|
108 g_printerr ("%s - FAILED: %s\n", filename, err->message); |
|
109 g_error_free (err); |
|
110 gst_message_unref (msg); |
|
111 } else { |
|
112 g_printerr ("%s - FAILED: unknown error\n", filename); |
|
113 } |
|
114 break; |
|
115 } |
|
116 case GST_STATE_CHANGE_SUCCESS:{ |
|
117 if (caps) { |
|
118 gchar *caps_str; |
|
119 |
|
120 caps_str = gst_caps_to_string (caps); |
|
121 g_print ("%s - %s\n", filename, caps_str); |
|
122 g_free (caps_str); |
|
123 gst_caps_unref (caps); |
|
124 } else { |
|
125 g_print ("%s - %s\n", filename, "No type found"); |
|
126 } |
|
127 break; |
|
128 } |
|
129 default: |
|
130 g_assert_not_reached (); |
|
131 } |
|
132 |
|
133 gst_element_set_state (pipeline, GST_STATE_NULL); |
|
134 gst_object_unref (pipeline); |
|
135 } |
|
136 |
|
137 int |
|
138 main (int argc, char *argv[]) |
|
139 { |
|
140 static gchar **filenames = NULL; |
|
141 guint num, i; |
|
142 GError *err = NULL; |
|
143 GOptionContext *ctx; |
|
144 GOptionEntry options[] = { |
|
145 GST_TOOLS_GOPTION_VERSION, |
|
146 {G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &filenames, NULL}, |
|
147 {NULL} |
|
148 }; |
|
149 |
|
150 #ifdef ENABLE_NLS |
|
151 bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR); |
|
152 bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8"); |
|
153 textdomain (GETTEXT_PACKAGE); |
|
154 #endif |
|
155 |
|
156 if (!g_thread_supported ()) |
|
157 g_thread_init (NULL); |
|
158 |
|
159 ctx = g_option_context_new ("FILES"); |
|
160 g_option_context_add_main_entries (ctx, options, GETTEXT_PACKAGE); |
|
161 g_option_context_add_group (ctx, gst_init_get_option_group ()); |
|
162 if (!g_option_context_parse (ctx, &argc, &argv, &err)) { |
|
163 g_print ("Error initializing: %s\n", GST_STR_NULL (err->message)); |
|
164 exit (1); |
|
165 } |
|
166 g_option_context_free (ctx); |
|
167 |
|
168 gst_tools_print_version ("gst-typefind"); |
|
169 |
|
170 if (filenames == NULL || *filenames == NULL) { |
|
171 g_print ("Please give a filename to typefind\n\n"); |
|
172 return 1; |
|
173 } |
|
174 |
|
175 num = g_strv_length (filenames); |
|
176 |
|
177 for (i = 0; i < num; ++i) { |
|
178 typefind_file (filenames[i]); |
|
179 } |
|
180 |
|
181 g_strfreev (filenames); |
|
182 |
|
183 return 0; |
|
184 } |