2012-09-07 97 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

相关问题