&引用;ffmpeg";:java.io.IOException:错误=24,打开的文件太多 我正在使用FFMPEG生成预览,但是在执行程序的过程中,我得到了这个错误:

&引用;ffmpeg";:java.io.IOException:错误=24,打开的文件太多 我正在使用FFMPEG生成预览,但是在执行程序的过程中,我得到了这个错误:,java,file,Java,File,“ffmpeg”:java.io.IOException:error=24,打开的文件太多 有人知道如何解决或如何避免吗?? 我在使用ffmpeg的地方添加了一段代码: for (int j = 0; j < temp.length; j++) { if(j==2){ String p

“ffmpeg”:java.io.IOException:error=24,打开的文件太多

有人知道如何解决或如何避免吗??
我在使用ffmpeg的地方添加了一段代码:

    for (int j = 0; j < temp.length; j++) {
                                                if(j==2){
                                                    String preview = temp2[i] + temp[j] +".jpg";
                                                    Process p = Runtime.getRuntime().exec("ffmpeg -i anotados/" +temp2[i] + " -r 1 -ss 00:00:"+temp[j]+" -t 1 -s 158x116 imagenes/" + preview);

                                                    TextOut.write(preview+"\n");
                                                }
 }
for(int j=0;j
检查您的
ulimit-n
输出,查看从该shell生成的进程允许有多少个打开的文件。历史上的Unix系统限制为20个文件,但在我的Ubuntu桌面上默认为1024个打开的文件

您可能需要增加
/etc/security/limits.conf
文件中允许打开的文件数。或者,您可能需要修改应用程序以更积极地关闭打开的文件


另一种可能性是对可能打开的文件数量进行系统范围的限制。我不知道哪些现代系统仍然有这样的限制,但首先要看的是
sysctl-a
输出。(好的,也许是第二位,在系统文档之后。)

每次使用
运行时
,它都会打开
stdout
stderr
stdin
。确保在使用完
exec()
后关闭这些流。 差不多

    if(j==2){
       String preview = temp2[i] + temp[j] +".jpg";
       Process p = Runtime.getRuntime().exec("ffmpeg -i anotados/" +temp2[i] + " -r 1 -ss      00:00:"+temp[j]+" -t 1 -s 158x116 imagenes/" + preview);
       TextOut.write(preview+"\n");

       //try this here 
       //add exception handling if necessary
       InputStream is = p.getInputStream();
       InputStream es = p.getErrorStream();
       OutputStream os = p.getOutputStream();
       is.close();
       es.close();
       os.close();
}

请注意,每次调用
Runtime.exec
都会生成一个并行运行的新进程。您确定要以循环所能达到的速度生成进程吗?您可能希望
int exitValue=p.waitFor()
等待进程完成。如果您需要一些并发性,我建议使用
java.util.concurrent.ThreadPoolExecutor
来调度任务

例如,没有太多的错误检查,例如:

final ExecutorService executor = Executors.newFixedThreadPool(2);

for (int j = 0; j < temp.length; j++) {
    if(j==2) {
        final String preview = temp2[i] + temp[j] +".jpg";
        final String ffmpegPreviewCommand = "ffmpeg -i anotados/" +temp2[i] + " -r 1 -ss 00:00:"+temp[j]+" -t 1 -s 158x116 imagenes/" + preview;

        executor.submit(new Callable() {
            @Override
            public Object call() throws Exception {
                final Process p = Runtime.getRuntime().exec(ffmpegPreviewCommand);
                final int exitValue = p.waitFor();
                //TODO Check ffmpeg's exit value.

                TextOut.write(preview+"\n");
            }
        });
}

// This waits for all scheduled tasks to be executed and terminates the executor.
executor.shutdown();
final executor服务executor=Executors.newFixedThreadPool(2);
对于(int j=0;j
}


查看
java.util.concurrent.Executors
以选择适合您需要的执行器。

进程有一个方法:
destroy()

尝试在末尾添加它