Emre Erol
Emre Erol

Reputation: 460

FFMpeg giving invalid argument error with python subprocess

I am trying to convert a file or microphone stream to 22050 sample rate and change tempo to double. I can do it using terminal with below code;

#ffmpeg -i test.mp3 -af asetrate=44100*0.5,aresample=44100,atempo=2 output.mp3 

But i can not run this terminal code with python subprocess. I try many things but every time fail. Generaly i am taking Requested output format 'asetrate' or 'aresample' or 'atempo' is not suitable output format errors. Invalid argument. How can i run it and take a stream with pipe?

song = subprocess.Popen(["ffmpeg.exe", "-i", sys.argv[1], "-f", "asetrate", "22050", "wav", "pipe:1"],
                        stdout=subprocess.PIPE)

Upvotes: 0

Views: 1911

Answers (2)

llogan
llogan

Reputation: 134113

Your two commands are different. Try:

song = subprocess.Popen(["ffmpeg", "-i", sys.argv[1], "-af", "asetrate=22050,aresample=44100,atempo=2", "-f", "wav", "pipe:1"],
  • -af is for audio filter.
  • -f is to manually set muxer/output format

Upvotes: 2

metatoaster
metatoaster

Reputation: 18938

ffmpeg interprets whatever supplied by -af as a single argument that it would then parse internally into separate ones, so splitting them out before passing it via Popen would not achieve the same thing.

The initial example using the terminal should be created using Popen as

subprocess.Popen([
    'ffmpeg', '-i', 'test.mp3', '-af', 'asetrate=44100*0.5,aresample=44100,atempo=2',
    'output.mp3',
])

So for your actual example with pipe, try instead the following:

song = subprocess.Popen(
    ["ffmpeg.exe", "-i", sys.argv[1], "-f", "asetrate=22050,wav", "pipe:1"],
    stdout=subprocess.PIPE
)

You will then need to call song.communicate() to get the output produced by ffmpeg.exe.

Upvotes: 2

Related Questions