Stijn Goethals Stijn Goethals - 4 months ago 45
Python Question

Using ffmpeg for cutting mp3 in python -codec copy error

def cut_file(file, start_time, end_time):
""" Cut the mp3 file with start and end time. """
output = file[:-4] + "_cut.mp3"
except Exception:
p=Popen(["ffmpeg", "-i", file, "-c:a copy -ss", start_time, "-to", end_time, output], stdout=PIPE)
return file

When using this function for cutting a mp3 file I get error from ffmpeg. The error is:

Unknown encoder '0:07'

Why doesn't ffmpeg recognize the copy command when using Python? Running the command in the shell doesn't give me any errors.

I have tried to change the order of the arguments but this give me the same sort of errors.

I got the code out of the official documentation.


Since you're passing all the arguments list-style (which is good practice), you need to split all the arguments space-wise, otherwise Popen will quote-protect the ones containing spaces to respect what you passed.

This argument "-c:a copy -ss" is interpreted as one argument, which probably explains why ffmpeg is tring to read your start time as an encoder.

What's really issued to the system call is:

ffmpeg -i file "-c:a copy -ss" start_time -to end_time output

Do that instead:

p=Popen(["ffmpeg", "-i", file, "-c:a","copy","-ss", start_time, "-to", end_time, output], stdout=PIPE)