在 C# 中将视频拆分为四个文件 [英] Splitting a video in four files in C#

查看:48
本文介绍了在 C# 中将视频拆分为四个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究如何将视频分成四个片段.我见过很多解决方案和库.我在看这个图书馆:

I am doing a research of how to split a video in four fragments. I have seen a lot of solutions and libraries. I was looking at this library:

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);
}

代码只拆分了一个片段.有没有办法把它分成四个片段?

The code is splitting just one fragment. Is there a way to split it in four fragments?

亲切的问候

PS:解决方案必须是C#,并且已经看过Directshow的解决方案.

PS: the solution must be in C# and already have seen the Directshow solution.

推荐答案

我以前没有使用过这个库,但我会这样做.

I haven't used this library before but this is how I would go about it.

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

    }
}

这篇关于在 C# 中将视频拆分为四个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