Advertisement
Guest User

Untitled

a guest
Mar 24th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.64 KB | None | 0 0
  1. #include "opencv2/core/core.hpp"
  2. #include "opencv2/highgui/highgui.hpp"
  3.  
  4. extern "C" {
  5. #include <gst/gst.h>
  6. #include <gst/app/gstappsink.h>
  7. }
  8.  
  9. #include <iostream>
  10.  
  11. int main(int argc, char *argv[])
  12. {
  13. GstElement* playbin = nullptr;
  14. GstElement* sink = nullptr;
  15. gchar *uri;
  16.  
  17. gst_init (&argc, &argv);
  18.  
  19. if (argc < 2) {
  20. g_print ("usage: %s <media file or uri>\n", argv[0]);
  21. return 1;
  22. }
  23.  
  24. if (gst_uri_is_valid (argv[1]))
  25. uri = g_strdup (argv[1]);
  26. else
  27. uri = gst_filename_to_uri (argv[1], NULL);
  28.  
  29. playbin = gst_element_factory_make ("playbin", NULL);
  30.  
  31. if (!playbin) {
  32. g_print ("'playbin' gstreamer plugin missing\n");
  33. return 1;
  34. }
  35.  
  36. sink = gst_element_factory_make ("appsink", "appsink");
  37. GstCaps* sinkcaps = nullptr;
  38. sinkcaps = gst_caps_new_simple ("video/x-raw",
  39. "format", G_TYPE_STRING, "GRAY8",
  40. "width", G_TYPE_INT, 640,
  41. "height", G_TYPE_INT, 480,
  42. "framerate", GST_TYPE_FRACTION, 24, 1,
  43. NULL);
  44. g_object_set (sink,"caps",sinkcaps,NULL);
  45. gst_caps_unref (sinkcaps);
  46.  
  47. if ( sink == nullptr ){
  48. g_print("appsink not initialised\n");
  49. return 1;
  50. }
  51.  
  52. g_object_set (playbin, "uri", uri, NULL);
  53. g_object_set (playbin, "mute", TRUE, NULL);
  54. g_object_set (playbin, "video-sink", sink, NULL);
  55. g_free (uri);
  56.  
  57. // loop = g_main_loop_new (NULL, FALSE);
  58. gst_element_set_state (playbin, GST_STATE_PLAYING);
  59.  
  60. GstSample* sample = nullptr;
  61. cv::VideoWriter writer;
  62. writer.open("output.avi",CV_FOURCC('X','V','I','D'),24,cv::Size(640,480),true);
  63. if (!writer.isOpened())
  64. {
  65. std::cout << "Error in opening file for video writing" << std::endl;
  66. return 0;
  67. }
  68.  
  69. do {
  70. g_signal_emit_by_name (sink, "pull-sample", &sample);
  71. if ( !sample )
  72. break;
  73. // g_print("got sample: %p", sample);
  74. // GstBuffer *buffer;
  75. // GstCaps *caps;
  76. GstStructure *s;
  77. gint width, height, fps;
  78. gboolean res;
  79. /* get the snapshot buffer format now. We set the caps on the appsink so
  80. * that it can only be an rgb buffer. The only thing we have not specified
  81. * on the caps is the height, which is dependant on the pixel-aspect-ratio
  82. * of the source material */
  83. GstCaps* caps = gst_sample_get_caps (sample);
  84. if (!caps)
  85. {
  86. g_print ("could not get snapshot format\n");
  87. std::exit (-5);
  88. }
  89. s = gst_caps_get_structure (caps, 0);
  90.  
  91. // /* we need to get the final caps on the buffer to get the size */
  92. res = gst_structure_get_int (s, "width", &width);
  93. res |= gst_structure_get_int (s, "height", &height);
  94. res |= gst_structure_get_int(s, "framerate", &fps);
  95. g_print("w: %d, H: %d, F: %d\n",width,height,fps);
  96.  
  97. if (!res)
  98. {
  99. g_print ("could not get snapshot dimension\n");
  100. std::exit (-6);
  101. }
  102. // g_print("width %d height %d\n",width,height);
  103. GstBuffer* buffer = gst_sample_get_buffer (sample);
  104. GstMapInfo map;
  105. gst_buffer_map (buffer, &map, GST_MAP_READ);
  106. cv::Mat image(cv::Size(640, 480), CV_8UC3, (char*)map.data, cv::Mat::AUTO_STEP);
  107. // g_print("Size %lu\n",map.size);
  108. writer.write(image);
  109. // cv::imshow("Gstreamer",image);
  110. // if (cv::waitKey(1000.0/24.0) == 27)
  111. // break;
  112.  
  113. gst_buffer_unmap (buffer, &map);
  114. gst_sample_unref (sample);
  115.  
  116. }
  117. while (sample != nullptr);
  118.  
  119. gst_element_set_state(playbin, GST_STATE_NULL);
  120. g_object_unref(playbin);
  121. // g_source_remove(bus_watch_id)
  122.  
  123.  
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement