如何在Docker中使用dotnet核心作为Azure函数运行FFProbe? [英] How to get FFProbe to run using dotnet core as an Azure Function in Docker?

查看:56
本文介绍了如何在Docker中使用dotnet核心作为Azure函数运行FFProbe?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从基于URL的视频中获取视频详细信息(长度,高度,宽度和内容类型).通过使用nuget包 https://www.nuget.org/packages/NReco.VideoInfo.LT 我能够轻松地创建一个在本地运行的Azure函数,指向我的FFProbe安装.但是,诀窍现在是使其与Azure函数兼容,据我所知,最简单的方法是提供一个包含所需内容的Docker映像(该映像将在Linux机器上运行)./p>

所以,让我们进入代码.以前,这是我在本地运行该函数时要进行的工作:

  var ffProbe =新的NReco.VideoInfo.FFProbe();ffProbe.FFProbeExeName ="ffprobe.exe";//只是"ffprobe"适用于Linux/OS-XffProbe.ToolPath ="C:\\ tools \\ ffmpeg \\ bin";var videoInfo = ffProbe.GetMediaInfo(videoUrl); 

现在,(再次,这是我所知的最好的),要使其在使用Docker的Linux环境中运行,我遇到了几个问题.

所以,第一次尝试是我想并尝试执行apt-get install ffmpeg的简单方法.这是Dockerfile(对不起,这将是一篇很长的文章……但是最好显示我所想的一切):

 来自mcr.microsoft.com/dotnet/core/sdk:3.1 AS installer-env#0.安装基本软件包运行apt-get -y更新运行apt-get -y升级运行apt-get install -y ffmpegENV AzureWebJobsStorage ="复制 ./src/dotnet-function-app运行cd/src/dotnet-function-app&&\mkdir -p/home/site/wwwroot&&\dotnet发布* .csproj --output/home/site/wwwroot运行mkdir -p/home/site/wwwroot/bin运行MV/usr/bin/*/home/site/wwwroot/bin#启用ssh&应用服务的远程调试将基础映像更改为以下映像#从mcr.microsoft.com/azure-functions/dotnet:3.0-appservice从mcr.microsoft.com/azure-functions/dotnet:3.0ENV AzureWebJobsScriptRoot =/home/site/wwwroot \AzureFunctionsJobHost__Logging__Console__IsEnabled = trueCOPY --from = installer-env ["/home/site/wwwroot","/home/site/wwwroot"] 

我将代码更改为

  var ffProbe =新的NReco.VideoInfo.FFProbe();ffProbe.FFProbeExeName ="ffprobe";//只是"ffprobe"适用于Linux/OS-XffProbe.ToolPath ="/home/site/wwwroot/bin";var videoInfo = ffProbe.GetMediaInfo(videoUrl); 

该构建了,但是当我运行它时,出现一个错误:加载共享库时出错:libavdevice.so.58

因此,我进行了一些挖掘,看来我必须走手工建造路线.这并不理想,但我可以尝试调整Dockerfile:

 来自mcr.microsoft.com/dotnet/core/sdk:3.1 AS installer-env#0.安装基本软件包运行apt-get update -qq&&apt-get -y安装\autoconf \自动制作建立必要的cmakegit-core \libass-dev \libfreetype6-dev \libsdl2-dev \libtool \libva-dev \libvdpau-dev \libvorbis-dev \libxcb1-dev \libxcb-shm0-dev \libxcb-xfixes0-dev \pkg-config \texinfo \wgetzlib1g-dev \nasm \yasm \libx265-dev \libnuma-dev \libvpx-dev \libmp3lame-dev \libopus-dev \libx264-dev运行mkdir -p/home/site/wwwroot运行mkdir -p/home/site/wwwroot/ffmpeg_sources/home/site/wwwroot/bin&&cd/home/site/wwwroot/ffmpeg_sources&&\wget -O ffmpeg-4.3.1.tar.bz2 https://ffmpeg.org/releases/ffmpeg-4.3.1.tar.bz2&&\tar xjvf ffmpeg-4.3.1.tar.bz2&&\cd ffmpeg-4.3.1&&\PATH ="/home/site/wwwroot/bin:$ PATH"PKG_CONFIG_PATH ="/home/site/wwwroot/ffmpeg_build/lib/pkgconfig"./配置 \--prefix ="/home/site/wwwroot/ffmpeg_build";\--pkg-config-flags =-static";\--extra-cflags ="-I/home/site/wwwroot/ffmpeg_build/include";\--extra-ldflags ="-L/home/site/wwwroot/ffmpeg_build/lib";\--extra-libs =-lpthread -lm";\--bindir ="/home/site/wwwroot/bin";\--enable-gpl \--enable-libass \--enable-libfreetype \--enable-libmp3lame \--enable-libopus \--enable-libvorbis \--enable-libvpx \--enable-libx264 \--enable-libx265 \-启用非自由&&\PATH ="/home/site/wwwroot/bin:$ PATH"制作-j8&&\进行安装-j8&&\哈希-rENV AzureWebJobsStorage ="复制 ./src/dotnet-function-app运行cd/src/dotnet-function-app&&\dotnet发布* .csproj --output/home/site/wwwroot从mcr.microsoft.com/azure-functions/dotnet:3.0ENV AzureWebJobsScriptRoot =/home/site/wwwroot \AzureFunctionsJobHost__Logging__Console__IsEnabled = trueCOPY --from = installer-env ["/home/site/wwwroot","/home/site/wwwroot"] 

