0

Running

/usr/bin/mediainfo --Inform='Video;%Duration%' /home/daniel/upload/videos/4/f/6/e/f/4f6ef2e0d67c4.flv

from terminal gives me this output

903520

And running this in java

        Process p1;
    try {
        p1 = Runtime.getRuntime().exec("/usr/bin/mediainfo --Inform='Video;%Duration%' /home/daniel/upload/videos/4/f/6/e/f/4f6ef2e0d67c4.flv");

        BufferedReader input1 = new BufferedReader(new InputStreamReader(p1.getInputStream()));
        String line1;
        while ((line1 = input1.readLine()) != null) {
            System.out.println("-"+line1);
        }
        input1.close();         

        p1.waitFor();               


    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

Results in

-General
-Complete name : /home/daniel/upload/videos/4/f/6/e/f/4f6ef2e0d67c4.flv
-Format                                   : Flash Video
-File size                                : 62.0 MiB
-Duration                                 : 15mn 3s
-Overall bit rate                         : 576 Kbps
-Tagging application       : Yet Another Metadata Injector for FLV - Version 1.4
-
-Video
-Format                                   : AVC
-Format/Info                              : Advanced Video Codec
-Format profile                           : [email protected]
-Format settings, CABAC                   : Yes
-Format settings, ReFrames                : 4 frames
-Codec ID                                 : 7
-Duration                                 : 15mn 3s
-Bit rate                                 : 512 Kbps  
(much more here) ... 

How do I get my desired output (903520) from Runtime.getRuntime().exec(cmd) ?

edit: fixed formatting

8
  • 2
    Hint - check your question in the preview window before posting. That formatting doesn't look healthy to me... Commented May 20, 2013 at 14:34
  • Without knowing much about what /usr/bin/mediainfo does, is it clear to you how the arguments must have been interpreted to cause the second set of output? This seems especially weird if it would usually simply output a six-digit number. Commented May 20, 2013 at 14:38
  • If the output of one command differs from the output of another, then either (a) the command lines are different, or (b) the environment (current working directory, environment variables, etc.) are different. (Or (a) and (b) both, obviously.) Can you put an echo in front of the Runtime.getRuntime().exec() command line and share the captured results? In that way, we can determine if the two command lines really are the same, as you think they are. If they are the same, we'll check the environment next. I suspect that the issue is that %Duration% is not being expanded as it would in a "n Commented May 20, 2013 at 14:39
  • An echo? How do I do this? Commented May 20, 2013 at 14:45
  • The output from mediainfo with that set of arguments should return the length of the movie in nano seconds. So a six-digit number is what I expect for a clip ~15mins Commented May 20, 2013 at 14:47

2 Answers 2

4

The commandline shell does some magic for you which Runtime.exec() does NOT for you.

In this case I guess, that the shell interprets (and omits) the ' marks in your commandline.

So please try this version, where the ' have been removed and the commandline has been splitted into parts by hand (another common issue):

String[] args = new String[]{
    "/usr/bin/mediainfo",
    "--Inform=Video;%Duration%",
    "/home/daniel/upload/videos/4/f/6/e/f/4f6ef2e0d67c4.flv"
};
Runtime.getRuntime().exec(args);
Sign up to request clarification or add additional context in comments.

1 Comment

arrghh One Wasted hour. Thanks a lot!
1

Note that there is a Runtime.exec(String) and a Runtime.exec(String[]).
Have you tried the second method, just to make sure the string command is being interpreted the way it should?

From the docs:

public Process exec(String command)
             throws IOException

Executes the specified string command in a separate process. (...)


public Process exec(String[] cmdarray)
             throws IOException

Executes the specified command and arguments in a separate process. (...)


You may try:

String[] myArgs = new String[]{
    "/usr/bin/mediainfo",
    "--Inform='Video;%Duration%'",
    "/home/daniel/upload/videos/4/f/6/e/f/4f6ef2e0d67c4.flv"
};

Process p1;
try {
    p1 = Runtime.getRuntime().exec(myArgs);
        ...
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.