2017-05-09 33 views
0

我正在研究如何将视频分成四个片段。我见过很多解决方案和库。我一直在寻找这个库:在C中分割四个文件中的视频#

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

也许这样做4次改变起始第二? – Pikoh

+0

你的意思是我必须做4 var选项=新的ConversionOptions(); ?我正在考虑这个问题,但这是一个很好的解决方案吗? – Fearcoder

+0

我没有使用过这个库,但对我来说听起来不错,4'var options'和4'engine.Convert'我猜 – Pikoh

回答

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 

    } 
} 
+0

感谢您的时间!这行代码有一个问题'double t = inputFile.Length/4;' 'MediaToolkit.Model.MediaFile'没有包含'Length'的定义,并且没有找到接受'MediaToolkit.Model.MediaFile'类型的第一个参数的扩展方法'Length'(你是否缺少using指令或程序集引用?) – Fearcoder

+0

同样的故事为engine.Destroy() – Fearcoder

+0

@Fearhunter你需要寻找类似的东西,我会评论我在找什么。我从来没有使用过这个库,所以我不知道我需要怎样称呼对不起。 'inputFile.Length/4'将是文件长度/ 4,应该有一个方法或者类似的东西来获得这个'engine.Destroy()'应该有一种销毁对象或关闭的方法输入流。那就是你应该在那里做的 – artman41