How to Use Ffmpeg in a Python Function

How to use ffmpeg in a python function

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.

How do I use this FFMPEG command in python script?

You could you os.system or you could use the ffmpeg library, but since it is only one command I would just use the os.system method

# importing os module  
import os

input_file = 'something.mp4' # You should enter the file's path here
# Using fstring for variable file name
# Command to execute
# Using Windows OS command
cmd = f'ffmpeg -i {input_file} -r 1 %04d.jpg'
# Using os.system() method
os.system(cmd)

Can I run ffmpeg from my Python code and return a signal when it's done compressing?

Subprocess.call() returns the exit code of the child-process. Most programs return 0 on success and non-zero on failure, which is also the case for ffmpeg.

import subprocess

return_value = subprocess.call([
'ffmpeg',
'-loop', '1',
'-i', video_file,
'-i', sound_file,
'-c:v', 'libx264',
'-tune', 'stillimage',
'-c:a', 'aac',
'-strict', 'experimental',
'-b:a', '192k',
'-pix_fmt', 'yuv420p',
'-shortest', output_name,
])

if return_value:
print("Failure")
else:
print("Sucess!")

As you can see, subprocess.call() expects its arguments as a list of strings, unless you specify the shell=True argument which is widely considered a security risk and bad practice.

Use the likes of str.split() in the Python interpreter to convert a string to the desired format and then remove the quotes for any variables like video_file and output_name.

Upload ffmpeg to Azure Function Environment PATH [Python]

I think the reason should be I did not add sudo, but I dont know the password of Azure Function Linux platform.

Users can run commands with elevated privileges on a Linux virtual machine using the sudo command. However, the experience may vary depending on how the system was provisioned.

1.SSH key and password or password only - the virtual machine was provisioned with either a certificate (.CER file) as well as a password, or just a user name and password. In this case sudo will prompt for the user's password before executing the command.

2.SSH key only - the virtual machine was provisioned with a certificate (.cer or .pem file), but no password. In this case sudo will not prompt for the user's password before executing the command.

You can refer the following link : http://azure.microsoft.com/en-us/documentation/articles/virtual-machines-linux-use-root-privileges/


I followed the official tutorial Create your first Python function in Azure to create a HttpTrigger Function for Python, and tried different ways to make ffmpeg works in Azure Functions, it works for me.

These are my steps:

  1. Install Azure Functions Core Tools on my local Windows machine to create a project named MyFunctionProj and a function named HttpTrigger.

  2. Before uploading ffmpeg with deployment, checked the OS platform architecture of my instance of Azure Functions on Azure via change the official sample code with the code below.

    # add these codes
    import platform, os
    .....

    def main(req: func.HttpRequest) -> func.HttpResponse:
    if name:
    return func.HttpResponse(f"Hello {name}! {platform.architecture()}")

    Its result is Hello krishna! ('64bit', '') in browser.

  3. Then I put the ffmpeg AMD64 static binary file downloaded from https://johnvansickle.com/ffmpeg/ into my MyFunctionProj and change my code below to check the file path, and to command func azure functionapp publish MyFunctionProj to publish to Azure.

    def main(req: func.HttpRequest) -> func.HttpResponse:
    if name:
    return func.HttpResponse(f"Hello {name}! {platform.architecture()} {os.listdir()} {os.listdir('HttpTrigger')}")

Output:

Hello krishna! ('64bit', '') ['in.mp4', 'ffmpeg', 'host.json', 'requirements.txt', 'ffmpeg.exe', '.python_packages', 'HttpTrigger'] ['in.mp4', '__pycache__', 'sample.dat', 'host.json', 'function.json', '__init__.py'] in browser as same as these in my MyFunctionProj


  1. I found everything in MyFunctionProj folder will be uploaded to Azure and call os.listdir() to show the file list of MyFunctionProj, so the current path in Python is the same as MyFunctionProj locally. Then I tried to invoke ffmpeg in my local Windows environment via the code below.

    def main(req: func.HttpRequest) -> func.HttpResponse:
    if name:
    return func.HttpResponse(f"Hello {name}! {platform.architecture()} {os.listdir()} {os.listdir('HttpTrigger')} {os.path.exists('in.mp4')} {os.popen('ffmpeg.exe -i in.mp4 out.mp4').read()} {os.path.exists('out.mp4')} {os.popen('del out.mp4').read()}")

    It works to output the file out.mp4 via command ffmpeg.exe -i in.mp4 out.mp4, then considering for reproduce it to command del out.mp4.

  2. Try to make it works for Linux environment on Azure Function, I change the commands with ./ffmpeg -i in.mp4 out.mp4 and rm out.mp4. But it didn't work on Azure Function. It may be caused by missing the execute permission of ffmpeg Linux binary file while uploading from Windows. So I checked via command ls -l ffmpeg and chmod u+x ffmpeg before invoke it.

    def main(req: func.HttpRequest) -> func.HttpResponse:
    if name:
    return func.HttpResponse(f"Hello {name}! {platform.architecture()} {os.listdir()} {os.listdir('HttpTrigger')} {os.popen('ls -l ffmpeg').read()} {os.popen('chmod u+x ffmpeg').read()} {os.popen('ls -l ffmpeg').read()} {os.path.exists('in.mp4')} {os.popen('./ffmpeg -i in.mp4 out.mp4').read()} {os.path.exists('out.mp4')} {os.popen('rm out.mp4').read()}")

    It works now, the result is like below I formatted it pretty.

    Hello krishna! // Official sample output
    ('64bit', '') // the Linux platform architecture of Azure Functions for Python
    ['in.mp4', 'ffmpeg', 'host.json', 'requirements.txt', 'ffmpeg.exe', '.python_packages', 'HttpTrigger'] // file list of MyFunctionProj
    ['in.mp4', '__pycache__', 'sample.dat', 'host.json', 'function.json', '__init__.py'] // file list of HttpTrigger
    -r--r--r-- 1 root root 69563752 Nov 10 2021 ffmpeg // before chmod u+x
    -rwxr--r-- 1 root root 69563752 Nov 10 2021 ffmpeg // after chmod u+x
    True // in.mp4 exists
    True // out.mp4 exists before delete it using `rm`



Related Topics



Leave a reply



Submit