bytedeco/javacv

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

Open

#2,192 opened on 2024年2月25日

GitHub で見る
 (1 comment) (0 reactions) (0 assignees)Java (1,583 forks)batch import
help wantedquestion

Repository metrics

Stars
 (6,985 stars)
PR merge metrics
 (30d に merged PR はありません)

説明

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

コントリビューターガイド