Jets on nano not saving video file properly

Hi,

I can’t get jets on nano to save jetson nano to save video files, avi video keeps showing the size = 0 when saved but works fine on Linux. What do I need to here.

Using opencv

Thanks,
# Release handle to the webcam
video_capture.release()
cv2.destroyAllWindows()

#Capture a short video recording onceface is detected

def captureIntruder(sendTimeStamp):

# video1 = cv2.VideoCapture(0)
video1 = cv2.VideoCapture(get_jetson_gstreamer_source(), cv2.CAP_GSTREAMER)
frame_width = int(video1.get(3)) 
frame_height = int(video1.get(4)) 
size = (frame_width, frame_height)   

# Below VideoWriter object will create 
# a frame of above defined The output  
# is stored in 'filename.avi' file. 
savename = sendTimeStamp+'.avi'
result = cv2.VideoWriter(savename,  
                         cv2.VideoWriter_fourcc(*'MJPG'), 
                         6, size)

import time
timeout = time.time() + 2   # 5 seconds from now
# print("Timeout -",timeout)
while True:
    # print(" current Time " , time.time())
    test = 0
    if test == 5 or time.time() > timeout:
        break

    ret, frame = video1.read() 
    result.write(frame)

if name == “main”:
load_known_faces()
main_loop()

Hi,
Please refer to the sample:

Should I run script as is or I need to make changes. If so, where?

Tried it as is, it pops out errors

Opening in BLOCKING MODE 

[ WARN:0] global /home/nvidia/host/build_opencv/nv_opencv/modules/videoio/src/cap_gstreamer.cpp (1757) handleMessage OpenCV | GStreamer warning: Embedded video playback halted; module filesrc0 reported: Resource not found.
[ WARN:0] global /home/nvidia/host/build_opencv/nv_opencv/modules/videoio/src/cap_gstreamer.cpp (886) open OpenCV | GStreamer warning: unable to start pipeline
[ WARN:0] global /home/nvidia/host/build_opencv/nv_opencv/modules/videoio/src/cap_gstreamer.cpp (480) isPipelinePlaying OpenCV | GStreamer warning: GStreamer: pipeline have not been created
[ERROR:0] global /home/nvidia/host/build_opencv/nv_opencv/modules/videoio/src/cap.cpp (116) open VIDEOIO(CV_IMAGES): raised OpenCV exception:

OpenCV(4.1.1) /home/nvidia/host/build_opencv/nv_opencv/modules/videoio/src/cap_images.cpp:253: error: (-5:Bad argument) CAP_IMAGES: can’t find starting number (in the name of file): filesrc location=/home/moon/Desktop/smartsurvelliance/sample_1080p_h264.mp4 ! qtdemux ! h264parse ! nvv4l2decoder ! nvvidconv ! video/x-raw, format=BGRx ! videoconvert ! video/x-raw,format=BGR ! appsink in function ‘icvExtractPattern’

Src opened, 0x0 @ 0 fps
[ERROR:0] global /home/nvidia/host/build_opencv/nv_opencv/modules/videoio/src/cap.cpp (392) open VIDEOIO(GSTREAMER): raised OpenCV exception:

OpenCV(4.1.1) /home/nvidia/host/build_opencv/nv_opencv/modules/videoio/src/cap_gstreamer.cpp:1392: error: (-215:Assertion failed) fps > 0 in function ‘open’

Failed to open output

Hi,
You can save it to a file such as test.py, put a video file at

/home/nvidia/sample_1080p_h264.mp4

and run

$ python test.py

Here’s the thing, I’mstill not able to save the video files. which is what I want to do since I can’t find a way to stream directly to our mobile app without have a roadblock\

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