【问题标题】:Read Outputinformation of ffmpeg-process runned from java Runtime.exec(...)读取从 java Runtime.exec(...) 运行的 ffmpeg-process 的输出信息
【发布时间】:2015-09-24 16:21:51
【问题描述】:

我想读出一个用 ffmpeg 加载的视频文件的信息。 ffmpeg 在控制台中的输出:

[sebastian@ULBP2681 ~]$ ffmpeg -i /mnt/Speicherschwein/workspace/testVideos/00-50-C2-1D-7F-85_005.avi
ffmpeg version 2.7.1 Copyright (c) 2000-2015 the FFmpeg developers
  built with gcc 5.1.0 (GCC)
  configuration: --prefix=/usr --disable-debug --disable-static --disable-stripping --enable-avisynth --enable-avresample --enable-fontconfig --enable-gnutls --enable-gpl --enable-libass --enable-libbluray --enable-libfreetype --enable-libfribidi --enable-libgsm --enable-libmodplug --enable-libmp3lame --enable-libopencore_amrnb --enable-libopencore_amrwb --enable-libopenjpeg --enable-libopus --enable-libpulse --enable-libschroedinger --enable-libspeex --enable-libssh --enable-libtheora --enable-libv4l2 --enable-libvorbis --enable-libvpx --enable-libx264 --enable-libx265 --enable-libxvid --enable-shared --enable-version3 --enable-x11grab
  libavutil      54. 27.100 / 54. 27.100
  libavcodec     56. 41.100 / 56. 41.100
  libavformat    56. 36.100 / 56. 36.100
  libavdevice    56.  4.100 / 56.  4.100
  libavfilter     5. 16.101 /  5. 16.101
  libavresample   2.  1.  0 /  2.  1.  0
  libswscale      3.  1.101 /  3.  1.101
  libswresample   1.  2.100 /  1.  2.100
  libpostproc    53.  3.100 / 53.  3.100
Input #0, avi, from '/mnt/Speicherschwein/workspace/testVideos/00-50-C2-1D-7F-85_005.avi':
  Duration: 00:00:21.12, start: 0.000000, bitrate: 303 kb/s
    Stream #0:0: Video: h264 (High) (H264 / 0x34363248), yuv420p, 512x512 [SAR 1:1 DAR 1:1], 268 kb/s, 50 fps, 50 tbr, 50 tbn, 50 tbc

我希望在 Duration 中拥有的信息。

所以现在我的问题: 我通过

运行ffmpeg
String command = "ffmpeg -i " + absolutePath;
Process processDuration = Runtime.getRuntime().exec(command);

我需要将控制台输出(不在控制台中打印输出)读出到 Sting 中。

我试图从 outputStream 中读取,但这似乎不起作用,因为 inputStream 不起作用。

谁能给我提示我如何读出通过 java Runtime.getRuntime().exec(command); 运行的进程的控制台输出?

【问题讨论】:

  • 另请参阅When Runtime.exec() won't,了解有关正确创建和处理流程的许多好技巧。然后忽略它引用exec 并使用ProcessBuilder 创建进程。

标签: java string ffmpeg console runtime.exec


【解决方案1】:

我使用 FFProbe 完成了这项工作,我确信类似的代码也适用于 FFMPEG。 您可以打印出 Json 并对其进行解析以获取 Duration。

    Process process = Runtime.getRuntime().exec(
            new String[] { "./ffprobe", "-v", "quiet", "-print_format", "json", "-show_format",
                    "-show_streams", filePath});

    process.waitFor();
    BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));

    String line = "";
    String outputJson = "";
    while ((line = reader.readLine()) != null) {
        outputJson = outputJson + line;
    }

您可以解析outputJson 并获取持续时间和其他内容。

【讨论】:

  • 嗯,这也是我发现的,但由于某种原因,我不知道为什么,outputJson 总是空的("")。
【解决方案2】:

我得到了解决方案:使用 ProcesssBuilder 和 StringBuilder!

Process processDuration = new ProcessBuilder("ffmpeg", "-i", absolutePath).redirectErrorStream(true).start();
StringBuilder strBuild = new StringBuilder();
try (BufferedReader processOutputReader = new BufferedReader(new InputStreamReader(processDuration.getInputStream(), Charset.defaultCharset()));) {
    String line;
    while ((line = processOutputReader.readLine()) != null) {
        strBuild.append(line + System.lineSeparator());
    }
    processDuration.waitFor();
}
String outputJson = strBuild.toString().trim();

它对我来说很好,我很高兴与互联网上的每个人分享这个:) 我从here得到了解决方案。

【讨论】:

    猜你喜欢
    • 2011-08-01
    • 2011-03-21
    • 2013-12-28
    • 1970-01-01
    • 2013-12-28
    • 2011-12-07
    • 1970-01-01
    • 2012-05-01
    • 1970-01-01
    相关资源
    最近更新 更多