【问题标题】:"ffmpeg": java.io.IOException: error=24, Too many open files“ffmpeg”:java.io.IOException:错误=24,打开的文件太多
【发布时间】:2012-01-07 20:49:04
【问题描述】:

我正在使用 ffmpeg 生成预览,但在我的程序执行过程中出现此错误:

"ffmpeg": java.io.IOException: error=24, Too many open files

有人知道如何解决或如何避免吗??
我在使用 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");
                                                }
 }

【问题讨论】:

    标签: java file


    【解决方案1】:

    检查您的ulimit -n 输出以查看允许从该外壳生成的打开文件进程的数量。历史上的 Unix 系统有 20 个文件的限制,但我的 Ubuntu 桌面上的默认打开文件是 1024 个。

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

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

    【讨论】:

      【解决方案2】:

      请注意,每次调用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();
      

      }

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

      【讨论】:

        【解决方案3】:

        进程有一个方法:destroy()
        尝试在最后添加它。

        【讨论】:

          【解决方案4】:

          每次使用Runtime 时,都会打开stdoutstderrstdin。确保在完成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();
          }
          

          【讨论】:

            猜你喜欢
            • 2014-09-21
            • 2012-07-05
            • 1970-01-01
            • 2015-11-30
            • 1970-01-01
            • 1970-01-01
            • 2020-03-07
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多