0

Can u please help me with bash script for ffmpeg? Trying to create condition based on args to play OR save media to file, so I've created this kind of IF:

#!/bin/bash

if [ "$1" == "play" ]; then
    POSTFIX="-f matroska - | mpv -"
else
    POSTFIX="-y $OUTPUT"
fi

ffmpeg \
  # skipped personal ffmpeg stuff
  "$POSTFIX"

Now when I try to run it with "play" argument it says:

Unrecognized option 'f matroska - | mpv -'.

So if I add this POSTFIX both variants to command everything works fine...

It looks like something in my bash screens this args or something like that? Also I don't see in error this dash symbol before f option

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
Pavel
  • 131
  • 2
  • 13
  • 2
    First, see [BashFAQ #50](https://mywiki.wooledge.org/BashFAQ/050). Second, `| mpv -` is not an argument at all. – Charles Duffy Dec 19 '22 at 23:21
  • 1
    Third (though not as relevant as long as your code really is running only with bash and not sh), `==` is not a standard string comparison operator in `[` -- the only standardized one just a single `=`. – Charles Duffy Dec 19 '22 at 23:22

1 Answers1

2

Shell operators are not processed after expanding variables, you would need to use eval "ffmpeg $POSTFIX" to do this. But getting all the quoting right for eval() will be difficult.

Instead, you can use the exec command and redirect its output to the mpv - command.

It's also best to put dynamic arguments into an array rather than a string; see Setting an argument with bash

#!/bin/bash

if [ "$1" == "play" ]; then
    POSTFIX=(-f matroska -)
    exec > >(mpv -)
else
    POSTFIX=(-y "$OUTPUT")
fi

ffmpeg "${POSTFIX[@]}"
Barmar
  • 741,623
  • 53
  • 500
  • 612