再次构建,然后尝试运行,这次出现相同的错误,但文件不同:加载共享库时出错:libxcb.so.1

因此,我应该提到Docker对我来说是个新手,所以我可能会错过一些简单的事情.但是我整夜试图解决这个问题,直到我决定需要一些建议.我的主应用程序也托管在Linux机器上,因此不行了.除了Azure Function方法外,另一个疯狂的想法是以每月约20美元的价格出售低优先级的Windows VM……但是随后我必须配置IIS和所有其他东西.因此,弄清楚我在做什么错或是否可行是非常好的.在基于 https:之前,我已经使Python/Selenium可以使用Docker和Azure Functions进行工作.//github.com/rebremer/azure-function-selenium .. .所以我希望只是我对Docker不够熟悉.

任何帮助我完成这项工作的帮助将不胜感激.如有任何疑问,请随时询问.

谢谢!

解决方案

一旦我意识到基本上Docker中Azure功能的映像是从Ubuntu开始的,我的解决方案最终就不再使用另一个Docker映像.意识到这一点,我的Dockerfile出来了

  FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS installer-env复制 ./src/dotnet-function-app运行cd/src/dotnet-function-app&&\mkdir -p/home/site/wwwroot&&\dotnet发布* .csproj --output/home/site/wwwroot从mcr.microsoft.com/azure-functions/dotnet:3.0ENV AzureWebJobsScriptRoot =/home/site/wwwroot \AzureFunctionsJobHost__Logging__Console__IsEnabled = true \AzureWebJobsStorage ="COPY --from = installer-env ["/home/site/wwwroot","/home/site/wwwroot"]运行apt-get update&&&apt-get install -y ffmpeg 

因此,它最终是一个非常简单的解决方案.让Azure建立映像,将网站复制到wwwroot,然后像通常在Ubuntu上一样安装ffmpeg!然后,只需将代码更新为:

  var ffProbe = new NReco.VideoInfo.FFProbe();ffProbe.FFProbeExeName ="ffprobe";//只是"ffprobe"适用于Linux/OS-XffProbe.ToolPath ="/usr/bin";var mediaInfo = ffProbe.GetMediaInfo(videoUrl); 

就是这样!

I am trying to get video details (length, height, width, and content type) from a video based on a URL. By using the nuget package https://www.nuget.org/packages/NReco.VideoInfo.LT I was able to create an Azure Function running locally pointing to my local install of FFProbe rather easily. However, the trick is now making it compatible with an Azure Function and, to the best of my knowledge, the easiest way to do that is by providing a Docker image that contains everything needed (this would be running on a Linux machine).

So, let's get into the code. Previously, this is what I had working if I ran the function locally:

   var ffProbe = new NReco.VideoInfo.FFProbe();
   ffProbe.FFProbeExeName = "ffprobe.exe";  // just "ffprobe" for Linux/OS-X
   ffProbe.ToolPath = "C:\\tools\\ffmpeg\\bin";
   var videoInfo = ffProbe.GetMediaInfo(videoUrl);

Now, (and again, this is the best to my knowledge), to get it to run in a Linux environment using Docker, I've run into several issues.

