running ffmpeg command in java Process hangs in waitFor()

1.8k views Asked by At

I'm running ffmpeg command to generate video for given images (img001.jpg, img002.jpg ...) it's creating slide.mp4, but it waits infinitely:

public class Ffmpeg {

public static void main(String[] args) throws IOException, InterruptedException {
    String path = "E:\\pics\\Santhosh\\FadeOut\\testing";       
    String cmd = "ffmpeg -r 1/5 -i img%03d.jpg -c:v libx264 -r 30 -y -pix_fmt yuv420p slide.mp4";
    runScript (path, cmd);
}

private static boolean runScript(String path, String cmd) throws IOException, InterruptedException {       
    List<String> commands = new ArrayList<String>();
    commands.add("cmd");
    commands.add("/c");
    commands.add(cmd);
    ProcessBuilder pb = new ProcessBuilder(commands);
    pb.directory(new File(path));
    pb.redirectErrorStream(true);
    Process process = pb.start();   
    flushInputStreamReader(process);                
    int exitCode = process.waitFor();
    return exitCode == 0;
}    
}

private static void flushInputStreamReader (Process process) throws IOException, InterruptedException {
            BufferedReader input = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line=null;
            StringBuilder s = new StringBuilder();
            while((line=input.readLine()) != null) {            
                s.append(line);
            }
        }

Any suggestions?

After writing the function flushInputStreamReader, its working

2

There are 2 answers

0
knucle On

Aside from reading the ErrorStream, there's a better way to handle this.

Add -loglevel quiet to the command, so that the ErrorStream won't overflow and blocking the process at the first place.

0
Hariom Maurya On

Here the working code for the live project:

public void executeHLS() throws IOException, InterruptedException {
        String original_video_file = "C:\\xampp\\htdocs\\hls\\test.mp4";
        String conversion = "cmd.exe /c F:\\java\\ffmpeg\\ffmpeg\\bin\\ffmpeg -i "+original_video_file+" -hls_time 10  -hls_playlist_type vod -hls_segment_filename \"C:\\xampp\\htdocs\\hls\\video_segments_%0d.ts\" C:\\xampp\\htdocs\\hls\\hls_master_for_test.m3u8";
        //String conversion = "cmd.exe /c "+"dir";
        String[] cmds={conversion};
        
        for(int i=0;i<cmds.length;i++) {
             try {
                System.out.println(cmds[i]);
                if(runScript(conversion)) {
                    System.out.println("Operation Successfull!!!!");
                }else {
                    System.out.println("Operation Failed ####");
                }
             } catch (IOException e) {
                 e.printStackTrace();
             }
             //System.exit(0);
            
         }
    }
    private static boolean runScript(String cmd) throws IOException, InterruptedException {       
        ArrayList<String> commands = new ArrayList<String>();
        commands.add("cmd");
        commands.add("/c");
        commands.add(cmd);
        ProcessBuilder pb = new ProcessBuilder(commands);
        //pb.directory(new File(path));
        pb.redirectErrorStream(true);
        Process process = pb.start();   
        flushInputStreamReader(process);                
        int exitCode = process.waitFor();
        return exitCode == 0;
    }    
    
    private static void flushInputStreamReader (Process process) throws IOException, InterruptedException {
            BufferedReader input = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line=null;
            StringBuilder s = new StringBuilder();
            while((line=input.readLine()) != null) {            
                s.append(line);
            }
    }