2012-08-02 61 views
0

我从Java运行ffmpeg。使用它将flv文件转换为mp3。在cmd上执行进程,不处理

当我运行下面的代码时,ffmpeg启动,创建一个新文件(.mp3),但运行在CPU的0%处。当我停止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); 
    } 

回答

0

我认为,问题是这里的某个地方:

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

Java环境具有极大的优先,不给ffmpeg的足够多的CPU时间。实际上考虑不要使用线程优先级,因为它可能导致不同OS /环境中的不同行为。

而且你在等待这里的ffmpeg命令结束:

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

所以,你会在这里等着,直到完成的ffmpeg,这是很难,如果Java有所有的CPU时间。

+0

我试着按照你的建议。仍然是相同的结果。 – FredTheLover 2012-08-02 18:20:42

+0

你可以尝试执行'gedit'或'notepad.exe'吗?它会产生什么结果?我试过你的代码并运行了'gedit'应用程序。我也见过输出。 – gkuzmin 2012-08-02 18:35:22