java - 如何从java中的ffmpeg命令读取百分比?
问题描述
我正在尝试通过执行 ffmpeg 命令将视频文件转换为特定格式。在那个过程中,我想通过使用时间模式格式来读取百分比。不知何故,我无法做到。
我试过使用下面的代码。特别是我在 while 循环条件下得到空值。
import java.io.*;
import java.util.Scanner;
import java.util.regex.Pattern;
class Test {
public static void main(String[] args) throws IOException {
ProcessBuilder pb = new ProcessBuilder("ffmpeg","-i","in.webm","out.mp4");
final Process p = pb.start();
new Thread() {
public void run() {
Scanner sc = new Scanner(p.getErrorStream());
// Find duration
Pattern durPattern = Pattern.compile("(?<=Duration: )[^,]*");
String dur = sc.findWithinHorizon(durPattern, 0);
if (dur == null)
throw new RuntimeException("Could not parse duration.");
String[] hms = dur.split(":");
double totalSecs = Integer.parseInt(hms[0]) * 3600
+ Integer.parseInt(hms[1]) * 60
+ Double.parseDouble(hms[2]);
System.out.println("Total duration: " + totalSecs + " seconds.");
// Find time as long as possible.
Pattern timePattern = Pattern.compile("(?<=time=)[\\d.]*");
String match;
while (null != (match = sc.findWithinHorizon(timePattern, 0))) {
double progress = Double.parseDouble(match) / totalSecs;
System.out.printf("Progress: %.2f%%%n", progress * 100);
}
}
}.start();
}
}
我期待 while 条件中有一个值,但它为空。enter code here
解决方案
推荐阅读
- javascript - 使Angular mat-table中的行数长于dataSource的长度
- python - 给定一个包含多个日期和每个日期多次的 pandas 数据框,我如何选择每个日期的时间?
- list - 引用列表包含可变和不可变元素python
- python - 弃用警告:无效的转义序列 \D
- amazon-web-services - 授予对 AWS Secret Manager 中特定密钥的标签为 t 的所有 Lambda 的访问权限
- python - 如何将列表中的字符串更改为整数
- tensorflow - 如何预处理我的 Mapdataset 以适应我的模型输入?
- python - 将 exe 转换为为用户创建快捷方式的安装程序
- python - Python Rock - Paper Scissor 项目 - 如何在列表中增加变量?
- java - 如何使用 jdbi 返回已删除的行?