bytedeco/javacv

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

Open

#2,192 创建于 2024年2月25日

在 GitHub 查看
 (1 评论) (0 反应) (0 负责人)Java (1,583 fork)batch import
help wantedquestion

仓库指标

Star
 (6,985 star)
PR 合并指标
 (30 天内没有已合并 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;
        }
}

贡献者指南