1

I am making a small project on extracting video frames and remake it into video. How to make sequence images back to video again?

Here is part of my extracting video frames code.

if (n_frame % 3 == 0)
        {
            //Save an image
            sprintf(filename, "frame%.3d.jpg", n_save++);
            imwrite(filename, frame);
            cout << "save: " << filename << endl;

        }

I named my images frame000, frame001, frame002....etc.

I am using opencv 2.4.11.

Thanks a lot!

Jamie Wu
  • 67
  • 2
  • 5
  • Visit [THIS PAGE](http://stackoverflow.com/questions/13623394/how-to-write-video-file-in-opencv-2-4-3?rq=1) – Jeru Luke Feb 16 '17 at 11:26
  • Possible duplicate of [How to write video file in OpenCV 2.4.3](http://stackoverflow.com/questions/13623394/how-to-write-video-file-in-opencv-2-4-3) – Humam Helfawi Feb 16 '17 at 11:28

2 Answers2

1

you used FFmpegFrameRecorder

String path = Environment.getExternalStorageDirectory().getPath() + "/Video_images";

        File folder = new File(path);

        File[] listOfFiles = folder.listFiles();

        if (listOfFiles.length > 0) {

            iplimage = new opencv_core.IplImage[listOfFiles.length];

            for (int j = 0; j < listOfFiles.length; j++) {

                String files = "";

                if (listOfFiles[j].isFile()) {
                    files = listOfFiles[j].getName();
                    System.out.println(" j " + j + listOfFiles[j]);
                }

                String[] tokens = files.split("\\.(?=[^\\.]+$)");
                String name = tokens[0];

                iplimage[j] = cvLoadImage(Environment.getExternalStorageDirectory().getPath() + "/Video_images/" + name + ".jpg");

            }    

recorder = new FFmpegFrameRecorder(Constn.SS, 480, 480);

        try {
            recorder.setVideoCodec(13);
            recorder.setFrameRate(0.4d);
            recorder.setPixelFormat(0);
            recorder.setVideoQuality(1.0d);
            recorder.setVideoBitrate(4000);
            startTime = System.currentTimeMillis();
            recorder.start();
            int time = Integer.parseInt(params[0]);
            resp = "Slept for " + time + " milliseconds";
            for (int i = 0; i < iplimage.length; i++) {
                long t = 1000 * (System.currentTimeMillis() - startTime);
                if (t < recorder.getTimestamp()) {
                    t = recorder.getTimestamp() + 1000;
                }
                recorder.setTimestamp(t);
                recorder.record(iplimage[i]);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
Manish Godhani
  • 189
  • 1
  • 5
0

You need VideoWriter - http://docs.opencv.org/trunk/dd/d9e/classcv_1_1VideoWriter.html

Once you construct it with desired file type and path, you feed it with Mat objects containing frames using << operator - i.e.

auto frame = cv::imread("somePicture.png");
auto writer = cv::VideoWriter("out.avi", VideoWriter::fourcc('M','J','P','G'), 24, frame.size());
writer << frame;
writer.release();

The code above will read frame from file, feed it into a video file which has 24fps and MJPG format and AVI container and then the release() method will close the writer.

Max Walczak
  • 433
  • 2
  • 12