3

I'm attempting to create a looped video file by calling ffmpeg from the python subprocess library. Here's the part that's giving me problems:

import subprocess as sp
sp.Popen(['ffmpeg', '-f', 'concat', '-i', "<(for f in ~/Desktop/*.mp4; do echo \"file \'$f\'\"; done)", "-c", "copy", "~/Desktop/sample3.mp4"])

With the above code I'm getting the following error:

<(for f in /home/delta/Desktop/*.mp4; do echo "file '$f'"; done): No such file or directory

I did find a similarly phrased question here. But I'm not sure how the solution might apply to solving my issue.

Edit: Thanks for the help everyone! Following the advice in the comments and looking elsewhere I ended up changing the code to this:

sp.Popen("ffmpeg -f concat -i <(for f in ~/Desktop/*.mp4; do echo \"file \'$f\'\"; done) -c copy ~/Desktop/sample3.mp4", shell=True, executable="/bin/bash")

--which works fine.

Community
  • 1
  • 1
moorej
  • 507
  • 3
  • 17
  • 3
    You are running `ffmpeg`, but redirection is a feature of a shell. Use `shell=True` parameter to force `Popen` to pass it through the shell. Also consider not passing an array of command and parameters, but a single string. Read more [here](https://docs.python.org/2/library/subprocess.html#popen-constructor) – Amadan Oct 08 '14 at 02:10
  • 3
    If you use `shell=True`, the parameters need to passed as a string, not a list. – dano Oct 08 '14 at 02:11
  • 4
    +1 to what Amadan and dano said. You will likely also need to explicitly indicate that you want `/bin/bash` instead of the default `/bin/sh` for that `Popen`. – Etan Reisner Oct 08 '14 at 02:12
  • 1
    See the discussion on http://stackoverflow.com/a/24560058/258523 for why a single string argument to `Popen` with `shell=True`. – Etan Reisner Oct 08 '14 at 02:17

1 Answers1

3

Following the advice in the comments and looking elsewhere I ended up changing the code to this:

sp.Popen("ffmpeg -f concat -i <(for f in ~/Desktop/*.mp4; do echo \"file \'$f\'\"; done) -c copy ~/Desktop/sample3.mp4", shell=True, executable="/bin/bash")

--which works fine. – moorej

Armali
  • 16,557
  • 13
  • 53
  • 152
  • A short explanation why `bin/bash` helps to process substitution ` – Fibo Kowalsky Aug 01 '18 at 12:30
  • 2
    @Fibo Kowalsky - It's simply because [process substitution](https://www.gnu.org/software/bash/manual/html_node/Process-Substitution.html) is a special feature of `bash`, which other shells don't have. – Armali Aug 01 '18 at 13:13