0

我正在尝试使用 FFmpegFrameFilter 和 JavaCV 的 -grabber 更改视频的帧速率。但是,filter.pull();始终返回 null。

我基本上尝试了拉和推框架的所有组合,但似乎没有任何效果。

这是我的代码:


    FFmpegFrameGrabber grabber = new FFmpegFrameGrabber(new File("input.mp4"));
    grabber.start();

    FFmpegFrameFilter filter = new FFmpegFrameFilter("fps=fps=60", null, grabber.getImageWidth(), grabber.getImageHeight(), 0);
    filter.setVideoInputs(1);
    filter.start();

    Java2DFrameConverter converter = new Java2DFrameConverter();

    for (int i = 0; i < 10; i++) {
        //Push the newly grabbed frame to the filter.
        Frame frame = grabber.grabImage();
        int n = frame.image.length - 1;
        filter.pushImage(n > 0 ? n : 0, frame.imageWidth, frame.imageHeight, frame.imageDepth, frame.imageChannels, frame.imageStride, grabber.getPixelFormat(), frame.image);

        //Pull the frame from the filter, convert it to a BufferedImage, and save it.
        //This line throws an IllegalArgumentException, stating the image is null.
        ImageIO.write(converter.convert(filter.pullImage()), "jpg", new File(outputPath + i + ".jpg"));
    }

我最好的猜测是我给出pushImage(...)了错误的论点,因为我不确定第一个论点n应该是什么。

4

1 回答 1

0

我想出了一种避免过滤器的方法。

class GrabWithDifferentFrameRate {

    private Frame lastFrame;
    private FFmpegFrameGrabber grabber = new FFmpegFrameGrabber(input);

    // Basically converts a frameNumber based on the desiredFrameRate 
    // to one fitting for the grabbers frameRate and returns the frame at that frameNumber
    public Frame grabAt(int frameNumber, int desiredFrameRate) {
        double stepSize = desiredFrameRate / grabber.getFrameRate();
        double adjustedFrameNumber = calculateAdjustedFrameNumber(stepSize);

        Frame frame = lastFrame;
        while ((adjustedFrameNumber <= frameNumber && frameNumber / stepSize < grabber.getLengthInFrames())) {
            frame = grabber.grabImage();
            adjustedFrameNumber = calculateAdjustedFrameNumber(stepSize);
        }

        return frame;
    }

    private double calculateAdjustedFrameNumber(double stepSize) {
        return grabber.getFrameNumber() * stepSize;
    }
}
于 2020-01-15T16:23:17.070 回答