python - 将 ffmpeg 上传到 Azure 函数环境 PATH [Python]

标签 python ffmpeg azure-functions filepath

我正在做音频提取项目。我的 python 脚本中使用了 Audioread 库。 Audioread 要求 FFMPEG 存在于 linux/usr/bin 中。如果/usr/bin 中存在 FFMPEG,代码 audioread.audio_open(XXX.m4a)可以正常运行,否则输出 NoBackendError我可以在本地 linux 系统中成功运行我的代码。但是,代码将输出 NoBackendError在 Azure 函数中。我的猜测是ffmpeg没有部署到/usr/bin,而是部署在/home/site/wwwroot。我的 python 代码在 PATH 中找不到 ffmpeg。
我尝试使用 os.popen('mv /home/site/wwwroot/ffmpeg /usr/bin/ffmpeg')在我的 python 脚本中将 ffmpeg 移动到 PATH,但它不起作用。我想原因应该是我没有添加sudo,但是我不知道Azure Function Linux平台的密码。
请给我一些帮助!

最佳答案

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


用户可以使用 在 Linux 虚拟机上以提升的权限运行命令须藤命令。但是,体验可能会有所不同,具体取决于系统的配置方式。
1. SSH key 和密码或仅密码 - 为虚拟机提供了证书(.CER 文件)和密码,或者仅提供了用户名和密码。在这种情况下 须藤将提示输入 用户密码在执行命令之前。
2. 仅 SSH key - 虚拟机配置有证书(.cer 或 .pem 文件),但没有密码。在这种情况下 须藤 不会提示输入用户密码 在执行命令之前。
您可以引用以下链接:http://azure.microsoft.com/en-us/documentation/articles/virtual-machines-linux-use-root-privileges/

我按照官方教程Create your first Python function in Azure创建 HttpTrigger Python的函数,并尝试了不同的方法来制作ffmpeg在 Azure Functions 中工作,它对我有用。
这些是我的步骤:
  • 安装 Azure Functions 核心工具 在我的本地 Windows 机器上创建一个名为 的项目我的功能项目 和一个名为 的函数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 krishna! ('64bit', '')在浏览器中。
  • 然后我把 ffmpeghttps://johnvansickle.com/ffmpeg/ 下载的 AMD64 静态二进制文件进入我的我的功能项目 并更改下面的代码以检查文件路径,并命令 func azure functionapp publish MyFunctionProj发布到 Azure。
    def main(req: func.HttpRequest) -> func.HttpResponse:
        if name:
            return func.HttpResponse(f"Hello {name}! {platform.architecture()} {os.listdir()} {os.listdir('HttpTrigger')}")
    
    

  • 输出: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']在浏览器中与我的 MyFunctionProj 中的这些相同
  • 我在 中找到了所有内容我的功能项目 文件夹将上传到 Azure 并调用 os.listdir()显示的文件列表我的功能项目 , 所以 Python 中的当前路径与 相同我的功能项目 本地。然后我尝试调用 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.mp4rm 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 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`
    
    
  • 关于python - 将 ffmpeg 上传到 Azure 函数环境 PATH [Python],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69909524/

    相关文章:

    Python Durable Function 不调用 Activity

    python - 我可以在对象内部创建列表吗

    FFMPEG 颜色到透明度

    android - 如何从以特定时间间隔分隔的 N 个连接的音频样本生成音频文件?

    python - 如何安排实验在 Azure ML SDK 中每天运行

    Visual Studio 中缺少 Azure Function App 发布目标

    python - 为什么 pickle 吃内存?

    python - 在两个 Python 脚本中将文件作为参数传递

    python - 具有相对时间的 Pandas 时间序列

    objective-c - 无法构建;架构 x86_64 : 的 undefined symbol