SMH SMH - 6 days ago 6
Python Question

invalid syntax while concat mpeg files on windows PYTHON

I am tying to concatenate all mpeg files together in one new file in windows 7, I adjusted the environment variables and running the code from python shell but it gives invalid syntax. Any help as I am new to Python and ffmpeg library?

My code:

ffmpeg -f concat -i <(for f in glob.glob("*.mpeg"); do echo "file '$PWD/$f'"; done) -c copy output.mpeg


Thanks

Answer

Your example code is mix or Python code and Bash code so it can't run in Python Shell nor in Bash Shell :)

On Linux it works in Bash as two commands:
(Windows probably doesn't have printf command)

printf "file '%s'\n" *.wav > input.txt 

ffmpeg -f concat -i input.txt -c copy output.mpeg

Python version which doesn't need Bash:

#!/usr/bin/env python3

import os
import sys
import glob
import subprocess

# get Current Working Directory (CWD)
pwd = os.getcwd()

# get list of files 
if len(sys.argv) > 1:
    #filenames = sys.argv[1:] # Linux
    filenames = glob.glob(sys.argv[1]) # Windows
else:
    filenames = glob.glob("*.mpg")

#print(filenames)

# generate "input.txt" file
with open("input.txt", "w") as f:
    for name in filenames:
        f.write("file '{}/{}'\n".format(pwd, name))
        #f.write("file '{}'\n".format(name))

# run ffmpeg 
subprocess.run('ffmpeg -f concat -i input.txt -c copy output.mpeg', shell=True)

And you can run it with or without argument ie. "*.wav"

python script.py *.wav

(tested only on Linux)


printf (and other Bash commands) for Windows: GnuWin32

More on GnuWin32

Comments