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

emp
  • 602
  • 3
  • 11
  • 22
  • Does it work for you if you put it in a script outside of python? – kevinkayaks Sep 06 '18 at 06:39
  • Yeah it work if I don't use a function – emp Sep 06 '18 at 06:43
  • `c3 = "ffmpeg -i dm.mov -ac 1 -f flac -vn testdm.flac" subprocess.call(c3, shell=True)` like this – emp Sep 06 '18 at 06:44
  • are you inputting strings into the arguments of the function? Do you think you therefore have extra quotes around the arguments in the script? just a guess. I think your arguments may not be properly formatted going into the script – kevinkayaks Sep 06 '18 at 06:45
  • You should try using format on your command variable, to change value for video and output to: dm.MOV and dm-new.flac. – boandriy Sep 06 '18 at 06:46
  • I tried to delete the extra quote around the argument but it still the same – emp Sep 06 '18 at 06:46
  • 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 – kevinkayaks Sep 06 '18 at 06:47
  • @boandriy I know that it work on that way. But my problem here is that if I just want to be able to write an argument like video, output in the ffpeg script – emp Sep 06 '18 at 06:48
  • 1
    @kevinkayaks `command = 'ffmpeg -i {video} -ac 1 -f flac -vn {output}' ` This is what I meant but it didn't work – emp Sep 06 '18 at 06:54
  • All of the answers below are worked. Thank you guy so much – emp Sep 06 '18 at 07:01

4 Answers4

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.

tda
  • 2,045
  • 1
  • 17
  • 42
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')
Tamil Selvan S
  • 562
  • 8
  • 25
U13-Forward
  • 69,221
  • 14
  • 89
  • 114
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')
Tamil Selvan S
  • 562
  • 8
  • 25
kevinkayaks
  • 2,636
  • 1
  • 14
  • 30
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')
boandriy
  • 527
  • 6
  • 19