2012-09-07 106 views
1

我正在爲Raspberry Pi開發一個Java項目,並且需要幫助讓Runtime正常工作。Java Runtime.getRuntime()。exec()youtube2mp3

youtube2mp3利用YouTube-DL和ffmpeg的(INFO:http://jeffreyv.hubpages.com/hub/Youtube-to-MP3-on-Ubuntu-Linux

我修改劇本有點一邊寫上我的iMac代碼:

x=youtube-dl-$RANDOM-$RANDOM.flv 
youtube-dl --output=$x --format=18 "$1" 
ffmpeg -i $x -acodec libmp3lame -ac 2 -ab 128k -vn -y "$2" 
mv "$2" ~/Downloads 
rm $x 
rm *.mp4 

然後我累調用它在我的servlet開始下載:

try { 
     String command = "sudo /usr/local/bin/youtube2mp3 \"" + requestedSong.getTrackUrl() +"\" \"" + requestedSong.getTrackArtist() + "-" + requestedSong.getTrackTitle() + ".mp3\""; 
     System.out.println("CMD: " + command); 
     //Process child = Runtime.getRuntime().exec(new String[]{"/usr/local/bin/youtube2mp3", "\"" + requestedSong.getTrackUrl() + "\"", "\"" + requestedSong.getTrackArtist() + "-" + requestedSong.getTrackTitle() + ".mp3 \""}); 
     Process child = Runtime.getRuntime().exec(command); 
     child.waitFor(); 
     InputStream in = child.getInputStream(); 
     Scanner sc = new Scanner(new BufferedReader(new InputStreamReader(in))); 
     String line = ""; 
     while (sc.hasNextLine()) { 
      line = sc.nextLine(); 
      System.out.println("INFO: " + line); 
     } 
    } catch (InterruptedException ex) { 
     Logger.getLogger(ControlServlet.class.getName()).log(Level.SEVERE, null, ex); 
    } catch (IOException ex) { 
     Logger.getLogger(ControlServlet.class.getName()).log(Level.SEVERE, null, ex); 
    } 


} 

該命令在運行時相當詳細,但我的掃描器對象沒有拾取任何東西。我試着把這段代碼留在servlet中,並且使用線程,但是兩次都失敗了。

的命令不需要sudo來運行,我已經改變sudoers文件不要求對管理員的密碼

我(我使用運行時()運行ARP掃描早些時候在節目中)能夠將規則複製並粘貼到「命令」輸出的終端中,並且運行良好。

任何幫助將不勝感激。

UPDATE

這裏是運行youtube2mp3命令的輸出:

sudo /usr/local/bin/youtube2mp3 "http://www.youtube.com/watch?v=X_tbksFYhl4" "test-none.mp3" 
[youtube] Setting language 
[youtube] X_tbksFYhl4: Downloading video webpage 
[youtube] X_tbksFYhl4: Downloading video info webpage 
[youtube] X_tbksFYhl4: Extracting video information 
[download] Destination: youtube-dl-7433-2196.flv 
[download] 100.0% of 39.17M at 1.83M/s ETA 00:00 
ffmpeg version 0.11.1 Copyright (c) 2000-2012 the FFmpeg developers 
built on Sep 6 2012 13:52:52 with clang 4.0 ((tags/Apple/clang-421.0.60)) 
configuration: --prefix=/usr/local/Cellar/ffmpeg/0.11.1 --enable-shared --enable-gpl --enable-version3 --enable-nonfree --enable-hardcoded-tables --cc=cc --host-cflags= --host-ldflags= --enable-libx264 --enable-libfaac --enable-libmp3lame --enable-libxvid 
libavutil  51. 54.100/51. 54.100 
libavcodec  54. 23.100/54. 23.100 
libavformat 54. 6.100/54. 6.100 
libavdevice 54. 0.100/54. 0.100 
libavfilter  2. 77.100/2. 77.100 
libswscale  2. 1.100/2. 1.100 
libswresample 0. 15.100/0. 15.100 
libpostproc 52. 0.100/52. 0.100 
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'youtube-dl-7433-2196.flv': 
Metadata: 
major_brand  : mp42 
minor_version : 0 
compatible_brands: isomavc1mp42 
creation_time : 2010-01-17 00:16:32 
Duration: 00:09:59.23, start: 0.000000, bitrate: 548 kb/s 
Stream #0:0(und): Audio: aac (mp4a/0x6134706D), 44100 Hz, stereo, s16, 113 kb/s 
Metadata: 
    creation_time : 2010-01-17 00:16:32 
    handler_name : (C) 2007 Google Inc. v08.13.2007. 
