I'd like to create a small shell script that will tell me the duration of a video whose file name is passed to it as a parameter. It'd be something like this:

$ script_name "file name with spaces.webm" # or any other media file extension

The script file would resemble

SECRET_SAUCE_USING_$1 | mediainfo | grep Duration | sort -u
# maybe SECRET_SAUCE_USING_$1 | xargs -0 mediainfo . . .

On the command line, this is what works and what I want the script to do:

$ mediainfo file\ name\ with\ spaces.webm | grep Duration | sort -u

I've tried using things like

echo "$1" | sed 's/ /\\ /g' # piped to mediainfo . . .

to change the spaces in the file name to '\ ' character pairs (without the quotes). The echo ... | sed ... part works but mediainfo barfs


echo "$1" | tr ' ' '\000' | xargs -0 mediainfo . . .

to change the spaces to nulls and get xargs to invoke mediainfo with nulls in the file name instead of spaces.

I haven't found a solution so far. (I actually use zsh, not bash.) All help greatly appreciated. Tnx.

  • mediainfo '--Inform=Video;%Duration/String4%\n' "$1" – Alex Nov 27 '18 at 10:38

If you want the script's parameter ($1) to end up as mediainfo's parameter, then just put it there:

# ...
mediainfo "$1" | grep Duration | sort -u
# ...

That works for a single parameter to the script. More flexibly, you could loop over all of the script's parameters and feed them one at a time to mediainfo:

for file in "$@"
  mediainfo "$file" # ...
  • Thanks for your answer. That was the very first thing I tried and it didn't work when I originally tried it. Now it does. I must have been doing something wrong before. How do I mark this question as solved? – Don Nadie Jan 30 '18 at 12:44
  • Happy to hear! Looks like you found the check mark, so you’re all set! – Jeff Schaller Jan 30 '18 at 13:06

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.