4

When I use in the function: 'x'.write(frame) for write to video file in opencv the program pass the code and I compile it without Errors but when I open the file I see that it's 0 kb and the player can't play it. Can someone help me?

Here my code:

    // Setup output video
    cv::VideoWriter output_cap("output.avi",
        CV_CAP_PROP_FOURCC,
        CV_CAP_PROP_FPS,
        cv::Size(1376, 768));


    // Loop to read frames from the image and write it to the output capture
    cv::Mat frame = imread("1.jpg", 0);
    for(int hgf=1;hgf<=300;hgf++)
    {

        if (!frame.data)
        {
            break;
        }

            output_cap.write(frame);

    }

Good Day everybody!!

Elidor
  • 172
  • 1
  • 21
  • Check this: [**OpenCV VideoWriter won't write anything**](http://stackoverflow.com/questions/12054907/opencv-videowriter-wont-write-anything-although-cvwritetoavi-does) – Khalil Khalaf Aug 12 '16 at 15:51
  • I suspect that opening the file "1.jpg" fails, so there's nothing to write. Start with making sure that it opens properly. – molbdnilo Aug 12 '16 at 19:43
  • it's open "1.JPG" I checked it. – Elidor Aug 14 '16 at 09:07
  • 1
    did you check that it's open and not empty? try displaying it, and add a check on `!frame.empty()`. Start by making sure your input is correct. I suggest using http://docs.opencv.org/2.4/doc/tutorials/introduction/windows_visual_studio_image_watch/windows_visual_studio_image_watch.html – La bla bla Aug 22 '16 at 05:56
  • What are your settings for `CV_CAP_PROP_FOURCC` and `CV_CAP_PROP_FPS`? Have you tried to replace `output_cap.write(frame);` with `output_cap << frame`? And does the number of `frame` channels equal to the `VideoWriter` settings? – Daniel Aug 22 '16 at 06:08
  • @Daniel I tried do output_cap< – Elidor Aug 22 '16 at 06:14
  • @Eliyahu-Shmuel It is very easy. You check the `frame` channel number (should be 3). For most video files, the frame color channel number is 3. Additionally, you should resize the image if the size of it is not 1376x768. – Daniel Aug 22 '16 at 06:16

2 Answers2

2

I think the main problem is that your code passes a bad FOURCC value to VideoWriter. CV_CAP_PROP_FOURCC (#defined as 6) is used to have a name for the FOURCC property but it is not a proper value. Similarly for CV_CAP_PROP_FPS (#defined as 5), but here the effect is just telling VideoWriter to use 5.0 fps.

This works for me:

#include <stdio.h>
#include <opencv2/opencv.hpp>

using namespace cv;
using namespace std;

int main(int argc, char** argv)
{
    if ( argc != 2 ) {
        cout << "image required" << endl;
        return -1;
    }
    Mat frame = imread(argv[1], 1);

    VideoWriter output_cap("output.avi", CV_FOURCC('M','J','P','G'), 15,
        frame.size());

    for(int hgf=1; hgf<=300; hgf++) {
        output_cap.write(frame);
    }

    return 0;
}

Note: On Linux, VideoWriter's support for video formats is so-so in my experience. For the two widely used formats M-JPEG (used above) and H.264, M-JPEG worked well for OpenCV 2.4 but not 3.X and H.264 failed in the same way as in this question for both 2.4 and 3.X.

Community
  • 1
  • 1
Ulrich Stern
  • 10,761
  • 5
  • 55
  • 76
2

I changed my code to this and I changed my image to smaller image and it's work!

my code:

cv::Mat frame = imread("01.jpg",1);
VideoWriter output_cap("output.avi", CV_FOURCC('M', 'J', 'P', 'G'), 1,
frame.size(),true);

for (int hgf = 1; hgf <= 10; hgf++) {
output_cap.write(frame);
std::cout << "-";
}

the problem was that my image was 4608x3456 and the opencv cann't use in this file to create a video. I changed to 752x515 an this work!! good day!!!!!!

Elidor
  • 172
  • 1
  • 21
  • 1
    While it is ok to answer your own question, if your answer is quite close to an answer you already got, Stack Overflow etiquette would be to accept that answer instead and possibly comment. Re: 4608x3456, OpenCV handled a test image with this resolution fine on my Ubuntu 12.04 machine. (Movie Player could not play the resulting video but Avidemux could.) – Ulrich Stern Aug 28 '16 at 06:29
  • @UlrichStern I agree with your comment, but I told my answer that I did not notice I wrote an answer but anyway I leave it because the code that has worked for me with more details. Good Day! – Elidor Aug 28 '16 at 10:47
  • 1
    @UlrichStern It's good to keep in mind though, the questioner has no obligation to even accept an answer. – Christian Dean Aug 29 '16 at 04:31