繁体   English   中英

读取从java Runtime.exec(…)运行的ffmpeg进程的输出信息

[英]Read Outputinformation of ffmpeg-process runned from java Runtime.exec(…)

我想读出有关加载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

我希望持续时间中包含的信息。

所以现在我的问题:我通过运行ffmpeg

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

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

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

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

我得到了解决方案:使用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();

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

我使用FFProbe做到了这一点,我确信类似的代码也可以用于FFMPEG。 您可以打印出Json并解析它以获得持续时间。

    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并获取持续时间等信息。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM