Can not create video in c++ (python works) in JetPack5.0.2 (worked on JetPack4)

(Xavier AGX, JetPack 5.0.2, TRT8.4.1, Cuda11.4)

I’m trying to create a video. I have opencv Mat trying to save into a video file.

In python:

out_size = (1440,1080)
video = cv.VideoWriter(“out.mp4”, cv.VideoWriter_fourcc(‘M’,‘J’,‘P’,‘G’), 20, out_size)
works ok.

When trying in cpp:

int ex = cv::VideoWriter::fourcc(‘m’,‘p’,‘4’,‘v’);
videoWriter->open(“RESULT.mp4”,ex,float(20),cv::Size(w,h),true);

I get garbage file, using ‘M’,‘J’,‘P’,‘G’ same.

I tried also a code that worked on Jetpack 4.XX:

char outSrc[1024];
sprintf(outSrc, “appsrc ! video/x-raw, format=BGR ! queue ! videoconvert ! video/x-raw, format=BGRx ! nvvidconv ! nvv4l2h264enc ! video/x-h264, stream-format=byte-stream ! h264parse ! qtmux ! filesink location=RESULT.mp4”);
int w = 1440;
int h = 1080;
cv::VideoWriter* videoWriter = new cv::VideoWriter();
videoWriter->open(outSrc,0,20,cv::Size(w,h),true);

I get output, but video is distorted.

Tried to re-install ffmpg from source (compile), same.

Any ideas ?

1. For saving as MJPG, may only be using CPU:

  /* Raw MJPG with GSTREAMER backend */
  cv::VideoWriter gst_mjpg_writer("appsrc ! queue ! jpegenc ! filesink location=test-gst-writer.mjpg  ", cv::CAP_GSTREAMER, 0, float(fps), cv::Size (width, height));
  if (!gst_mjpg_writer.isOpened ()) {
    std::cout << "Failed to open gst_mjpg_writer" << std::endl;
    return (-3);
  }

You may playback with (adjust resolution and framerate):

gst-launch-1.0 filesrc location=test-gst-writer.mjpg ! image/jpeg,format=MJPG,width=640,height=480,framerate=30/1 ! queue ! jpegdec ! queue ! videoconvert ! xvimagesink

Or better use a container such AVI or MKV:

  /* GSTREAMER backend  */
  cv::VideoWriter gst_mjpg_avi_writer("appsrc ! video/x-raw,format=BGR ! queue ! jpegenc ! image/jpeg,format=MJPG ! avimux ! filesink location=test-gst-writer.avi ", cv::CAP_GSTREAMER, 0, float(fps), cv::Size (width, height));
  if (!gst_mjpg_avi_writer.isOpened ()) {
    std::cout << "Failed to open gst_mjpg_avi writer" << std::endl;
    return (-4);
  }

  /* FFMPEG backend */
  cv::VideoWriter ffmpeg_mjpg_avi_writer ("ffmpeg-mjpg-writer.avi", cv::VideoWriter::fourcc ('M', 'J', 'P', 'G'), float(fps), cv::Size (width, height));
  if (!ffmpeg_mjpg_avi_writer.isOpened ()) {
    std::cout << "Failed to open ffmpeg-mjpg-writer." << std::endl;
    return (-5);
  } 

2. Or using H264 encoding into container:

/* Gstreamer backend would leverage NVENC HW: H264 to MP4 container */
cv::VideoWriter gst_nvh264_mp4_writer("appsrc ! video/x-raw, format=BGR ! queue ! videoconvert ! video/x-raw,format=BGRx ! nvvidconv ! video/x-raw(memory:NVMM),format=NV12 ! nvv4l2h264enc ! video/x-h264,format=byte-stream ! h264parse ! qtmux ! filesink location=test-nvh264-writer.mp4 ", cv::CAP_GSTREAMER, 0, float(fps), cv::Size (width, height));
if (!gst_nvh264_mp4_writer.isOpened ()) {
    std::cout << "Failed to open gst_nvh264_mp4 writer." << std::endl;
    return (-6);
}
 /* FFMPEG backend using x264 encoder to AVI container */
