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:
Install Azure Functions Core Tools on my local Windows machine to create a project named MyFunctionProj and a function named HttpTrigger.
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.
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
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.
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`