我正在研究如何将视频分成四个片段。我见过很多解决方案和库。我在看这个图书馆:
https://github.com/AydinAdn/MediaToolkit
这是分割视频的代码
var inputFile = new MediaFile {Filename = @"C:\Path\To_Video.flv"};
var outputFile = new MediaFile {Filename = @"C:\Path\To_Save_ExtractedVideo.flv"};
using (var engine = new Engine())
{
engine.GetMetadata(inputFile);
var options = new ConversionOptions();
// This example will create a 25 second video, starting from the
// 30th second of the original video.
//// First parameter requests the starting frame to cut the media from.
//// Second parameter requests how long to cut the video.
options.CutMedia(TimeSpan.FromSeconds(30), TimeSpan.FromSeconds(25));
engine.Convert(inputFile, outputFile, options);
}
代码只拆分一个片段。有没有办法将它分成四个片段?
亲切的问候
PS:解决方案必须在C#中,并且已经看过Directshow解决方案。
答案 0 :(得分:0)
之前我没有使用过这个库,但这就是我要去做的。
var inputFile = new MediaFile {Filename = @"C:\Path\To_Video.flv"};
var outputName = "C:\Path\To_Save_ExtractedVideo";
var outputExtension = ".flv";
double t = inputFile.Length/4; //length of parts -- need to use method to get file playtime length
for(int i=0;i<4;i++){
var engine = new Engine()
engine.GetMetadata(inputFile);
var options = new ConversionOptions();
// This example will create a 25 second video, starting from the
// 30th second of the original video.
//// First parameter requests the starting frame to cut the media from.
//// Second parameter requests how long to cut the video.
options.CutMedia(TimeSpan.FromSeconds(30 + (i*int.Parse(t))), TimeSpan.FromSeconds((i+1)*int.Parse(t)));
engine.Convert(inputFile, $"{outputName}_{i.ToString()}{outputExtension}, options);
engine.Destroy(); // Need to destroy object or close inputstream. Whichever the library offers
}
}
答案 1 :(得分:0)
它对我来说效果很好,但是我会修复算法,因为我错过了下面的最终视频,目前我的代码是这样的:
static void Main(string[] args)
{
using (var engine = new Engine())
{
string file = @"C:\Users\wilso\Downloads\IZA - Meu Talismã.mp4";
var inputFile = new MediaFile { Filename = file };
engine.GetMetadata(inputFile);
var outputName = @"C:\Users\wilso\Downloads\output";
var outputExtension = ".mp4";
double Duration = inputFile.Metadata.Duration.TotalSeconds;
double currentPosition = 0;
int contador = 0;
while (currentPosition < Duration)
{
currentPosition = contador * 30;
contador++;
var options = new ConversionOptions();
var outputFile = new MediaFile(outputName + contador.ToString("00") + outputExtension);
options.CutMedia(TimeSpan.FromSeconds(currentPosition), TimeSpan.FromSeconds(30));
engine.Convert(inputFile, outputFile, options);
}
}
}