我需要从运行 python/linux 的 Azure 函数中运行 Linux 二进制文件(在本示例中为 ffmpeg
)。
我还没有找到如何打包它或如何执行它。
Some of the python docs on Azure证明可以有 bin/
根文件夹作为函数包的一部分。将文件放在那里然后调用 subprocess.run()
就失败了。它显然找不到可执行文件。
我尝试将文件移动到根目录,或尝试使用完整路径查找它(我尝试过 /home/site/www
及其变体)。还是没有运气。
我错过了什么?
最佳答案
我按照官方教程 Create your first Python function in Azure (preview)
创建 HttpTrigger
Python的函数,并尝试了不同的方法来制作ffmpeg
在 Azure Functions 中工作,那么它也适合我。
以上是我的操作步骤,如下,希望对您有所帮助。
按照Azure Functions for Python官方教程安装
Azure Functions Core Tools
在我的本地 Windows 计算机上创建一个名为MyFunctionProj
的项目和一个名为HttpTrigger
的函数.上传之前
ffmpeg
通过部署,我通过使用以下代码更改官方示例代码来检查 Azure 上的 Azure Functions 实例的操作系统平台体系结构。# add these codes import platform, os ..... def main(req: func.HttpRequest) -> func.HttpResponse: if name: return func.HttpResponse(f"Hello {name}! {platform.architecture()}")
其结果是
Hello peter-pan! ('64bit', '')
在浏览器中。然后我输入
ffmpeg
AMD64 静态二进制文件从 https://johnvansickle.com/ffmpeg/ 下载进入我的MyFunctionProj
并更改下面的代码以检查文件路径,并命令func azure functionapp publish <my app name>
发布到 Azure。def main(req: func.HttpRequest) -> func.HttpResponse: if name: return func.HttpResponse(f"Hello {name}! {platform.architecture()} {os.listdir()} {os.listdir('HttpTrigger')}")
其结果是
Hello peter-pan! ('64bit', '') ['in.mp4', 'ffmpeg', 'host.json', 'requirements.txt', 'ffmpeg.exe', '.python_packages', 'HttpTrigger'] ['in.mp4', '__pycache__', 'sample.dat', 'host.json', 'function.json', '__init__.py']
在浏览器中与我的MyFunctionProj
中的相同我在
MyFunctionProj
中找到了所有内容文件夹将上传到 Azure 并调用os.listdir()
显示MyFunctionProj
的文件列表,所以Python中的当前路径与MyFunctionProje
相同本地。然后我尝试调用ffmpeg
通过下面的代码在我的本地 Windows 环境中。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()}")
它可以输出文件
out.mp4
通过命令ffmpeg.exe -i in.mp4 out.mp4
,然后考虑将其复制到命令del out.mp4
.尝试使其适用于 Azure Function 上的 Linux 环境,我将命令更改为
./ffmpeg -i in.mp4 out.mp4
和rm out.mp4
。但它在 Azure Function 上不起作用。可能是缺少ffmpeg
的执行权限导致的从 Windows 上传时的 linux 二进制文件。所以我通过命令ls -l ffmpeg
检查了我的猜测和chmod u+x ffmpeg
在调用它之前。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()}")
它现在可以工作了,结果如下所示,我把它格式化得很漂亮。
Hello peter-pan! // Offical 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 Mar 13 2019 ffmpeg // before chmod u+x -rwxr--r-- 1 root root 69563752 Mar 13 2019 ffmpeg // after chmod u+x True // in.mp4 exists True // out.mp4 exists before delete it using `rm`
注意:我认为ffmpeg
的执行权限问题当你在 Linux 中开发时就会发生这种情况。还有in.mp4
文件来自https://github.com/kkroening/ffmpeg-python/blob/master/examples/in.mp4 。我曾尝试使用 ffmpeg-python
包来实现您的需求,但它似乎不适用于本地和Azure。
关于python - 如何在 Azure Functions 中添加和执行二进制文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55132619/