From a2c899d02e4384da7076a3a9b9d125755b79a672 Mon Sep 17 00:00:00 2001 From: Kevin Heritage Date: Fri, 17 Feb 2023 19:17:31 +0100 Subject: [PATCH] feat: create sub video function --- FFMpegCore/FFMpeg/FFMpeg.cs | 40 +++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/FFMpegCore/FFMpeg/FFMpeg.cs b/FFMpegCore/FFMpeg/FFMpeg.cs index 58526b8..3939502 100644 --- a/FFMpegCore/FFMpeg/FFMpeg.cs +++ b/FFMpegCore/FFMpeg/FFMpeg.cs @@ -239,6 +239,46 @@ public static bool Join(string output, params string[] videos) } } + private static FFMpegArgumentProcessor BaseSubVideo(string input, string output, TimeSpan startTime, TimeSpan endTime) + { + if (Path.GetExtension(input) != Path.GetExtension(output)) + { + output = Path.Combine(Path.GetDirectoryName(output), Path.GetFileNameWithoutExtension(output), Path.GetExtension(input)); + } + + return FFMpegArguments + .FromFileInput(input, true, options => options.Seek(startTime).EndSeek(endTime)) + .OutputToFile(output, true, options => options.CopyChannel()); + } + + /// + /// Creates a new video starting and ending at the specified times + /// + /// Input video file. + /// Output video file. + /// The start time of when the sub video needs to start + /// The end time of where the sub video needs to end + /// Output video information. + public static bool SubVideo(string input, string output, TimeSpan startTime, TimeSpan endTime) + { + return BaseSubVideo(input, output, startTime, endTime) + .ProcessSynchronously(); + } + + /// + /// Creates a new video starting and ending at the specified times + /// + /// Input video file. + /// Output video file. + /// The start time of when the sub video needs to start + /// The end time of where the sub video needs to end + /// Output video information. + public static async Task SubVideoAsync(string input, string output, TimeSpan startTime, TimeSpan endTime) + { + return await BaseSubVideo(input, output, startTime, endTime) + .ProcessAsynchronously(); + } + /// /// Records M3U8 streams to the specified output. ///