0

I am attempting to using the OpenCV VideoWriter class to save a two camera video feed to a single video file. I am using the FFMPEG API to ahcieve the desired outcome and am trying to generate an .mkv file with the Fourcc code FFV1 for lossless frame writing. Currently I am expriencing upon launching of the pipeline the file writer is creating a file in the directory specified and upon completion of the pipeline the writer is transformed into a video file .mkv however no frames are being written to said file. My constructor:

cv::VideoWriter writer(out_vid_name + ".mkv", cv::VideoWriter::fourcc('F','F','V','1'), cam_fps,frame_double_size, true);

Note that cv::Size(3840,1080) = frame_double_size and cam_fps is a param

My video capture:

cv::VideoCapture cap(out_vid_name + ".mkv", cv::CAP_FFMPEG);

and my writer:

while (controller->running && ros::ok()) //Shutdown race condition exists. One thread will have to be SIGTERM'ed
        {
            std::shared_lock<std::shared_mutex> guard(doubleSaveFrame->mtx);
            doubleSaveFrame->cond_var.wait(guard, [&]{ return doubleSaveFrame->cond_bool_save; });
            doubleSaveFrame->cond_bool_save = false;
            std_msgs::String msg;
            msg.data = std::to_string(frame_count);
            frame_count++;
            frame_pub.publish(msg);

            writer.write(doubleSaveFrame->getHost());
            cv::Mat frame = doubleSaveFrame->getHost();
            int rows = frame.rows;
            int cols = frame.cols;
            std::cout << "rows: " << std::to_string(rows) << " cols: "<< std::to_string(cols) << std::endl;
            cv::imwrite("home/Documents/camera/imwritefun"+ std::to_string(frame_count) +".JPG", doubleSaveFrame->getHost());
        }
        writer.release();

Note that this code above produced successfully the camera frames to file when cv::imwrite was executed, however the the writer failed to write to video file. I am very new to OpenCV and the associated methods used here, if anyone has an idea on what is causing the issue that would be extremely helpful as research has not prevailed a solution.

  • what is the output of cols and rows in cout part ? `ffmpeg -codecs` includes your input codec ? – Yunus Temurlenk Sep 23 '22 at 08:23
  • rows: 1080 cols: 3840 -output in terminal and I believe so as, "Most codecs are lossy. If you want lossless video file you need to use a lossless codecs (eg. FFMPEG FFV1, Huffman HFYU, Lagarith LAGS, etc...)" Ref. https://docs.opencv.org/3.4/dd/d9e/classcv_1_1VideoWriter.html#ad59c61d8881ba2b2da22cff5487465b5 –  Sep 23 '22 at 08:27
  • ı am suspicious with the codec you are using, can you try other alternatives like MJPG ? – Yunus Temurlenk Sep 23 '22 at 08:28
  • @YunusTemurlenk I changed the Codec as suggested to the MJPG and it had no effect on the issue unfortunately, I did also try several others as well as the .avi file extension with non to prevail as well. –  Sep 23 '22 at 08:38
  • @adav0033 What is the type of `frame`? [Here](https://stackoverflow.com/a/17820615/4926757) is a way for finding the type. How many channels? Use `frame.channels()`. Does the code reach the line `writer.release();`? – Rotem Sep 23 '22 at 08:45
  • @Rotem I implemented the above suggestions and the output was as follows, Matrix: 8UC1 3840x1080 Frame Channels: 1 –  Sep 23 '22 at 09:22
  • `8UC1` is Grayscale, and not colored. Replace the last argument of `writer` from `true` to `false` (the last argument is `isColor`). Make sure the code reaches `writer.release()`. For testing you may break the loop after 50 frames. – Rotem Sep 23 '22 at 09:36
  • @Rotem, Implemented a limit and yes the writer.release() was actually reached and allowed the video the be written, thank you for your help on debugging this issue. –  Sep 23 '22 at 09:58

0 Answers0