For your post#1, you would have to get framerate from capture and use the same instead of ‘6’ for VideoWriter if you’re writing a frame for every read frame.
For your post#3, not sure but it seems the input file name lead to confusion, you may check further.
What is your use case ? Reading CSI camera with argus and saving into MJPG file ?
Here is some C++ code doing the same, translating to python would be straight forward (mainly replacing cv:: by cv2.).
This code makes 2 writers. One is using ffmeg backend and writes into file test-ocv-writer.mjpg
, the second one uses gstreamer backend (CPU encoding too), and produces file test-gst-writer.mjpg
:
#include <iostream>
#include <opencv2/opencv.hpp>
#include <opencv2/videoio.hpp>
#include <opencv2/highgui.hpp>
int
main ()
{
setenv ("GST_DEBUG", "*:3", 0);
/* Setup capture with gstreamer pipeline from onboard camera converting into BGR frames for app */
const char *gst_cap =
"nvarguscamerasrc ! video/x-raw(memory:NVMM), format=(string)NV12, width=(int)640, height=(int)480, framerate=(fraction)30/1 ! "
"nvvidconv ! video/x-raw, format=(string)BGRx ! "
"videoconvert ! video/x-raw, format=(string)BGR ! "
"appsink";
cv::VideoCapture cap (gst_cap, cv::CAP_GSTREAMER);
if (!cap.isOpened ()) {
std::cout << "Failed to open camera." << std::endl;
return (-1);
}
unsigned int width = cap.get (cv::CAP_PROP_FRAME_WIDTH);
unsigned int height = cap.get (cv::CAP_PROP_FRAME_HEIGHT);
float fps = cap.get (cv::CAP_PROP_FPS);
unsigned int pixels = width * height;
std::
cout << " Frame size : " << width << " x " << height << ", " << pixels <<
" Pixels " << fps << " FPS" << std::endl;
/* MJPG writer, backend: ffmpeg */
cv::VideoWriter ocv_mjpg_writer ("test-ocv-writer.mjpg",
cv::VideoWriter::fourcc ('M', 'J', 'P', 'G'), fps,
cv::Size (width, height));
if (!ocv_mjpg_writer.isOpened ()) {
std::cout << "Failed to open ocv-mjpg-writer." << std::endl;
return (-2);
}
/* MJPG writer, backend gtsreamer, pipeline encoding into MJPG with CPU codec */
cv::VideoWriter gst_mjpg_writer("appsrc ! queue ! jpegenc ! filesink location=test-gst-writer.mjpg ", 0, fps, cv::Size(width, height));
if (!gst_mjpg_writer.isOpened ()) {
std::cout << "Failed to open gst-mjpg-writer." << std::endl;
return (-4);
}
/* Loop for 3000 frames (100s at 30 fps) */
cv::Mat frame_in;
int frameCount = 0;
while (frameCount++ < 3000) {
if (!cap.read (frame_in)) {
std::cout << "Capture read error" << std::endl;
break;
}
else {
/* Choose a writer...not all at the same time */
ocv_mjpg_writer.write(frame_in);
gst_mjpg_writer.write(frame_in);
}
}
ocv_mjpg_writer.release();
gst_mjpg_writer.release();
cap.release ();
return 0;
}
You would be able to playback the recorded videos with:
gst-launch-1.0 filesrc location=test-ocv-writer.mjpg ! image/jpeg,format=MJPG,framerate=30/1 ! queue2 ! jpegparse ! jpegdec ! queue ! videoconvert ! xvimagesink
gst-launch-1.0 filesrc location=test-gst-writer.mjpg ! image/jpeg,format=MJPG,framerate=30/1 ! queue2 ! jpegparse ! jpegdec ! queue ! videoconvert ! xvimagesink