So, the first attempt was the easy way I suppose and try to do a apt-get install ffmpeg. This was the Dockerfile (Sorry, this is going to be a long post... but better to show everything I suppose):

   FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS installer-env
   
   # 0. Install essential packages
   RUN apt-get -y update
   RUN apt-get -y upgrade
   RUN apt-get install -y ffmpeg
   
   ENV AzureWebJobsStorage=""
   
   COPY . /src/dotnet-function-app
   RUN cd /src/dotnet-function-app && \
   mkdir -p /home/site/wwwroot && \
   dotnet publish *.csproj --output /home/site/wwwroot
    
   RUN mkdir -p /home/site/wwwroot/bin
   RUN mv /usr/bin/* /home/site/wwwroot/bin
   
   # To enable ssh & remote debugging on app service change the base image to the one below
   # FROM mcr.microsoft.com/azure-functions/dotnet:3.0-appservice
   FROM mcr.microsoft.com/azure-functions/dotnet:3.0
   ENV AzureWebJobsScriptRoot=/home/site/wwwroot \
   AzureFunctionsJobHost__Logging__Console__IsEnabled=true

   COPY --from=installer-env ["/home/site/wwwroot", "/home/site/wwwroot"]

I change the code to

   var ffProbe = new NReco.VideoInfo.FFProbe();
   ffProbe.FFProbeExeName = "ffprobe";  // just "ffprobe" for Linux/OS-X
   ffProbe.ToolPath = "/home/site/wwwroot/bin";
   var videoInfo = ffProbe.GetMediaInfo(videoUrl);

That builds, but when I run it, I get an error: error while loading shared libraries: libavdevice.so.58

So, I do some digging and it looks like I have to go the manual build route. Which isn't ideal, but I give it a go and adjust the Dockerfile:

   FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS installer-env

   # 0. Install essential packages
   RUN apt-get update -qq && apt-get -y install \
       autoconf \
       automake \
       build-essential \
       cmake \
       git-core \
       libass-dev \
       libfreetype6-dev \
       libsdl2-dev \
       libtool \
       libva-dev \
       libvdpau-dev \
       libvorbis-dev \
       libxcb1-dev \
       libxcb-shm0-dev \
       libxcb-xfixes0-dev \
       pkg-config \
       texinfo \
       wget \
       zlib1g-dev \
       nasm \
       yasm \
       libx265-dev \
       libnuma-dev \
       libvpx-dev \
       libmp3lame-dev \
       libopus-dev \
       libx264-dev
   
   RUN mkdir -p /home/site/wwwroot
   
   RUN mkdir -p /home/site/wwwroot/ffmpeg_sources /home/site/wwwroot/bin && cd /home/site/wwwroot/ffmpeg_sources && \
       wget -O ffmpeg-4.3.1.tar.bz2 https://ffmpeg.org/releases/ffmpeg-4.3.1.tar.bz2 && \
       tar xjvf ffmpeg-4.3.1.tar.bz2 && \
       cd ffmpeg-4.3.1 && \
       PATH="/home/site/wwwroot/bin:$PATH" PKG_CONFIG_PATH="/home/site/wwwroot/ffmpeg_build/lib/pkgconfig" ./configure \
       --prefix="/home/site/wwwroot/ffmpeg_build" \
       --pkg-config-flags="--static" \
       --extra-cflags="-I/home/site/wwwroot/ffmpeg_build/include" \
       --extra-ldflags="-L/home/site/wwwroot/ffmpeg_build/lib" \
       --extra-libs="-lpthread -lm" \
       --bindir="/home/site/wwwroot/bin" \
       --enable-gpl \
       --enable-libass \
       --enable-libfreetype \
       --enable-libmp3lame \
       --enable-libopus \
       --enable-libvorbis \
       --enable-libvpx \
       --enable-libx264 \
       --enable-libx265 \
       --enable-nonfree && \
       PATH="/home/site/wwwroot/bin:$PATH" make -j8 && \
       make install -j8 && \
       hash -r
   
   ENV AzureWebJobsStorage=""
   
   COPY . /src/dotnet-function-app
   RUN cd /src/dotnet-function-app && \
   dotnet publish *.csproj --output /home/site/wwwroot
   
   FROM mcr.microsoft.com/azure-functions/dotnet:3.0
   ENV AzureWebJobsScriptRoot=/home/site/wwwroot \
       AzureFunctionsJobHost__Logging__Console__IsEnabled=true
   
   COPY --from=installer-env ["/home/site/wwwroot", "/home/site/wwwroot"]

Again, that builds and then I try to run, this time getting the same error but for a different file: error while loading shared libraries: libxcb.so.1

So, I should mention that Docker is somewhat new to me, so I could be missing something easy. But I've tried to figure this out all night until I decided that I needed some advice. My main app is also hosted on a linux machine, so that would be a no go there. Besides the Azure Function approach, the other crazy idea is spinning up a low-priority Windows VM for like $20 a month... but then I have to configure IIS and all that stuff. So, it would be great to figure out what I'm doing wrong or if it's even feasible. I've gotten Python/Selenium to work using Docker and Azure Functions before based off of https://github.com/rebremer/azure-function-selenium... so I'm hoping it's just me not being familiar with Docker enough.

Any help to get this working for me would be greatly appreciated. If you have any questions, please don't hesitate to ask.

Thanks!

解决方案

My solution ended up not using another Docker image once I realized that basically the image for Azure Functions in Docker starts off with Ubuntu. Realizing that, my Dockerfile came out to be

FROM mcr.microsoft.com/dotnet/core/sdk:3.1 AS installer-env

COPY . /src/dotnet-function-app
RUN cd /src/dotnet-function-app && \
    mkdir -p /home/site/wwwroot && \
    dotnet publish *.csproj --output /home/site/wwwroot

FROM mcr.microsoft.com/azure-functions/dotnet:3.0
ENV AzureWebJobsScriptRoot=/home/site/wwwroot \
    AzureFunctionsJobHost__Logging__Console__IsEnabled=true \
    AzureWebJobsStorage=""

COPY --from=installer-env ["/home/site/wwwroot", "/home/site/wwwroot"]

RUN apt-get update && apt-get install -y ffmpeg

So, it ended up being a really simple solution. Have Azure build the image, copy the website to wwwroot and then install ffmpeg like you would normally on Ubuntu! Then, it was a matter of just updating the code to:

var ffProbe = new NReco.VideoInfo.FFProbe();
ffProbe.FFProbeExeName = "ffprobe";  // just "ffprobe" for Linux/OS-X
ffProbe.ToolPath = "/usr/bin";
var mediaInfo = ffProbe.GetMediaInfo(videoUrl);

And that's it!

这篇关于如何在Docker中使用dotnet核心作为Azure函数运行FFProbe?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