3 * Copyright (C) 2015 Thibault Saunier <tsaunier@gnome.org>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
24 #include <gst/transcoder/gsttranscoder.h>
26 static const gchar *HELP_SUMMARY =
27 "gst-transcoder-1.0 transcodes a stream defined by its first <input-uri>\n"
28 "argument to the place defined by its second <output-uri> argument\n"
29 "into the format described in its third <encoding-format> argument,\n"
30 "or using the given <output-uri> file extension.\n"
32 "The <encoding-format> argument:\n"
33 "===============================\n"
35 "If the encoding format is not defined, it will be guessed with\n"
36 "the given <output-uri> file extension."
38 "<encoding-format> describe the media format into which the\n"
39 "input stream is going to be transcoded. We have two different\n"
40 "ways of describing the format:\n"
42 "GstEncodingProfile serialization format\n"
43 "---------------------------------------\n"
45 "GStreamer encoding profiles can be descibed with a quite extensive\n"
46 "syntax which is descibed in the GstEncodingProfile documentation.\n"
48 "The simple case looks like:\n"
50 " muxer_source_caps:videoencoder_source_caps:audioencoder_source_caps\n"
52 "Name and category of serialized GstEncodingTarget\n"
53 "-------------------------------------------------\n"
55 "Encoding targets describe well known formats which\n"
56 "those are provided in '.gep' files. You can list\n"
57 "available ones using the `--list-targets` argument.\n";
63 GstEncodingProfile *profile;
64 gchar *src_uri, *dest_uri, *encoding_format, *size;
69 position_updated_cb (GstTranscoder * transcoder, GstClockTime pos)
71 GstClockTime dur = -1;
72 gchar status[64] = { 0, };
74 g_object_get (transcoder, "duration", &dur, NULL);
76 memset (status, ' ', sizeof (status) - 1);
78 if (pos != -1 && dur > 0 && dur != -1) {
79 gchar dstr[32], pstr[32];
81 /* FIXME: pretty print in nicer format */
82 g_snprintf (pstr, 32, "%" GST_TIME_FORMAT, GST_TIME_ARGS (pos));
84 g_snprintf (dstr, 32, "%" GST_TIME_FORMAT, GST_TIME_ARGS (dur));
86 g_print ("%s / %s %s\r", pstr, dstr, status);
91 get_profiles_of_type (GstEncodingProfile * profile, GType profile_type)
93 GList *tmp, *profiles = NULL;
95 if (GST_IS_ENCODING_CONTAINER_PROFILE (profile)) {
97 gst_encoding_container_profile_get_profiles
98 (GST_ENCODING_CONTAINER_PROFILE (profile)); tmp; tmp = tmp->next) {
99 if (G_OBJECT_TYPE (tmp->data) == profile_type)
100 profiles = g_list_prepend (profiles, tmp->data);
102 } else if (GST_IS_ENCODING_VIDEO_PROFILE (profile)) {
103 profiles = g_list_prepend (profiles, profile);
110 set_video_settings (Settings * settings)
112 GList *video_profiles, *tmp;
113 gchar *p, *tmpstr, **vsize;
114 gint width = 0, height = 0;
115 GValue framerate = G_VALUE_INIT;
117 if (!settings->size && !settings->framerate)
120 if (settings->size) {
121 p = tmpstr = g_strdup (settings->size);
124 *p = g_ascii_tolower (*p);
126 vsize = g_strsplit (tmpstr, "x", -1);
129 if (!vsize[1] || vsize[2]) {
131 error ("Video size should be in the form: WxH, got %s", settings->size);
136 width = g_ascii_strtoull (vsize[0], NULL, 0);
137 height = g_ascii_strtoull (vsize[1], NULL, 10);
141 if (settings->framerate) {
142 g_value_init (&framerate, GST_TYPE_FRACTION);
143 if (!gst_value_deserialize (&framerate, settings->framerate)) {
144 error ("Video framerate should be either a fraction or an integer"
145 " not: %s", settings->framerate);
150 video_profiles = get_profiles_of_type (settings->profile,
151 GST_TYPE_ENCODING_VIDEO_PROFILE);
152 for (tmp = video_profiles; tmp; tmp = tmp->next) {
153 GstCaps *rest = gst_encoding_profile_get_restriction (tmp->data);
156 rest = gst_caps_new_empty_simple ("video/x-raw");
158 rest = gst_caps_copy (rest);
160 if (settings->size) {
161 gst_caps_set_simple (rest, "width", G_TYPE_INT, width,
162 "height", G_TYPE_INT, height, NULL);
164 if (settings->framerate)
165 gst_caps_set_value (rest, "framerate", &framerate);
167 gst_encoding_profile_set_restriction (tmp->data, rest);
174 set_audio_settings (Settings * settings)
176 GList *audio_profiles, *tmp;
178 if (settings->rate < 0)
182 get_profiles_of_type (settings->profile, GST_TYPE_ENCODING_AUDIO_PROFILE);
183 for (tmp = audio_profiles; tmp; tmp = tmp->next) {
184 GstCaps *rest = gst_encoding_profile_get_restriction (tmp->data);
187 rest = gst_caps_new_empty_simple ("audio/x-raw");
189 rest = gst_caps_copy (rest);
191 gst_caps_set_simple (rest, "rate", G_TYPE_INT, settings->rate, NULL);
192 gst_encoding_profile_set_restriction (tmp->data, rest);
199 list_encoding_targets (void)
201 GList *tmp, *targets = gst_encoding_list_all_targets (NULL);
203 for (tmp = targets; tmp; tmp = tmp->next) {
204 GstEncodingTarget *target = tmp->data;
205 GList *usable_profiles = get_usable_profiles (target);
207 if (usable_profiles) {
210 g_print ("\n%s (%s): %s\n * Profiles:\n",
211 gst_encoding_target_get_name (target),
212 gst_encoding_target_get_category (target),
213 gst_encoding_target_get_description (target));
215 for (tmpprof = usable_profiles; tmpprof; tmpprof = tmpprof->next)
216 g_print (" - %s: %s",
217 gst_encoding_profile_get_name (tmpprof->data),
218 gst_encoding_profile_get_description (tmpprof->data));
221 g_list_free (usable_profiles);
225 g_list_free_full (targets, (GDestroyNotify) g_object_unref);
229 _error_cb (GstTranscoder * transcoder, GError * err, GstStructure * details)
231 if (g_error_matches (err, GST_CORE_ERROR, GST_CORE_ERROR_PAD)) {
232 GstPadLinkReturn lret;
235 if (details && gst_structure_get (details, "linking-error",
236 GST_TYPE_PAD_LINK_RETURN, &lret,
237 "msg-source-type", G_TYPE_GTYPE, &type, NULL) &&
238 type == g_type_from_name ("GstTranscodeBin")) {
239 error ("\nCould not setup transcoding pipeline,"
240 " make sure that you transcoding format parametters"
241 " are compatible with the input stream.");
247 error ("\nFAILURE: %s", err->message);
251 _warning_cb (GstTranscoder * transcoder, GError * error, GstStructure * details)
253 gboolean cant_encode;
254 GstCaps *caps = NULL;
255 gchar *stream_id = NULL;
257 if (details && gst_structure_get (details, "can-t-encode-stream",
258 G_TYPE_BOOLEAN, &cant_encode, "stream-caps", GST_TYPE_CAPS,
259 &caps, "stream-id", G_TYPE_STRING, &stream_id, NULL)) {
260 gchar *source_uri = gst_transcoder_get_source_uri (transcoder);
262 warn ("WARNING: Input stream %s: WON'T BE ENCODED.\n"
263 "Make sure the encoding settings are valid and that"
264 " any preset you set actually exists.\n"
265 "For more information about that stream, you can inspect"
266 " the source stream with:\n\n"
267 " gst-discoverer-1.0 -v %s\n", stream_id, source_uri);
268 gst_caps_unref (caps);
270 g_free (source_uri);;
274 warn ("Got warning: %s", error->message);
278 main (int argc, char *argv[])
282 GstTranscoder *transcoder;
284 Settings settings = {
287 .encoding_format = NULL,
291 GOptionEntry options[] = {
292 {"cpu-usage", 'c', 0, G_OPTION_ARG_INT, &settings.cpu_usage,
293 "The CPU usage to target in the transcoding process", NULL},
294 {"list-targets", 'l', G_OPTION_ARG_NONE, 0, &settings.list,
295 "List all encoding targets", NULL},
296 {"size", 's', 0, G_OPTION_ARG_STRING, &settings.size,
297 "set frame size (WxH or abbreviation)", NULL},
298 {"audio-rate", 'r', 0, G_OPTION_ARG_INT, &settings.rate,
299 "set audio sampling rate (in Hz)", NULL},
300 {"framerate", 'f', 0, G_OPTION_ARG_STRING, &settings.framerate,
301 "set video framerate as a fraction (24/1 for 24fps)"
302 " or a single number (24 for 24fps))", NULL},
303 {"video-encoder", 'v', 0, G_OPTION_ARG_STRING, &settings.size,
304 "The video encoder to use.", NULL},
308 g_set_prgname ("gst-transcoder");
310 ctx = g_option_context_new ("<source uri> <destination uri> "
311 "[<encoding format>[/<encoding profile name>]]");
312 g_option_context_set_summary (ctx, HELP_SUMMARY);
314 g_option_context_add_main_entries (ctx, options, NULL);
315 g_option_context_add_group (ctx, gst_init_get_option_group ());
317 if (!g_option_context_parse (ctx, &argc, &argv, &err)) {
318 g_print ("Error initializing: %s\n", GST_STR_NULL (err->message));
319 g_clear_error (&err);
320 g_option_context_free (ctx);
323 gst_pb_utils_init ();
326 list_encoding_targets ();
330 if (argc < 3 || argc > 4) {
331 g_print ("%s", g_option_context_get_help (ctx, TRUE, NULL));
332 g_option_context_free (ctx);
336 g_option_context_free (ctx);
338 settings.src_uri = ensure_uri (argv[1]);
339 settings.dest_uri = ensure_uri (argv[2]);
342 settings.encoding_format = get_file_extension (settings.dest_uri);
344 if (!settings.encoding_format)
347 settings.encoding_format = argv[3];
350 settings.profile = create_encoding_profile (settings.encoding_format);
352 if (!settings.profile) {
353 error ("Could not find any encoding format for %s\n",
354 settings.encoding_format);
355 warn ("You can list available targets using %s --list-targets", argv[0]);
360 g_print ("Encoding to:\n\n");
361 describe_encoding_profile (settings.profile);
362 if (!set_video_settings (&settings)) {
367 if (!set_audio_settings (&settings)) {
372 transcoder = gst_transcoder_new_full (settings.src_uri, settings.dest_uri,
373 settings.profile, NULL);
374 gst_transcoder_set_avoid_reencoding (transcoder, TRUE);
376 gst_transcoder_set_cpu_usage (transcoder, settings.cpu_usage);
377 g_signal_connect (transcoder, "position-updated",
378 G_CALLBACK (position_updated_cb), NULL);
379 g_signal_connect (transcoder, "warning", G_CALLBACK (_warning_cb), NULL);
380 g_signal_connect (transcoder, "error", G_CALLBACK (_error_cb), NULL);
382 g_assert (transcoder);
384 ok ("Starting transcoding...");
385 gst_transcoder_run (transcoder, &err);
390 g_free (settings.dest_uri);
391 g_free (settings.src_uri);
396 error ("No <encoding-format> specified and no extension"
397 " available in the output target: %s", settings.dest_uri);