【问题标题】:Compress and get thumbnails from .mp4 files using .net c#使用 .net c# 压缩并从 .mp4 文件中获取缩略图
【发布时间】:2020-03-15 18:41:38
【问题描述】:

我正在搜索如何压缩 mp4 文件并从该文件生成缩略图,幸运的是,我找到了一种解决方案 “MediaToolKit”,它非常适合生成缩略图,但不适用于压缩。

目前,我在做什么,我已经生成了 .bat 文件,其中包含例如 FFmpeg 的命令

ffmpeg -i %1 -an -crf 25 -vf fps=fps=30,scale=640x480 %output.mp4

但这不是什么好东西,我想用 C# 编写一个程序,它会拍摄视频并压缩它并生成缩略图

有人知道吗?

【问题讨论】:

    标签: c# .net .net-core ffmpeg


    【解决方案1】:

    您可以使用 FFMpegCore 库。

    Nuget: Install-Package FFMpegCore
    

    安装软件包后,您必须从ffbinaries 下载二进制文件。 要更改根目录路径,请使用:

    public Startup() 
    {
       FFMpegOptions.Configure(new FFMpegOptions { RootDirectory = "./bin" });
    }
    

    这些都参考githubvladjerca/FFMpegCore

    进行压缩:

    Startup();
    string inputFile = @"yourVideoPath.mp4";
    var encoder = new FFMpeg();
    FileInfo outputFile = new FileInfo(@"newVideo.mp4");
    var video = VideoInfo.FromPath(inputFile);
    //track conversion progress
    encoder.OnProgress += (percentage) => Console.WriteLine("Progress {0}%", percentage);
    encoder.Convert(
          video,
          outputFile,
          VideoType.Mp4,
          Speed.UltraFast,
          VideoSize.Ld,
          AudioQuality.Low,
         true
         );
    

    【讨论】:

      【解决方案2】:

      经过大量搜索后,我发现 MediaToolKit 是最好的选择,因为 它提供了自定义命令的选项

      通过自定义命令压缩视频 例如

        using (var engine = new Engine(@"C:\ffmpeg.exe"))
        {
           int crf = 23;
           string resolution = "960:720";
           string command = string.Format(@"-i {0} -vf scale={1} -c:v libx264 -preset 
           ultrafast -r 30 -crf {2} {3}", input, resolution, crf, output);
      
           engine.CustomCommand(command);
         }
      

      要获取缩略图,它有方法

      engine.GetThumbnail() // it will take 3 arguments inputFile, outputFile, conversionOptions
      

      【讨论】:

        猜你喜欢
        • 2011-06-13
        • 1970-01-01
        • 2018-02-02
        • 1970-01-01
        • 2013-08-30
        • 2014-02-09
        • 2013-07-05
        • 2010-10-12
        • 2017-03-28
        相关资源
        最近更新 更多