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

I work with ffmpeg to create a preview, but I get this error in the middle of my program execution:

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

Does anyone know how to solve or how to avoid it?
I add a code snippet where I use 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"); } } 
+4
source share
4 answers

Check your ulimit -n output to see how many permissions for open files generated from this shell are. Historical Unix systems had a limit of 20 files, but by default there were 1024 open files on my Ubuntu desktop.

You may need to increase the number of open files allowed in the /etc/security/limits.conf file. Or you may need to modify your application to more aggressively close open files.

Another possibility is a system-wide limit on the number of files that can be opened. I don’t know which modern systems will still have such limitations, but the first thing to look at will be sysctl -a . (Well, maybe second place, after system documentation.)

+1
source

Note that each call to Runtime.exec will spawn a new process that runs in parallel. Are you sure you want to create processes as fast as the loop can go? You probably want int exitValue = p.waitFor() wait for the process to complete. If you need concurrency, I would recommend scheduling tasks using java.util.concurrent.ThreadPoolExecutor .

For example, without much error checking, something like this:

 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 exit value. TextOut.write(preview+"\n"); } }); } // This waits for all scheduled tasks to be executed and terminates the executor. executor.shutdown(); 

}

Take a look at java.util.concurrent.Executors to choose an artist that suits your needs.

+1
source

The process has a method: destroy() .

Try adding it to the end.

+1
source

Each time Runtime used, it opens stdout , stderr and stdin . Make sure you close these threads when you are done with exec() . Sort of

  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(); } 
0
source

All Articles