3

I am doing the audio extraction project. Audioread library is used in my python script. Audioread required FFMPEG to be existed in linux /usr/bin. If the FFMPEG is existed in /usr/bin, the code audioread.audio_open(XXX.m4a) can run without error, if not, it will output NoBackendError

I can run my code successfully in my local linux system. However, the code will output NoBackendError in Azure Function. My guess is the ffmpeg is not deployed to the /usr/bin, but is deployed in /home/site/wwwroot. My python code cannot find ffmpeg in PATH.

I have tried to use os.popen('mv /home/site/wwwroot/ffmpeg /usr/bin/ffmpeg') in my python script to move ffmpeg to PATH, but it doesnt work. I think the reason should be I did not add sudo, but I dont know the password of Azure Function Linux platform.

Please give me some help!

1 Answer 1

4

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

6 Comments

I have tried the above, azure.microsoft.com/en-us/documentation/articles/…, it is just run the ffmpeg command line tool, the python library still cannot detect the ffmpeg and output NoBackendError. I need to add ffmpeg to the PATH of Azure Function, if not my local python file cannot run on the Azure Function.
@user14834847, Please check this - After download and installing the ffmpeg, make sure you can start ffmpeg from the command line (type ffmpeg -h). You will probably need to add the path to the install folder (eg c:\ffmpeg\bin) to the Windows path. Finally, make sure to restart your IDE. Visual Studio Code probably won't recognise the new path until after a reset.
Thank you for your reply. That is the point, is there any method that can add the path to the folder of ffmpeg or move ffmpeg to system PATH of AZURE Serverless Function. My code and ffmpeg both can work well locally.
@user14834847 - The ffmpeg dependencies will be in root path of our project like <project_root>/ffmpeg_lib directory. Please check this Mcrosoft documentation on how to add your ffmpeg path into your project folder of your azure function and how to access it.
I dont know why it doesnt work for me. I finally edit the source file of the audioread library audio_open function to make it can call ffmpeg even the ffmpeg is not in the environmet PATH. Anyway, thank you for your help!
|

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.