2012-08-02 98 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