我正在为Raspberry Pi开发Java项目,需要帮助以使Runtime正常工作。
youtube2mp3使用youtube-dl和ffmpeg(信息: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文件,不需要管理员密码(我在程序中使用Runtime()进行了arp扫描)
我能够将规则复制并粘贴到“命令”输出的终端中,并且运行正常。
任何帮助将不胜感激。
更新
这是运行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
最佳答案
来自Process类文档:由于某些本机平台仅为标准输入和输出流提供了有限的缓冲区大小,因此未能及时写入子流程的输入流或读取子流程的输出流可能导致子流程阻塞甚至死锁。
您提到您得到了详细的输出,因此我将尝试将child.waitFor();
移动到try
块的末尾。您无需担心会丢失任何东西。相反,流数据在OS中非常自然。我很确定InputStream在过程完成之前不会关闭。