0

I have a project on azure function serverless platform with .net core 3. In this project, I have a function which converts video files from different formats to mp4 format using Nreco.VideoConverter.

When I run it locally it works perfectly. But when I deploy it works but takes much longer time to do so (For example: for a 1.3mb file it takes 10 seconds locally to convert but about 100 seconds on the deployed version)

this is my code:

public static Stream ConvertVideoToMp4(Stream fileStream, string fileName)
{
    MemoryStream outputStream = new MemoryStream();
    try
    {
        string inputFormat = GetVideoFormat(fileName);
        FFMpegConverter ffMpeg = initFfmpeg();

        if (ffMpeg == null)
        {
            throw new Exception("ffMpeg was not initilaized correctly");
        }

        string tempOriginalFilePath = Path.Combine(Path.GetTempPath(), fileName);

        if (File.Exists(tempOriginalFilePath))
        {
            File.Delete(tempOriginalFilePath);
        }

        using (var fs = new FileStream(tempOriginalFilePath, FileMode.Create, FileAccess.ReadWrite))
        {
            fileStream.CopyTo(fs);
            ffMpeg.ConvertMedia(tempOriginalFilePath, outputStream, Format.mp4);
        }

        File.Delete(tempOriginalFilePath);
    }
    catch (Exception ex)
    {
        Logger.Error(ex);
    }
    return outputStream;
}

private static FFMpegConverter initFfmpeg()
{
    //License setup
    //https://www.nuget.org/packages/NReco.VideoConverter.LT/
    FFMpegConverter ffMpeg = null;

    try
    {
        ffMpeg = new FFMpegConverter();

        NReco.VideoConverter.License.SetLicenseKey("secretKey");

        ffMpeg.FFMpegExeName = "ffmpeg.exe";

        DirectoryInfo dir = new DirectoryInfo(Directory.GetCurrentDirectory());
        dir = new DirectoryInfo(dir.Parent.Parent.Parent.FullName);
        ffMpeg.FFMpegToolPath = dir.FullName + @"\ffmpeg\";

        //then we are on the serveless enviroment 
        if (!File.Exists(dir.FullName + @"\ffmpeg\ffmpeg.exe"))
        {
            ffMpeg.FFMpegToolPath = @"D:\home\site\wwwroot\ffmpeg\";
        }
    } 
    catch (Exception ex)
    {
       Logger.Error(ex);
    }

    return ffMpeg;
}

Does anyone know how to solve it?

Ran Marciano
  • 1,431
  • 5
  • 13
  • 30

0 Answers0