0

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)

2 Answers 2

2

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
Sign up to request clarification or add additional context in comments.

1 Comment

Ok it is working. Thanks a lot.. May i ask you a new question? How can i give a live stream microphone input these code? Accualy i want to change sys.argv[1] parameter with pyaudio callback sound data. Can you give an example or tip about this ? @llogan
2

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.

2 Comments

Ok it is working. Thanks a lot.. May i ask you a new question? How can i give a live stream microphone input these code? Accualy i want to change sys.argv[1] parameter with pyaudio callback sound data. Can you give an example or tip about this ?
Probably check to see if pyaudio can output to stdout and then pipe it into ffmpeg.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.