如何在 Azure Functions 中添加和执行二进制文件?

edm*_*dmz 4 python linux azure azure-functions

我需要ffmpeg从运行 python/linux 的 Azure 函数中运行一个 linux 二进制文件(就本示例而言)。

我还没有找到如何打包它或如何执行它。

Azure 上的一些 python 文档表明,可以有一个bin/根文件夹作为函数包的一部分。将文件放在那里然后调用subprocess.run()它失败。它显然找不到可执行文件。

我尝试将文件移动到根目录,或者尝试使用完整路径找到它(我已经尝试过/home/site/www它的变体)。仍然没有运气。

我错过了什么?

Pet*_*Pan 6

我按照官方教程为 PythonCreate your first Python function in Azure (preview)创建了一个HttpTrigger函数,并尝试了不同的方法ffmpeg在 Azure 函数中工作,然后它对我有用。

这是我在上面执行此操作的步骤,如下所示,希望对您有所帮助。

  1. 按照 Azure Functions for Python 的官方教程Azure Functions Core Tools在我的本地 Windows 机器上安装,创建一个名为 的项目MyFunctionProj和一个名为HttpTrigger.

  2. 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()}")
    
    Run Code Online (Sandbox Code Playgroud)

    它的结果是Hello peter-pan! ('64bit', '')在浏览器中。

  3. 然后我ffmpeg将从https://johnvansickle.com/ffmpeg/下载的AMD64 静态二进制文件放入我的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')}")
    
    Run Code Online (Sandbox Code Playgroud)

    它的结果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

  4. 我发现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()}")
    
    Run Code Online (Sandbox Code Playgroud)

    它可以out.mp4通过 command输出文件ffmpeg.exe -i in.mp4 out.mp4,然后考虑将其复制到 command del out.mp4

  5. 尝试使其适用于 Azure Function 上的 Linux 环境,我使用./ffmpeg -i in.mp4 out.mp4和更改命令rm out.mp4。但它不适用于 Azure Function。可能是ffmpeg从 Windows 上传时缺少linux 二进制文件的执行权限造成的。所以我通过命令检查了我的猜测ls -l ffmpegchmod 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()}")
    
    Run Code Online (Sandbox Code Playgroud)

    它现在可以工作了,结果如下所示,我将其格式化得很漂亮。

    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`
    
    Run Code Online (Sandbox Code Playgroud)

注意:我认为ffmpeg你在Linux中开发时会出现执行权限的问题。该in.mp4文件来自https://github.com/kkroening/ffmpeg-python/blob/master/examples/in.mp4。我曾尝试使用ffmpeg-python包来实现您的需求,但它似乎不适用于本地和 Azure。