在cmd上执行进程,不处理

时间:2012-08-02 17:56:01

标签: java ffmpeg

我正在从Java运行ffmpeg。用它来将flv文件转换为mp3。

当我运行下面的代码时,ffmpeg启动,创建一个新文件(.mp3一个),但运行在0%的CPU。当我停止JAVA应用程序(来自netbeans)时,ffmpeg保持打开状态,每个Windows任务管理器(CTRL-ALT-DEL)从0%到99%。另一个奇怪的事情是继续。 ffmpeg的输出没有打印出来。

线程正在启​​动但由于某种原因,java没有给它任何时间进行处理。

有关如何解决此问题的任何建议?感谢。

public class ConverterThread extends Thread {

 String fileToConvert;
 String fileToCreate;
 GetSong getSong;



public ConverterThread(String downloadLocationOnDisk, GetSong getSong) {
   this.fileToConvert = downloadLocationOnDisk;
   this.getSong = getSong;
}


public void run(){
    try {
        Thread cur=Thread.currentThread();
        cur.setPriority(Thread.MAX_PRIORITY);

           String downloadLocationOnDisk = fileToConvert;

            if(downloadLocationOnDisk.contains(".flv"))
                fileToCreate = downloadLocationOnDisk.replace(".flv", ".mp3");

            if(downloadLocationOnDisk.contains(".m4a"))
                fileToCreate = downloadLocationOnDisk.replace(".m4a", ".mp3");



            String cmdLine =  "ffmpeg/bin/ffmpeg -i \"" + fileToConvert + "\" \"" + fileToCreate +"\""; 

            System.err.println(cmdLine);

             // run the Unix "ps -ef" command
             // using the Runtime exec method:
             Process p = Runtime.getRuntime().exec(cmdLine);


             BufferedReader stdInput = new BufferedReader(new 
                  InputStreamReader(p.getInputStream()));

             BufferedReader stdError = new BufferedReader(new 
                  InputStreamReader(p.getErrorStream()));

             String s;

             // read the output from the command
             while ((s = stdInput.readLine()) != null) {
                 System.out.println(s);
             }

             // read any errors from the attempted command
             System.out.println("Here is the standard error of the command (if any):\n");
             while ((s = stdError.readLine()) != null) {
                 System.out.println(s);
             }

    } catch (IOException ex) {
        Logger.getLogger(ConverterThread.class.getName()).log(Level.SEVERE, null, ex);
    }

2 个答案:

答案 0 :(得分:1)

答案 1 :(得分:0)

我认为问题出现在这里:

Thread cur=Thread.currentThread();
cur.setPriority(Thread.MAX_PRIORITY);

您的Java环境具有最大优先级,并且不会给ffmpeg提供CPU时间。实际上考虑不要使用线程优先级,因为它可能导致不同操作系统/环境中的不同行为。

此外,您还在等待ffmpeg命令的结束:

while ((s = stdInput.readLine()) != null) {
    System.out.println(s);
}

所以,你会在这里等到ffmpeg完成,如果java有所有的cpu时间,这很难。

相关问题