I want to get duration of video and save it to database. I am running a java program to convert video into mp4 format, after conversion I want to get the duration of video.
[How to extract duration time from ffmpeg output?
I have gone threw this link, that command is running well in cmd, but it is giving 'exit code = 1' with java program.
Here is code :-
String videoDuration = ""; List args1 = new ArrayList();
args1.add("ffmpeg");
args1.add("-i");
args1.add(videoFilePath.getAbsolutePath());
args1.add("2>&1");
args1.add("|");
args1.add("grep");
args1.add("Duration");
args1.add("|");
// args.add("awk");
// args.add("'" + "{print $2}" + "'");
// args.add("|");
args1.add("tr");
args1.add("-d");
args1.add(",");
String argsString = String.join(" ", args1);
try
{
Process process1 = Runtime.getRuntime().exec(argsString);
logger.debug(strMethod + " Process started and in wait mode");
process1.waitFor();
logger.debug(strMethod + " Process completed wait mode");
// FIXME: Add a logger increment to get the progress to 100%.
int exitCode = process1.exitValue();
if (exitCode != 0)
{
throw new RuntimeException("FFmpeg exec failed - exit code:" + exitCode);
}
else
{
videoDuration = process1.getOutputStream().toString();
}
}
catch (IOException e)
{
e.printStackTrace();
new RuntimeException("Unable to start FFmpeg executable.");
}
catch (InterruptedException e)
{
e.printStackTrace();
new RuntimeException("FFmpeg run interrupted.");
}
catch (Exception ex)
{
ex.printStackTrace();
}
return videoDuration;
Updated Code:-
List<String> args1 = new ArrayList<String>();
args1.add("ffprobe");
args1.add("-i");
args1.add(videoFilePath.getAbsolutePath());
args1.add("-show_entries");
args1.add("format=duration");
args1.add("-v");
args1.add("quiet");
args1.add("-of");
args1.add("csv=\"p=0\"");
args1.add("-sexagesimal");
String argsString = String.join(" ", args1);
try
{
Process process1 = Runtime.getRuntime().exec(argsString);
}
catch (InterruptedException e)
{
e.printStackTrace();
}