Read a mp4 and write to anther mp4 creates bigger size

I extracted frames of the mp4 (175KB)file into a folder as jpg files and then trying to write it back to a new mp4 (2.96MB). I see a huge file difference.
Another point i notice is that the data rate is different in each file ,viz input file 164kbps output file 4149kbps. This probably is causing the issue. Although i could not find the exact API to configure this. any help or suggestion would be very helpful. Shared below code for your reference.

I used the code from openCV samples and tutorials available online.

Read mp4 file:
VideoCapture cap = new VideoCapture();

    String input = new File(folderPath, "input.mp4").getAbsolutePath();


    cap.open(input);

    int video_length = (int) cap.get(Videoio.CAP_PROP_FRAME_COUNT);
    int frames_per_second = (int) cap.get(Videoio.CAP_PROP_FPS);
    int frame_number = (int) cap.get(Videoio.CAP_PROP_POS_FRAMES);

    Mat frame = new Mat();

    if (cap.isOpened()) {
        System.out.println("Video is opened");
        System.out.println("Number of Frames: " + video_length);
        System.out.println(frames_per_second + " Frames per Second");
        System.out.println("Converting Video...");

        cap.read(frame);

        while (frame_number <= video_length) {
            Imgcodecs.imwrite(output + "/" + frame_number + ".jpg", frame);
            frame_number++;
        }
        cap.release();

        System.out.println(video_length + " Frames extracted");

    }

    else {
        System.out.println("Fail");
    }

Write to mp4

final String videoFileName = new File(folderPath,"output.mp4").getAbsolutePath();
    System.out.println("videoFileName: " + videoFileName);
    int FOURCC_MJPG = VideoWriter.fourcc('M', 'P', '4', '2');
    File folder = new File(folderPath);
    File[] listOfFiles = folder.listFiles(new FileFilter() {

        @Override
        public boolean accept(File pathname) {
            String path = pathname.getAbsolutePath();
            String extension = path.substring(path.lastIndexOf("."), path.length());
            return extension.contains("jpg");
        }
    });

    VideoWriter writer = null;
    for (int i = 0; i < listOfFiles.length; i++) {
        Mat frame = org.opencv.imgcodecs.Imgcodecs.imread(listOfFiles[i].getAbsolutePath());


        if (writer == null) {
            writer = new VideoWriter(videoFileName, FOURCC_MJPG, fps, new Size(frame.width(), frame.height()), true);
            if(!writer.isOpened()){
                System.out.println("VideoCap.writeFromCameraToFolder() Bummer!!!!!");
                break;
            }
        }

        System.out.println("Captured Frame Width " + frame.width() + " Height " + frame.height());
        writer.write(frame);
        // currentFrame = frame;
        try {
            Thread.currentThread().sleep(66);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        frame.release();
    }
    writer.release();

Leave a Reply

avatar
  Subscribe  
Notify of