int RecordingLength(string file) { // Get the length of the video in seconds. We will run the "ffmpeg -i" command and parse the standard output. // The line containing "Duration: ...." will contain the duration in HH:MM:SS format. // We will convert this to a TimeSpan object and then obtain total seconds. RunCommand runCommand = new RunCommand(); string inputFileQuoted = "\"" + file + "\""; // Ffmpeg outputs all of its logging data to stderr, // to leave stdout free for piping the output data to some other program or ffmpeg instance. string command = "ffmpeg -i " + inputFileQuoted + " 2>&1"; string result = runCommand.ExecuteCmd(command); int y = result.IndexOf("Duration: "); int z = result.IndexOf(".", y); string duration = result.Substring(y + 10, 8); TimeSpan time = TimeSpan.Parse(duration); int videoLength = (int)time.TotalSeconds; // length of video in seconds return(videoLength); }
public int Split(string inputFile, string outputFolder, int segmentSize, int segmentOverlap) { RunCommand runCommand = new RunCommand(); Directory.CreateDirectory(outputFolder); int videoLength = RecordingLength(inputFile); int numberOfSections = videoLength / segmentSize; int mod = videoLength % segmentSize; // If the last segment is greater than 1/2 segment size, put it in it's own segment. if (mod > (segmentSize / 2)) { numberOfSections++; } //string inputFilename = Path.GetFileNameWithoutExtension(inputFile); // Create subfolders part01, part02, part03, etc for (int x = 1; x <= numberOfSections; x++) { int start = (x - 1) * segmentSize; string segmentFolder = outputFolder + $"\\part{x:D2}"; Directory.CreateDirectory(segmentFolder); string outputFile = segmentFolder + "\\" + "ToFix.mp4"; if (x < numberOfSections) { ExtractPart(inputFile, outputFile, start, segmentSize + segmentOverlap); } else { ExtractPart(inputFile, outputFile, start); // extract to end } } return(numberOfSections); }