cv::VideoWriter ffmpeg_h264_avi_writer("test-ffmpeg-h264-writer.avi", cv::CAP_FFMPEG, cv::VideoWriter::fourcc ('X', '2', '6', '4'), float(fps), cv::Size (width, height));
if (!ffmpeg_h264_avi_writer.isOpened ()) {
    std::cout << "Failed to open ffmpeg_h264_avi writer" << std::endl;
    return (-7);
} 

Hi,

(Input imgs for the video were re-sized for 640x480)
tried:

cv::VideoWriter* videoWriter = new cv::VideoWriter(“test-ocv-writer.mjpg”,cv::VideoWriter::fourcc (‘M’, ‘J’, ‘P’, ‘G’), 20,cv::Size (width, height));

gave:

OpenCV: FFMPEG: tag 0x47504a4d/‘MJPG’ is not supported with codec id 7 and format ‘mjpeg / raw MJPEG video’
(and zero size output file)

tried:

cv::VideoWriter gst_mjpg_writer("appsrc ! queue ! nvvidconv ! video/x-raw(memory:NVMM),format=I420 ! nvjpegenc ! filesink location=test-gst-writer.mjpg ", cv::CAP_GSTREAMER, 0, fps, cv::Size (width, height));

gave:

JPEG parameter struct mismatch: library thinks size is 584, caller expects 728

tried:

cv::VideoWriter* videoWriter = new cv::VideoWriter(“test-ffmpeg-h264-writer.mp4”, cv::CAP_FFMPEG, cv::VideoWriter::fourcc (‘X’, ‘2’, ‘6’, ‘4’), float(20), cv::Size (width, height));

gave:

OpenCV: FFMPEG: tag 0x34363258/‘X264’ is not supported with codec id 27 and format 'mp4 / MP4 (MPEG-4 Part 14)
OpenCV: FFMPEG: fallback to use tag 0x31637661/‘avc1’

and 262 bytes output file (error)

tried:

cv::VideoWriter ffmpeg_h264_mp4_writer(“test-ffmpeg-h264-writer.mp4”, cv::CAP_FFMPEG, cv::VideoWriter::fourcc (‘X’, ‘2’, ‘6’, ‘4’), float(fps), cv::Size (width, height));

gave:

OpenCV: FFMPEG: tag 0x34363258/‘X264’ is not supported with codec id 27 and format ‘mp4 / MP4 (MPEG-4 Part 14)’

and zero sized output file.

Any ideas ?
Thanks for the help !

Sorry for the mess. With ffmpeg the issue is selecting AVI or MKV container.
MJPG encoding with HW is not easy because of a different version of jpg library.
I’ve edited my post with better answers.

Hi,

tried all 4. Only:

cv::VideoWriter gst_nvh264_mp4_writer("appsrc ! video/x-raw, format=BGR ! queue ! videoconvert ! video/x-raw,format=BGRx ! nvvidconv ! video/x-raw(memory:NVMM),format=NV12 ! nvv4l2h264enc ! video/x-h264,format=byte-stream ! h264parse ! qtmux ! filesink location=test-nvh264-writer.mp4 ", cv::CAP_GSTREAMER, 0, float(fps), cv::Size (width, height));

gave video file that is not totally corrupted, but the video still looks distorted and multiplied.
the output was:

Opening in BLOCKING MODE
NvMMLiteOpen : Block : BlockType = 4
===== NVMEDIA: NVENC =====
NvMMLiteBlockCreate : Block : BlockType = 4
H264: Profile = 66, Level = 0
NVMEDIA: Need to set EMC bandwidth : 564000
NVMEDIA: Need to set EMC bandwidth : 564000

My loop is basically:

Mat mIn;
Mat mOut;

cv::VideoCapture* cap = new cv::VideoCapture(videoFile); // .mp4 file, plays fine in all players.
cv::VideoWriter* videoWriter = new cv::VideoWriter(MAGIC_LINE-ONE_OF_THE_SUGGESTIONS_ABOVE)
while (!finished) {

(*cap)>>mIn;
videoWriter->write(mIn)

}
videoWriter->release();

I also did cv::imwrite to sample output frames for debugging, looks ok.

Am I missing something ? Need to install anything other than the Jetpack* ? Specific resolution ratios ?

Thanks again for your help, really appreciate it !

Hi,

Found the problem - it’s resolution issue.
Thanks for the help !

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.