1

我正在为我的 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
4

2 回答 2

1

来自Process类文档:由于一些原生平台只为标准输入输出流提供有限的缓冲区大小,未能及时写入子进程的输入流或读取输出流可能会导致子进程阻塞,甚至死锁。

你提到你得到一个详细的输出,所以我会尝试将 移到块child.waitFor();的末尾try。你不必担心你会失去任何东西。相反,流数据在操作系统中非常自然。我很确定 InputStream 在进程完成之前不会关闭。

于 2012-09-07T16:10:00.947 回答
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);
    }



    }
于 2012-09-07T22:51:40.293 回答