brendanw36
brendanw36

Reputation: 93

Some Java Process objects finish and close, but some finish and stall

My program uses ProcessBuilder to make various calls to ffmpeg. My problem is that with certain commands I can create a Process, have it run, and when it is done it will terminate/exit/close itself and the program will end whereas other commands will run and create a finished output (in my case it will finish encoding a file with no corruption or anything at the end of the video), but won't close at which point I need to force terminate the program. I have tested the ffmpeg commands that I am running in Windows Command Prompt and they all run fine without need for user input or anything. I will show some examples of commands that do and don't work, but ultimately what I need is a way to tell why certain Processes do and don't work. You probably don't even need to read the rest of this post if you know the inner workings of the Process class better than I do.

How I create my processes:

ProcessBuilder pb = new ProcessBuilder(commandGoesHere);
Process p = pb.start();
p.waitFor();

Works: ffmpeg -i test.y4m -f segment -segment_times timecodeList .temp/sgmnt_%d.y4m

This command takes a y4m(raw video format/large file size/1.7 GB for 53s of 720p video) and cuts it in to chunks.

Doesn't work (sometimes): ffmpeg -i chunkname.y4m outputName.mkv

This command takes the chunked video and encodes it as h.264/AVC video. When I create a process with this command it only works if the chunk is small in which case the Process will start, do its work, and close.

Doesn't work ever: ffmpeg -i test.mkv -c:v copy -f segment -segment_times timecodeList .temp/sgmnt_%d.mkv

This command takes and h.264/AVC input video and cut it in to chunks, but this one doesn't terminate/exit/close when it's done. I'm forced to terminate the program which I do after seeing the Process's CPU utilization drop to 0% in Task Manager. When I force terminate the program and check the output folder, all the chunks are there and not corrupted so I know it finished running successfully.

Upvotes: 2

Views: 263

Answers (1)

brendanw36
brendanw36

Reputation: 93

So I've solved my problem. I should have read the Javadoc for Process more thoroughly because it warned me of the problem there. The Javadoc warns "Because some native platforms only provide limited buffer size for standard input and output streams, failure to promptly write the input stream or read the output stream of the subprocess may cause the subprocess to block, or even deadlock". ffmpeg was printing out a lot to stdout and I wasn't doing anything to manage it. Instead of managing it in Java I chose to add -hide_banner and -loglevel error to my command so that ffmpeg won't output anything to stdout unless it is an error. In the future I can look in to a more comprehensive solution.

Upvotes: 4

Related Questions