Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
mumoing opened this issue Feb 25, 2024 · 1 comment

Comments

@mumoing
Copy link

mumoing commented Feb 25, 2024

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;
        }
}
@saudet
Copy link
Member

saudet commented Mar 16, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants