7

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

5
  • If memory serves well, I think the problem is stuff in the InputStreamReader, u either have to read them out or disable it Commented Jun 9, 2015 at 10:11
  • btw, just do system.out.println("hello"); after runScript(path,cmd); Commented Jun 9, 2015 at 10:16
  • Hi @nafas after inserting system.out.println("hello"); its not working, how InputStreamReader related this ? Commented Jun 9, 2015 at 10:58
  • i believe the process waits, because technically program hasn't been finished . since there are still data to be read. so you can try to clear the stream (by reading it) and see if it works. as i said, I'm not 100% here as it was a while ago I had this problem Commented Jun 9, 2015 at 12:08
  • Thanks @nafas, its worked, edited the code with solution Commented Jun 9, 2015 at 13:10

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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);
            }
    }

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.