Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "opencv2/core/core.hpp"
- #include "opencv2/highgui/highgui.hpp"
- extern "C" {
- #include <gst/gst.h>
- #include <gst/app/gstappsink.h>
- }
- #include <iostream>
- int main(int argc, char *argv[])
- {
- GstElement* playbin = nullptr;
- GstElement* sink = nullptr;
- gchar *uri;
- gst_init (&argc, &argv);
- if (argc < 2) {
- g_print ("usage: %s <media file or uri>\n", argv[0]);
- return 1;
- }
- if (gst_uri_is_valid (argv[1]))
- uri = g_strdup (argv[1]);
- else
- uri = gst_filename_to_uri (argv[1], NULL);
- playbin = gst_element_factory_make ("playbin", NULL);
- if (!playbin) {
- g_print ("'playbin' gstreamer plugin missing\n");
- return 1;
- }
- sink = gst_element_factory_make ("appsink", "appsink");
- GstCaps* sinkcaps = nullptr;
- sinkcaps = gst_caps_new_simple ("video/x-raw",
- "format", G_TYPE_STRING, "GRAY8",
- "width", G_TYPE_INT, 640,
- "height", G_TYPE_INT, 480,
- "framerate", GST_TYPE_FRACTION, 24, 1,
- NULL);
- g_object_set (sink,"caps",sinkcaps,NULL);
- gst_caps_unref (sinkcaps);
- if ( sink == nullptr ){
- g_print("appsink not initialised\n");
- return 1;
- }
- g_object_set (playbin, "uri", uri, NULL);
- g_object_set (playbin, "mute", TRUE, NULL);
- g_object_set (playbin, "video-sink", sink, NULL);
- g_free (uri);
- // loop = g_main_loop_new (NULL, FALSE);
- gst_element_set_state (playbin, GST_STATE_PLAYING);
- GstSample* sample = nullptr;
- cv::VideoWriter writer;
- writer.open("output.avi",CV_FOURCC('X','V','I','D'),24,cv::Size(640,480),true);
- if (!writer.isOpened())
- {
- std::cout << "Error in opening file for video writing" << std::endl;
- return 0;
- }
- do {
- g_signal_emit_by_name (sink, "pull-sample", &sample);
- if ( !sample )
- break;
- // g_print("got sample: %p", sample);
- // GstBuffer *buffer;
- // GstCaps *caps;
- GstStructure *s;
- gint width, height, fps;
- gboolean res;
- /* get the snapshot buffer format now. We set the caps on the appsink so
- * that it can only be an rgb buffer. The only thing we have not specified
- * on the caps is the height, which is dependant on the pixel-aspect-ratio
- * of the source material */
- GstCaps* caps = gst_sample_get_caps (sample);
- if (!caps)
- {
- g_print ("could not get snapshot format\n");
- std::exit (-5);
- }
- s = gst_caps_get_structure (caps, 0);
- // /* we need to get the final caps on the buffer to get the size */
- res = gst_structure_get_int (s, "width", &width);
- res |= gst_structure_get_int (s, "height", &height);
- res |= gst_structure_get_int(s, "framerate", &fps);
- g_print("w: %d, H: %d, F: %d\n",width,height,fps);
- if (!res)
- {
- g_print ("could not get snapshot dimension\n");
- std::exit (-6);
- }
- // g_print("width %d height %d\n",width,height);
- GstBuffer* buffer = gst_sample_get_buffer (sample);
- GstMapInfo map;
- gst_buffer_map (buffer, &map, GST_MAP_READ);
- cv::Mat image(cv::Size(640, 480), CV_8UC3, (char*)map.data, cv::Mat::AUTO_STEP);
- // g_print("Size %lu\n",map.size);
- writer.write(image);
- // cv::imshow("Gstreamer",image);
- // if (cv::waitKey(1000.0/24.0) == 27)
- // break;
- gst_buffer_unmap (buffer, &map);
- gst_sample_unref (sample);
- }
- while (sample != nullptr);
- gst_element_set_state(playbin, GST_STATE_NULL);
- g_object_unref(playbin);
- // g_source_remove(bus_watch_id)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement