2

I have tried to use a ffmpeg to extract an audio from a video file and this is my code

import io
import os
import subprocess

def extract_audio(video,output):
    command = "ffmpeg -i '{video}' -ac 1  -f flac -vn '{output}'"
    subprocess.call(command,shell=True)

extract_audio('dm.MOV','dm-new.flac')

And I got no error after compiled. By doing this I should get a new file which is 'dm-new.flac'. But there is no such a flac file created after I compile the script. I think there are something wrong with the syntax or something in the variable 'command' which I have no idea to fix this. My question here is how can I use ffmpeg in a python function base on this code?

By the way, I knew that I could just use ffmpeg without writing a function. But I really need to write in in a function. Thank you

10
  • Does it work for you if you put it in a script outside of python? Commented Sep 6, 2018 at 6:39
  • Yeah it work if I don't use a function Commented Sep 6, 2018 at 6:43
  • c3 = "ffmpeg -i dm.mov -ac 1 -f flac -vn testdm.flac" subprocess.call(c3, shell=True) like this Commented Sep 6, 2018 at 6:44
  • 1
    even with this I still think you need no quote at all in the script string, and I suspect you're still getting one quote, but again this is just a guess Commented Sep 6, 2018 at 6:47
  • 1
    @kevinkayaks command = 'ffmpeg -i {video} -ac 1 -f flac -vn {output}' This is what I meant but it didn't work Commented Sep 6, 2018 at 6:54

4 Answers 4

7

Try this:

import io
import os
import subprocess

def extract_audio(video,output):
    command = "ffmpeg -i {video} -ac 1  -f flac -vn {output}".format(video=video, output=output)
    subprocess.call(command,shell=True)

extract_audio('dm.MOV','dm-new.flac')

I think you were trying to reference two variables inside a string but did not tell Python that you should replace 'video' and 'output' with their corresponding variables. .format() allows you to reference the variables that you refer to in a string.

See here for more info.

Sign up to request clarification or add additional context in comments.

Comments

3

Add one character (f) to solve it (over python 3.6):

import subprocess
def extract_audio(video,output):
    command = f"ffmpeg -i '{video}' -ac 1  -f flac -vn '{output}'"
    subprocess.call(command,shell=True)

extract_audio('dm.MOV','dm-new.flac')

Comments

1

I belive this should work:

import io
import os
import subprocess

def extract_audio(video,output):
    command = "ffmpeg -i {} -ac 1  -f flac -vn {}".format(video, output)
    subprocess.call(command,shell=True)

extract_audio('dm.MOV','dm-new.flac')

Comments

1

I think this is it.

import io
import subprocess
def extract_audio(video,output):
    command = "ffmpeg -i {} -ac 1  -f flac -vn {}".format(video,output)
    subprocess.call(command,shell=True)

extract_audio('dm.MOV','dm-new.flac')

4 Comments

Interesting... accepted answer but written two minutes after mine... win some, lose some!
it's different!
Works fine for me - have you tested?
oh, I thought you had extra quotes. alright, kudos!

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.