I am currently using ffmpeg to slice video files. I automated the process through a script called ffmpeg_split.sh. To view the script click here. When I run the script I have an error when trying to get the parse_duration_info(). In other words, the issue is trying to set the time length of the video slices. The error thrown is Bad input to parse_duration_info()

Here is the syntax to slice a video with script: ffmpeg_split.sh -s test_vid.mp4 -o video-part%03d.mp4 -c 00:00:08

-s  Path to video source (string)

-o  Path to output file with format (string)
    Format must be printf-like, for example ./path/video-part-%03d.avi

-c  Chunk length in seconds (integer)

Issue is below: Here's the full script

function parse_duration_info() {
    if [[ $1 ]] && [[ $2 -gt 0 ]] && [[ $2 -lt 5 ]] ; then
        echo "$1" | sed "s/$__DURATION_PATTERN/\\$__OFFSET/"
        echo "Bad input to parse_duration_info()"
        echo "Givven duration $1"
        echo "Givven offset $2"
        echo "Exiting..."
        exit 1

You'll need to dissect this script a bit to see why it's failing. This isn't that hard a task, since you have the source code to it already. I'd start with confirming that the commands in the function implementation, get_video_duration_in_seconds_from_file() is correct. Specifically when I ran this command on a few .mp4 files I had, it would return nothing:

__DURATION_HMS=$(ffmpeg -i "$__FILE" 2>&1 | grep Duration | \
    grep '\d\d:\d\d:\d\d.\d\d' -o)

NOTE: This is line #54.

So without this value, this would be a good reason why the calls that come after it to the function parse_duration_info() are returning the error message.

According to the comments in the original script, there should be 2 arguments to parse_duration_info().

# arg1: duration in format 01:23:45.67
# arg2: offset for group 1 gives hours, 2 gives minutes, 
#       3 gives seconds, 4 gives milliseconds

More debugging output

Additionally you can enable more verbose output of this script by add this line just after the shebang (#!/bin/bash).

set +x

If enabling full verbosity on the entire script to too much you can selectively enable it around certain blocks of code like so:

parse_duration_info() {
    set +x

    ... body of function ...

    set -x
  • Ok i am new to scripting but I am having difficulties figuring it out: What do you mean it is missing value __DURATION_HMS? – Code_Ed_Student Oct 3 '14 at 6:51
  • I tried doing this instead for the duration ffmpeg -i "$__FILE" 2>&1 | grep Duration | awk '{print $2}' | tr -d , – Code_Ed_Student Oct 3 '14 at 6:57
  • Running this code returns nothing: ffmpeg -i "$__FILE" 2>&1 | grep Duration | grep '\d\d:\d\d:\d\d.\d\d' -o. Change "$__FILE" to "my.mp4" and run it in the shell. – slm Oct 3 '14 at 7:02
  • $__File to test_vid.mp4 worked. So that means the value of that variable is not being set to the path of the input video? – Code_Ed_Student Oct 3 '14 at 7:14
  • @Code_Ed_Student - you have to do more debugging, that just tells you that that line of code is working OK. I'd echo out $1 and $2 to the parse function to confirm that they have values being properly passed in. – slm Oct 3 '14 at 7:25

Try this:

__DURATION_HMS=$(ffmpeg -i "${FILE}" 2>&1 | grep Duration | \ grep -e '[0-9]\{2\}:[0-9]\{2\}:[0-9]\{2\}' -o)

  • A bit of explanation would be nice. – vonbrand Jan 9 '16 at 15:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.