How to use ffmpeg in a python function

ffmpeg-python documentation
python ffmpeg streaming
ffmpeg python extract audio
ffmpeg python convert audio
ffmpeg-python concat
python ffmpeg compress video
python ffmpeg create video from images
ffmpeg-python copy

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

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.

ffmpeg-python: Python bindings for FFmpeg, Build command-line for invoking ffmpeg. The run() function uses this to build the commnad line arguments and should work in most cases, but calling this function​  A python interface for FFmpeg. Download files. Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

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')

How to Use FFmpeg on Python? - Yanwei Liu, In the latest newsletter I find this project ffmpeg-python on… You can refer to its exampleor create your own scriptFor example: change the  Then start to import the ffmpeg module in your Python interactive shell with the help of the following command. import ffmpeg Then use the ffmpeg.convert_video function like shown below. ffmpeg.convert_video(‘test.mp4’, ‘test.avi’) The above Python command converts the video test.mp4 to test.avi.

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')

kkroening/ffmpeg-python: Python bindings for FFmpeg , Python bindings for FFmpeg - with complex filtering support #334 Write multiple streams using pipes Opened by faroit 3 months ago #251 FileNotFoundError:  I'm trying to use ffmpeg with Python's subprocess module to convert some audio files. I grab the audio files from a URL and would like to just be able to pass the Python File Objects to ffmpeg, instead of first saving them to disk. It would also be very nice if I could just get back a file stream instead of having ffmpeg save the output to a file.

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')

ffmpeg-progress · PyPI, Get progress information for an ffmpeg process. pip install ffmpeg-progress An ffprobe front-end function is included. Usage: from ffmpeg_progress import Developed and maintained by the Python community, for the Python community. Once you now know how to run your ffmpeg commands (and that it works), all you have to do is call the same set of commands from your app. Note that, you don't need to change directories (as you did with your initial code) or specify the path to ffmpeg. Step 3: Calling ffmpeg from Python app. Using Python's subprocess to call the same commands:

ffmpeg-python · PyPI, FFmpeg Python wrapper with support for complex filtering. ffmpeg-python 0.1.1​. pip install ffmpeg-python==0.1.1. Copy PIP instructions. FFmpeg will also join the multiple video parts and create a single video file. Create join.txt file that contains the exact paths of the files that you want to join. All files should be same format (same codec). The path name of all files should be mentioned one by one like below.

Run ffmpeg from Python Script, Do you have to have that video in the same folder as you python script and ffmpeg.exe? Read Duration: 2:49 Posted: Nov 12, 2015 Filename, size ffmpeg-1.4.tar.gz (5.1 kB) File type Source. Python version None. Upload date October 8, 2018. Hashes View hashes. Hashes for ffmpeg-1.4.tar.gz. Hashes

Hacking FFmpeg With Python - Part One, Through this tutorial you are going to learn how to use the python The following command can be used to run FFmpeg from the command line: make use of the communicate method like we did in the previous examples. Be aware that video and audio encoding can take a very long time to run. You should be prepared to settle in for a while when you use FFmpeg. Basic conversion. The thing that trips up most people when it comes to converting audio and video is selecting the correct formats and containers. Luckily, FFmpeg is pretty clever with its default settings.

Comments
  • Does it work for you if you put it in a script outside of python?
  • Yeah it work if I don't use a function
  • c3 = "ffmpeg -i dm.mov -ac 1 -f flac -vn testdm.flac" subprocess.call(c3, shell=True) like this
  • 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 command = 'ffmpeg -i {video} -ac 1 -f flac -vn {output}' This is what I meant but it didn't work
  • 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!