在java中运行ffmpeg命令进程在waitFor()中挂起

时间:2015-06-09 09:46:53

标签: java ffmpeg

我正在运行ffmpeg命令为给定图像生成视频(img001.jpg,img002.jpg ...)它正在创建slide.mp4,但它无限期地等待:

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

有什么建议吗?

编写函数flushInputStreamReader后,其工作

2 个答案:

答案 0 :(得分:0)

除了阅读ErrorStream之外,还有一种更好的方法来处理它。

-loglevel quiet添加到命令中,以便ErrorStream不会溢出并在第一个位置阻止进程。

答案 1 :(得分:0)

这里是实时项目的工作代码:

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);
            }
    }
相关问题