我正在使用 ffmpeg_wrapper,我想获取视频的每秒帧数 (fps)。可以用来执行此操作的命令是:
ffmpeg -i video.mp4 2>&1 | sed -n "s/.*, \(.*\) fps.*/\1/p"
这给了我输出:
24
但问题是我无法找到任何方法以 java 代码的形式执行此操作。请注意,我不想使用 ProcessBuilder 或 Runtime.getRuntime().exec(),因为它们会启动另一个进程。
我还有一个用例来捕获我为其编写此代码的视频的屏幕截图:
public void captureScreenshots(String videoPath, String outputPath, int intervalInSeconds) {
FFmpeg ffmpeg = null;
try {
ffmpeg = new FFmpeg();
} catch (IOException e) {
throw new RuntimeException(e);
}
int videoFrameRate=getFrameRate(videoPath);
int frameInterval=videoFrameRate*intervalInSeconds;
String videoFilter="select='not(mod(n,24))',setpts='N/(24*TB)'";
FFmpegBuilder builder = new FFmpegBuilder()
.setInput(videoPath)
.addOutput(outputPath)
.setVideoFilter(videoFilter)
.setFormat("image2")
.done();
FFmpegExecutor executor = null;
try {
executor = new FFmpegExecutor(ffmpeg);
} catch (IOException e) {
throw new RuntimeException(e);
}
executor.createJob(builder).run();
我想用这种类似的方法来获取 fps。有人可以帮忙吗?
为了适应该 API 的 Github 站点上的代码示例,我添加的行显示了帧速率:
import java.io.IOException;
import net.bramp.ffmpeg.probe.FFmpegFormat;
import net.bramp.ffmpeg.probe.FFmpegProbeResult;
import net.bramp.ffmpeg.probe.FFmpegStream;
import net.bramp.ffmpeg.FFprobe;
public class App {
public static void main(String[] args) throws IOException {
FFprobe ffprobe = new FFprobe("/usr/bin/ffprobe");
FFmpegProbeResult probeResult = ffprobe.probe("input.mp4");
FFmpegFormat format = probeResult.getFormat();
System.out.format("%nFile: '%s' ; Format: '%s' ; Duration: %.3fs", format.filename, format.format_long_name,
format.duration);
FFmpegStream stream = probeResult.getStreams().get(0);
System.out.format("%nCodec: '%s' ; Width: %dpx ; Height: %dpx", stream.codec_long_name, stream.width, stream.height);
System.out.format("%nAverage frame rate: %s%n", stream.avg_frame_rate);
}
}