循环

时间:2015-05-18 07:19:59

标签: java loops ffmpeg runtime exec

我正在编写一个小工具来自动在java中创建一些缩略图。

因此我在Runtime.getRuntime().exec(command);循环中执行for。 现在我的问题是,只创建了第一个缩略图。

到目前为止我的代码:

public static void testFFMpeg(File videoFile) throws IOException {
    FFMpegWrapper wraper = new FFMpegWrapper(videoFile);
    int length = (int) wraper.getInputDuration() / 1000;
    String absolutePath = videoFile.getAbsolutePath();
    String path = absolutePath.substring(0, absolutePath.lastIndexOf('/') + 1);
    int c = 1;
    System.out.println(path + "thumb_" + c + ".png");
    for (int i = 1; i <= length; i = i + 10) {
        int h = i / 3600;
        int m = i / 60;
        int s = i % 60;
        String command = "ffmpeg -i " + absolutePath + " -ss " + h + ":" + m + ":" + s + " -vframes 1 " + path
            + "thumb_" + c + "_" + videoFile.getName() + ".png";
        System.out.println(command);
        Runtime.getRuntime().exec(command);
        c++;
    }
}

输出是:

ffmpeg -i /mnt/Speicherschwein/workspace/testVideos/Roentgen_A_VisarioG2_005.avi -ss 0:0:1 -vframes 1 /mnt/Speicherschwein/workspace/testVideos/thumb_1_Roentgen_A_VisarioG2_005.avi.png
ffmpeg -i /mnt/Speicherschwein/workspace/testVideos/Roentgen_A_VisarioG2_005.avi -ss 0:0:11 -vframes 1 /mnt/Speicherschwein/workspace/testVideos/thumb_2_Roentgen_A_VisarioG2_005.avi.png
ffmpeg -i /mnt/Speicherschwein/workspace/testVideos/Roentgen_A_VisarioG2_005.avi -ss 0:0:21 -vframes 1 /mnt/Speicherschwein/workspace/testVideos/thumb_3_Roentgen_A_VisarioG2_005.avi.png

所以循环运行正常,命令也很好,如果我从命令行手动运行它创建每个缩略图,所以似乎有问题,在2. Runtime.getRuntime().exec(command);的调用中它没有开始,因为第一次运行还没有结束。

是否有可能暂停线程或类似的东西,直到Runtime.getRuntime().exec(command);运行的命令被擦除?

3 个答案:

答案 0 :(得分:0)

因为您当前在一个线程中运行它,所以每次执行exec命令时都尝试打开一个新线程。并在完成创建缩略图后加入线程。

答案 1 :(得分:0)

Runtime.exec会返回Process个实例,您可以使用该实例监控状态。

Process process = Runtime.getRuntime().exec(command);
boolean finished = process.waitFor(3, TimeUnit.SECONDS);

最后一行可以放入循环,或者只是设置一个合理的超时。

答案 2 :(得分:0)

除了MadProgrammer的评论之外,本文可以提供帮助:https://thilosdevblog.wordpress.com/2011/11/21/proper-handling-of-the-processbuilder/

相关问题