1

I want to get video duration with help of ffmpeg:

String command = "ffmpeg -i /home/user/Videos/my-video.mp4 2>&1 | grep Duration | awk '{print $2}' | tr -d ," Runtime.getRuntime().exec(cmdarray); 

But i always get java.io.IOException: Cannot run program "ffmpeg -i /home/user/Videos/my-video.mp4 2>&1 | grep Duration | awk '{print $2}' | tr -d ,": error=2, No such file or directory

If I run this command from terminal - all is ok

2 Answers 2

2

You've got several issues here. Firstly as @joy points out there could be a problem with the Path used by Java to locate the command so Java may not be finding a command called "ffmpeg". Fixing the Path used for launching your VM should resolve that, or just insert the fully qualified pathname to "ffmpeg".

Secondly: you are trying to run a terminal / shell command. The "|" pipes are normally interpretted correctly by a terminal / shell which breaks the chain into sub-processes linking stdout/stdin. But Java is being asked to run "ffmpeg" passing in some arguments containing "|" which would not be handled as you wish by "ffmpeg".

Check the shell you use:

echo $SHELL 

Let's say that printed /bin/bash - you can fix by getting Java to launch the shell and make that interpret the pipe command:

String[] command = new String[] { "/bin/bash", "-c", "ffmpeg -i /home/user/Videos/my-video.mp4 2>&1 | grep Duration | awk '{print $2}' | tr -d ," }; Runtime.getRuntime().exec(cmdarray); 
Sign up to request clarification or add additional context in comments.

Comments

0

Most likely the path isn't the same when you run from terminal vs when you run from Java. 1. you can try using the full path of ffmpeg (run "which ffmpeg" in terminal). 2. perhaps ffmpeg is an alias in your .profile file in that case you can try to source(load) your .profile file first before executing the command in Java.

3 Comments

When i run "ffmpeg -i <file> <outputFile>" from java all is ok too
At the same time "ffmpeg -i <file>" throws exception too. I don't understand what's wrong
Same error even if I run in loop the command that runs successfully outside of loop

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.