1

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();
        }
J Jain
  • 13
  • 4
  • The output of `ffmpeg` is not meant to be machine parsed. That's what `ffprobe` is for, and you'll also be able to eliminate `grep` and `tr`. See the [second answer](https://stackoverflow.com/a/22243834/1109017) in your linked question. – llogan Mar 04 '19 at 05:33
  • I have also tried the [ffprobe] command and it is giving same error. In command prompt it runs well and in above java program, it gives exitcode : 1 I am not getting the actual error. – J Jain Mar 04 '19 at 07:00
  • You should update your code. – llogan Mar 04 '19 at 07:01

0 Answers0