使用 .net c# 压缩并获取 .mp4 文件的缩略图

问题描述 投票:0回答:2

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

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

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

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

有人对此有想法吗?

c# .net .net-core ffmpeg
2个回答
1
投票

经过大量搜索后,我发现 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

1
投票

您可以使用 FFMpegCore 库。

Nuget: Install-Package FFMpegCore

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

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

这些都可以参考github vladjerca/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);
© www.soinside.com 2019 - 2024. All rights reserved.