1 /* GStreamer snapshot example
2 * Copyright (C) <2007> Wim Taymans <wim.taymans@gmail.com>
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.
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.
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., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
25 #define CAPS "video/x-raw-rgb,width=160,pixel-aspect-ratio=1/1,bpp=(int)24,depth=(int)24,endianness=(int)4321,red_mask=(int)0xff0000, green_mask=(int)0x00ff00, blue_mask=(int)0x0000ff"
28 main (int argc, char *argv[])
30 GstElement *pipeline, *sink;
36 gint64 duration, position;
38 GstStateChangeReturn ret;
41 gst_init (&argc, &argv);
44 g_print ("usage: %s <uri>\n Writes snapshot.png in the current directory\n",
49 /* create a new pipeline */
51 g_strdup_printf ("uridecodebin uri=%s ! ffmpegcolorspace ! videoscale ! "
52 " appsink name=sink caps=\"" CAPS "\"", argv[1]);
53 pipeline = gst_parse_launch (descr, &error);
56 g_print ("could not construct pipeline: %s\n", error->message);
62 sink = gst_bin_get_by_name (GST_BIN (pipeline), "sink");
64 /* set to PAUSED to make the first frame arrive in the sink */
65 ret = gst_element_set_state (pipeline, GST_STATE_PAUSED);
67 case GST_STATE_CHANGE_FAILURE:
68 g_print ("failed to play the file\n");
70 case GST_STATE_CHANGE_NO_PREROLL:
71 /* for live sources, we need to set the pipeline to PLAYING before we can
72 * receive a buffer. We don't do that yet */
73 g_print ("live sources not supported yet\n");
78 /* This can block for up to 5 seconds. If your machine is really overloaded,
79 * it might time out before the pipeline prerolled and we generate an error. A
80 * better way is to run a mainloop and catch errors there. */
81 ret = gst_element_get_state (pipeline, NULL, NULL, 5 * GST_SECOND);
82 if (ret == GST_STATE_CHANGE_FAILURE) {
83 g_print ("failed to play the file\n");
87 /* get the duration */
88 format = GST_FORMAT_TIME;
89 gst_element_query_duration (pipeline, &format, &duration);
92 /* we have a duration, seek to 5% */
93 position = duration * 5 / 100;
95 /* no duration, seek to 1 second, this could EOS */
96 position = 1 * GST_SECOND;
98 /* seek to the a position in the file. Most files have a black first frame so
99 * by seeking to somewhere else we have a bigger chance of getting something
100 * more interesting. An optimisation would be to detect black images and then
101 * seek a little more */
102 gst_element_seek_simple (pipeline, GST_FORMAT_TIME,
103 GST_SEEK_FLAG_KEY_UNIT | GST_SEEK_FLAG_FLUSH, position);
105 /* get the preroll buffer from appsink, this block untils appsink really
107 g_signal_emit_by_name (sink, "pull-preroll", &buffer, NULL);
109 /* if we have a buffer now, convert it to a pixbuf. It's possible that we
110 * don't have a buffer because we went EOS right away or had an error. */
115 /* get the snapshot buffer format now. We set the caps on the appsink so
116 * that it can only be an rgb buffer. The only thing we have not specified
117 * on the caps is the height, which is dependant on the pixel-aspect-ratio
118 * of the source material */
119 caps = GST_BUFFER_CAPS (buffer);
121 g_print ("could not get snapshot format\n");
124 s = gst_caps_get_structure (caps, 0);
126 /* we need to get the final caps on the buffer to get the size */
127 res = gst_structure_get_int (s, "width", &width);
128 res |= gst_structure_get_int (s, "height", &height);
130 g_print ("could not get snapshot dimension\n");
134 /* create pixmap from buffer and save, gstreamer video buffers have a stride
135 * that is rounded up to the nearest multiple of 4 */
136 pixbuf = gdk_pixbuf_new_from_data (GST_BUFFER_DATA (buffer),
137 GDK_COLORSPACE_RGB, FALSE, 8, width, height,
138 GST_ROUND_UP_4 (width * 3), NULL, NULL);
140 /* save the pixbuf */
141 gdk_pixbuf_save (pixbuf, "snapshot.png", "png", &error, NULL);
143 g_print ("could not make snapshot\n");
146 /* cleanup and exit */
147 gst_element_set_state (pipeline, GST_STATE_NULL);
148 gst_object_unref (pipeline);