0

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

And this is the code for splitting the video

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?

Kind regards

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

Fearcoder
  • 753
  • 3
  • 15
  • 47

2 Answers2

1

It works well for me, but I'll fix the algorithm, because I'm missing the final video following, the code I have at the moment is this:

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


            }
        }
    }
  • 1
    Welcome to SO! So if the last video is missing. Is it working or not? When you post an answer, comment it a little bit. In your case, if your proposed solution have one problem, mention where, even, make a question with it. – David García Bodego Oct 27 '19 at 05:13
0

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

    }
}
artman41
  • 402
  • 2
  • 5
  • 15
  • Thanks for your time! There is a problem with this line of code 'double t = inputFile.Length/4;' 'MediaToolkit.Model.MediaFile' does not contain a definition for 'Length' and no extension method 'Length' accepting a first argument of type 'MediaToolkit.Model.MediaFile' could be found (are you missing a using directive or an assembly reference?) – Fearcoder May 09 '17 at 10:46
  • Same story for engine.Destroy() – Fearcoder May 09 '17 at 10:46
  • @Fearhunter you'll need to look for something similar, I'll comment what I was looking for. I've never used the library so I don't know exactly what I'd need to call sorry. `inputFile.Length/4` would be the length of the file / 4, there should be a method or something similar to get this `engine.Destroy()` there should be a way of destroying the object or closing the inputstream. That's what you should do there – artman41 May 09 '17 at 10:54
  • @Chud37 dispose is typically to garbage collect an object and iirc is a function inherited from the base `object` class, I think what you're doing there is keeping the streams open in memory but destroying and garbage collecting your accessor to those streams. This may have the side effect of disposing the streams though since C# should be smart enough to garbage collect a resource no longer in use – artman41 Sep 17 '19 at 18:17