/tests/examples/overlays.c

https://github.com/lubosz/gst-editing-services · C · 176 lines · 123 code · 35 blank · 18 comment · 5 complexity · c9dfb26393321735c69e523d4a782922 MD5 · raw file

  1. /* GStreamer Editing Services
  2. * Copyright (C) 2010 Brandon Lewis <brandon@alum.berkeley.edu>
  3. *
  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.
  8. *
  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.
  13. *
  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., 51 Franklin St, Fifth Floor,
  17. * Boston, MA 02110-1301, USA.
  18. */
  19. #include <stdlib.h>
  20. #include <ges/ges.h>
  21. typedef struct
  22. {
  23. int type;
  24. char *name;
  25. } transition_type;
  26. GESClip *make_source (char *path, guint64 start, guint64 duration,
  27. gint priority);
  28. GESClip *make_overlay (char *text, guint64 start, guint64 duration,
  29. gint priority, guint32 color, gdouble xpos, gdouble ypos);
  30. GESPipeline *make_timeline (char *path, float duration, char *text,
  31. guint32 color, gdouble xpos, gdouble ypos);
  32. #define DEFAULT_DURATION 5
  33. #define DEFAULT_POS 0.5
  34. GESClip *
  35. make_source (char *path, guint64 start, guint64 duration, gint priority)
  36. {
  37. gchar *uri = gst_filename_to_uri (path, NULL);
  38. GESClip *ret = GES_CLIP (ges_uri_clip_new (uri));
  39. g_object_set (ret,
  40. "start", (guint64) start,
  41. "duration", (guint64) duration,
  42. "priority", (guint32) priority, "in-point", (guint64) 0, NULL);
  43. g_free (uri);
  44. return ret;
  45. }
  46. GESClip *
  47. make_overlay (char *text, guint64 start, guint64 duration, gint priority,
  48. guint32 color, gdouble xpos, gdouble ypos)
  49. {
  50. GESClip *ret = GES_CLIP (ges_text_overlay_clip_new ());
  51. g_object_set (ret,
  52. "text", (gchar *) text,
  53. "start", (guint64) start,
  54. "duration", (guint64) duration,
  55. "priority", (guint32) priority,
  56. "in-point", (guint64) 0,
  57. "color", (guint32) color,
  58. "valignment", (gint) GES_TEXT_VALIGN_POSITION,
  59. "halignment", (gint) GES_TEXT_HALIGN_POSITION,
  60. "xpos", (gdouble) xpos, "ypos", (gdouble) ypos, NULL);
  61. return ret;
  62. }
  63. GESPipeline *
  64. make_timeline (char *path, float duration, char *text, guint32 color,
  65. gdouble xpos, gdouble ypos)
  66. {
  67. GESTimeline *timeline;
  68. GESTrack *trackv, *tracka;
  69. GESLayer *layer1;
  70. GESClip *srca;
  71. GESClip *overlay;
  72. GESPipeline *pipeline;
  73. guint64 aduration;
  74. pipeline = ges_pipeline_new ();
  75. ges_pipeline_set_mode (pipeline, GES_PIPELINE_MODE_PREVIEW_VIDEO);
  76. timeline = ges_timeline_new ();
  77. ges_pipeline_set_timeline (pipeline, timeline);
  78. trackv = GES_TRACK (ges_video_track_new ());
  79. ges_timeline_add_track (timeline, trackv);
  80. tracka = GES_TRACK (ges_audio_track_new ());
  81. ges_timeline_add_track (timeline, tracka);
  82. layer1 = GES_LAYER (ges_layer_new ());
  83. g_object_set (layer1, "priority", (gint32) 0, NULL);
  84. if (!ges_timeline_add_layer (timeline, layer1))
  85. exit (-1);
  86. aduration = (guint64) (duration * GST_SECOND);
  87. srca = make_source (path, 0, aduration, 1);
  88. overlay = make_overlay (text, 0, aduration, 0, color, xpos, ypos);
  89. ges_layer_add_clip (layer1, srca);
  90. ges_layer_add_clip (layer1, overlay);
  91. return pipeline;
  92. }
  93. int
  94. main (int argc, char **argv)
  95. {
  96. GError *err = NULL;
  97. GOptionContext *ctx;
  98. GESPipeline *pipeline;
  99. GMainLoop *mainloop;
  100. gdouble duration = DEFAULT_DURATION;
  101. char *path = NULL, *text;
  102. guint64 color;
  103. gdouble xpos = DEFAULT_POS, ypos = DEFAULT_POS;
  104. GOptionEntry options[] = {
  105. {"duration", 'd', 0, G_OPTION_ARG_DOUBLE, &duration,
  106. "duration of segment", "seconds"},
  107. {"path", 'p', 0, G_OPTION_ARG_STRING, &path,
  108. "path to file", "path"},
  109. {"text", 't', 0, G_OPTION_ARG_STRING, &text,
  110. "text to render", "text"},
  111. {"color", 'c', 0, G_OPTION_ARG_INT64, &color,
  112. "color of the text", "color"},
  113. {"xpos", 'x', 0, G_OPTION_ARG_DOUBLE, &xpos,
  114. "horizontal position of the text", "color"},
  115. {"ypos", 'y', 0, G_OPTION_ARG_DOUBLE, &ypos,
  116. "vertical position of the text", "color"},
  117. {NULL}
  118. };
  119. ctx = g_option_context_new ("- file segment playback with text overlay");
  120. g_option_context_add_main_entries (ctx, options, NULL);
  121. g_option_context_add_group (ctx, gst_init_get_option_group ());
  122. if (!g_option_context_parse (ctx, &argc, &argv, &err)) {
  123. g_print ("Error initializing %s\n", err->message);
  124. exit (1);
  125. }
  126. if (argc > 1) {
  127. g_print ("%s", g_option_context_get_help (ctx, TRUE, NULL));
  128. exit (0);
  129. }
  130. g_option_context_free (ctx);
  131. ges_init ();
  132. if (path == NULL)
  133. g_error ("Must specify --path=/path/to/media/file option\n");
  134. pipeline = make_timeline (path, duration, text, color, xpos, ypos);
  135. mainloop = g_main_loop_new (NULL, FALSE);
  136. g_timeout_add_seconds ((duration) + 1, (GSourceFunc) g_main_loop_quit,
  137. mainloop);
  138. gst_element_set_state (GST_ELEMENT (pipeline), GST_STATE_PLAYING);
  139. g_main_loop_run (mainloop);
  140. return 0;
  141. }