diff --git a/modules/videoio/include/opencv2/videoio.hpp b/modules/videoio/include/opencv2/videoio.hpp index 6b7b25c6f9..c42de666ae 100644 --- a/modules/videoio/include/opencv2/videoio.hpp +++ b/modules/videoio/include/opencv2/videoio.hpp @@ -233,6 +233,7 @@ enum VideoWriterProperties { VIDEOWRITER_PROP_KEY_FLAG = 11, //!< Set to non-zero to signal that the following frames are key frames or zero if not, when encapsulating raw video (\ref VIDEOWRITER_PROP_RAW_VIDEO != 0). FFmpeg back-end only. VIDEOWRITER_PROP_PTS = 12, //!< Specifies the frame presentation timestamp for each frame using the FPS time base. This property is **only** necessary when encapsulating **externally** encoded video where the decoding order differs from the presentation order, such as in GOP patterns with bi-directional B-frames. The value should be provided by your external encoder and for video sources with fixed frame rates it is equivalent to dividing the current frame's presentation time (\ref CAP_PROP_POS_MSEC) by the frame duration (1000.0 / VideoCapture::get(\ref CAP_PROP_FPS)). It can be queried from the resulting encapsulated video file using VideoCapture::get(\ref CAP_PROP_PTS). FFmpeg back-end only. VIDEOWRITER_PROP_DTS_DELAY = 13, //!< Specifies the maximum difference between presentation (pts) and decompression timestamps (dts) using the FPS time base. This property is necessary **only** when encapsulating **externally** encoded video where the decoding order differs from the presentation order, such as in GOP patterns with bi-directional B-frames. The value should be calculated based on the specific GOP pattern used during encoding. For example, in a GOP with presentation order IBP and decoding order IPB, this value would be 1, as the B-frame is the second frame presented but the third to be decoded. It can be queried from the resulting encapsulated video file using VideoCapture::get(\ref CAP_PROP_DTS_DELAY). Non-zero values usually imply the stream is encoded using B-frames. FFmpeg back-end only. + VIDEOWRITER_PROP_COLOR_SPACE = 14, //!< (**open-only**) GStreamer backend only. Pixel format for the encoding profile. Default is "I420". Other values: "NV12", "BGRx". See GStreamer raw video formats for more options. #ifndef CV_DOXYGEN CV__VIDEOWRITER_PROP_LATEST #endif diff --git a/modules/videoio/src/cap_gstreamer.cpp b/modules/videoio/src/cap_gstreamer.cpp index 24e414c5f9..042cce3c83 100644 --- a/modules/videoio/src/cap_gstreamer.cpp +++ b/modules/videoio/src/cap_gstreamer.cpp @@ -2512,25 +2512,43 @@ bool CvVideoWriter_GStreamer::open( const std::string &filename, int fourcc, CV_WARN("OpenCV backend does not support this file type (extension): " << filename); return false; } - - //create pipeline elements encodebin.reset(gst_element_factory_make("encodebin", NULL)); - + if (!encodebin) + { + CV_WARN("GStreamer: cannot create encodebin element"); + return false; + } GSafePtr containercaps; GSafePtr containerprofile; GSafePtr videoprofile; containercaps.attach(gst_caps_from_string(mime)); - - //create encodebin profile containerprofile.attach(gst_encoding_container_profile_new("container", "container", containercaps.get(), NULL)); - videoprofile.reset(gst_encoding_video_profile_new(videocaps.get(), NULL, NULL, 1)); - gst_encoding_container_profile_add_profile(containerprofile.get(), (GstEncodingProfile*)videoprofile.get()); - + unsigned int colorspace_fourcc = (unsigned int)params.get(VIDEOWRITER_PROP_COLOR_SPACE, CV_FOURCC('I', '4', '2', '0')); + const char* colorspace = gst_video_format_to_string(gst_video_format_from_fourcc(colorspace_fourcc)); + GSafePtr prof_caps; + std::string caps_str = std::string("video/x-raw, format=") + std::string(colorspace); + prof_caps.attach(gst_caps_from_string(caps_str.c_str())); + videoprofile.attach(gst_encoding_video_profile_new(prof_caps.get(), NULL, NULL, 1)); + // Transfer ownership to the container profile + gst_encoding_container_profile_add_profile( + containerprofile.get(), + (GstEncodingProfile*)videoprofile.detach() + ); g_object_set(G_OBJECT(encodebin.get()), "profile", containerprofile.get(), NULL); source.reset(gst_element_factory_make("appsrc", NULL)); + if (!source) + { + CV_WARN("GStreamer: cannot create appsrc element"); + return false; + } file.reset(gst_element_factory_make("filesink", NULL)); + if (!file) + { + CV_WARN("GStreamer: cannot create filesink element"); + return false; + } g_object_set(G_OBJECT(file.get()), "location", (const char*)filename.c_str(), NULL); } @@ -2709,21 +2727,22 @@ void CvVideoWriter_GStreamer::write(InputArray image) Mat imageMat = image.getMat(); const size_t buf_size = imageMat.total() * imageMat.elemSize(); - duration = ((double)1/framerate) * GST_SECOND; - timestamp = num_frames * duration; + duration = gst_util_uint64_scale_int(GST_SECOND, 1, framerate); + timestamp = gst_util_uint64_scale_int(num_frames, GST_SECOND, framerate); //gst_app_src_push_buffer takes ownership of the buffer, so we need to supply it a copy GstBuffer *buffer = gst_buffer_new_allocate(NULL, buf_size, NULL); GstMapInfo info; - gst_buffer_map(buffer, &info, (GstMapFlags)GST_MAP_READ); - memcpy(info.data, (guint8*)imageMat.data, buf_size); - gst_buffer_unmap(buffer, &info); + if (gst_buffer_map(buffer, &info, (GstMapFlags)GST_MAP_WRITE)) { + memcpy(info.data, (guint8*)imageMat.data, buf_size); + gst_buffer_unmap(buffer, &info); + } GST_BUFFER_DURATION(buffer) = duration; GST_BUFFER_PTS(buffer) = timestamp; GST_BUFFER_DTS(buffer) = timestamp; //set the current number in the frame GST_BUFFER_OFFSET(buffer) = num_frames; - + GST_BUFFER_OFFSET_END(buffer) = num_frames + 1; ret = gst_app_src_push_buffer(GST_APP_SRC(source.get()), buffer); if (ret != GST_FLOW_OK) {