简体   繁体   中英

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"
    try:
        os.remove(output)
    except Exception:
        pass
    p=Popen(["ffmpeg", "-i", file, "-c:a copy -ss", start_time, "-to", end_time, output], stdout=PIPE)
    p.communicate()
    os.remove(file)
    os.rename(output,file)
    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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM