javacv icon indicating copy to clipboard operation
javacv copied to clipboard

How to make the pictType of each video frame of the output video consistent with the original video

Open mumoing opened this issue 1 year ago • 1 comments

Crop a video based on the start video frame number and end video frame number. The original video has 'P' frames and 'B' frames (pictType='p'), but the output videos are all 'P' frames. How to make the pictType of each video frame of the output video consistent with the original video;

// beginFrameNum : video begin Frame number;
public static void cutNewVideo(FFmpegFrameGrabber grabber, String targetPath, int beginFrameNum,int endFrameNum) throws Exception {
        try {
            FFmpegFrameRecorder recorder = new FFmpegFrameRecorder(targetPath, grabber.getImageWidth(), grabber.getImageHeight(), grabber.getAudioChannels());

            recorder.setVideoCodec(grabber.getVideoCodec());
            recorder.setFormat(grabber.getFormat());
            recorder.setFrameRate(grabber.getFrameRate());

            recorder.start();
            Frame frame = null;
            grabber.setVideoFrameNumber(beginFrameNum);
            while ((frame = grabber.grabFrame()) != null) {
                if (frame.type.toString().equals("VIDEO")) { // video frame
                    if (grabber.getFrameNumber() >  endFrameNum) { 
                        break;
                    }
                }
                recorder.record(frame);
            }
            recorder.stop();
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
}

mumoing avatar Feb 25 '24 06:02 mumoing

We can easily accomplish this with the ffmpeg program: http://bytedeco.org/javacpp-presets/ffmpeg/apidocs/org/bytedeco/ffmpeg/ffmpeg.html

saudet avatar Mar 16 '24 10:03 saudet