Stream #0:1(und): Video: h264 (Constrained Baseline) (avc1/0x31637661), yuv420p, 480x270 [SAR 1:1 DAR 16:9], 432 kb/s, 29.97 fps, 29.97 tbr, 30k tbn, 59.94 tbc 
Metadata: 
    creation_time : 2010-01-17 00:16:33 
    handler_name : (C) 2007 Google Inc. v08.13.2007. 
Output #0, mp3, to 'test-none.mp3': 
Metadata: 
major_brand  : mp42 
minor_version : 0 
compatible_brands: isomavc1mp42 
TDEN   : 2010-01-17 00:16:32 
TSSE   : Lavf54.6.100 
Stream #0:0(und): Audio: mp3, 44100 Hz, stereo, s16, 128 kb/s 
Metadata: 
    creation_time : 2010-01-17 00:16:32 
    handler_name : (C) 2007 Google Inc. v08.13.2007. 
Stream mapping: 
Stream #0:0 -> #0:0 (aac -> libmp3lame) 
Press [q] to stop, [?] for help 
size= 9364kB time=00:09:59.22 bitrate= 128.0kbits/s  
video:0kB audio:9363kB global headers:0kB muxing overhead 0.006039% 
rm: *.mp4: No such file or directory 
+0

爲什麼在嘗試讀取標準輸出之前等待進程完成? –

+0

我想確保我抓住了一切。我用arp-scan做了同樣的事情,以確保我得到了所有Mac地址和/或錯誤。這不應該導致問題。 – Ctcoggin

回答

1

從Process類DOC:由於某些本地平臺只提供用於標準輸入和輸出流有限緩衝區大小,未能及時寫入輸入流或讀取子流程的輸出流可能導致子流程阻塞,甚至死鎖。

您提到您會得到一個詳細輸出,所以我會嘗試將child.waitFor();移動到try塊的末尾。你不必擔心你會失去任何東西。相反,流式數據在OS中是非常自然的。我很確定InputStream在處理完成之前不會被關閉。

+0

感謝您的回答。但它仍然沒有幫助。我仍然沒有觸及while循環的內部。我也無法在計算機上的任何位置找到輸出文件。還有什麼要嘗試? – Ctcoggin

+0

那麼究竟哪一條線卡住了?緊接着'Process child = Runtime.getRuntime()。exec(command);'? –

+0

該servlet繼續運行而不會引發錯誤。我只是不知道youtube2mp3進程正在運行,因爲它現在顯示任何輸出。所以它從來沒有真的變得「卡住」。我只是不知道爲什麼它沒有運行命令。我將在終端中運行時更新問題以顯示命令的輸出。 – Ctcoggin

0

終於搞定了。不得不使用帶有「/ bin/sh」的字符串數組

try { 
     String outputInput = requestedSong.getTrackArtist() + "-" + requestedSong.getTrackTitle() + ".flv"; 
     String cmd1 = "sudo /usr/local/bin/youtube-dl --output=\"" + outputInput +"\" --format=18 \"" + requestedSong.getTrackUrl() +"\""; 
     String cmd2 = "sudo /usr/local/bin/ffmpeg -i \"" + outputInput +"\" -acodec libmp3lame -ac 2 -ab 128k -vn -y \"" + requestedSong.getTrackArtist() + " - " + requestedSong.getTrackTitle() +".mp3\""; 
     String cmd3 = "mv \"" + requestedSong.getTrackArtist() + " - " + requestedSong.getTrackTitle() +".mp3\" ~/Downloads"; 
     String cmd4 = "rm -f outputInput"; 

     String[] cmdList = {cmd1, cmd2, cmd3, cmd4}; 

     for(int i = 0; i < cmdList.length; i++) 
     { 
      String[] cmd = {"/bin/sh", "-c", cmdList[i]}; 
      System.out.println("CMD: " + cmdList[i]); 
      Process child = Runtime.getRuntime().exec(cmd); 
      InputStream in = child.getInputStream(); 
      Scanner sc = new Scanner(new BufferedReader(new InputStreamReader(in))); 
      String line = ""; 
      while (sc.hasNextLine()) 
      { 
       line = sc.nextLine(); 
       System.out.println("INFO: " + line); 
      } 
      child.waitFor(); 
     } 
    } catch (InterruptedException ex) { 
     Logger.getLogger(ControlServlet.class.getName()).log(Level.SEVERE, null, ex); 
    } catch (IOException ex) { 
     Logger.getLogger(ControlServlet.class.getName()).log(Level.SEVERE, null, ex); 
    } 



    } 
+0

請訪問['exec' info。鏈接的Java World文章。頁面(http://stackoverflow.com/tags/runtime.exec/info)。該代碼仍然存在問題。 –

+0

它下載並做我需要的一切。 – Ctcoggin

相關問題