bytedeco/javacv

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

Open

#2192 aperta il 25 feb 2024

Vedi su GitHub
 (1 commento) (0 reazioni) (0 assegnatari)Java (1583 fork)batch import
help wantedquestion

Metriche repository

Star
 (6985 star)
Metriche merge PR
 (Nessuna PR mergiata in 30 g)

Descrizione

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;
        }
}

Guida contributor