Sebastian Röher
Sebastian Röher

Reputation: 417

Read Outputinformation of ffmpeg-process runned from java Runtime.exec(...)

I want to read out the information about a videofile loaded with ffmpeg. The output in the console from 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

The Information wich I want to have in Duration.

So now my Problem: I run ffmpeg via

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

and i need to read out the console output (which is not printet out in console) into a Sting.

I tried to read from the outputStream but this doesn't seem to work eather the inputStream doesn't work.

Can anyone give me hint how I can read out the console-output of a process runned via java Runtime.getRuntime().exec(command);?

Upvotes: 1

Views: 4587

Answers (2)

Sebastian Röher
Sebastian Röher

Reputation: 417

I got the solution: Use ProcesssBuilder and 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();

it works fine for me and I'm glad to share this with everyone in the Internet :) I got the solution from here.

Upvotes: 5

Sandeep Kaul
Sandeep Kaul

Reputation: 3277

I did this with FFProbe, I am sure similar code should work with FFMPEG. You could print out the Json and parse it to get the 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;
    }

The you could parse the outputJson and get the duration and other things.

Upvotes: 2

Related Questions