diff --git a/MediaToolkit src/MediaToolkit/CommandBuilder.cs b/MediaToolkit src/MediaToolkit/CommandBuilder.cs index 127936e..7b5615f 100644 --- a/MediaToolkit src/MediaToolkit/CommandBuilder.cs +++ b/MediaToolkit src/MediaToolkit/CommandBuilder.cs @@ -4,6 +4,7 @@ using System; using System.Globalization; using System.Text; +using System.IO; namespace MediaToolkit { @@ -21,6 +22,10 @@ internal static string Serialize(EngineParameters engineParameters) case FFmpegTask.GetThumbnail: return GetThumbnail(engineParameters.InputFile, engineParameters.OutputFile, engineParameters.ConversionOptions); + case FFmpegTask.ExtractFrames: + return ExtractFrames(engineParameters.InputFile); + case FFmpegTask.FramesToVideo: + return FramesToVideo(engineParameters.InputFile, engineParameters.OutputFile, engineParameters.fps); default: throw new ArgumentOutOfRangeException(); } @@ -42,6 +47,26 @@ private static string GetThumbnail(MediaFile inputFile, MediaFile outputFile, Co return commandBuilder.AppendFormat(" \"{0}\" ", outputFile.Filename).ToString(); } + + private static string ExtractFrames(MediaFile inputFile) + { + var commandBuilder = new StringBuilder(); + + commandBuilder.AppendFormat(" -i \"{0}\" ", inputFile.Filename); + + return commandBuilder.AppendFormat(" " + Path.GetDirectoryName(inputFile.Filename) + "\\" + "frame%06d.jpg ").ToString(); + + } + + private static string FramesToVideo(MediaFile inputFile, MediaFile outputFile,int fps) + { + var commandBuilder = new StringBuilder(); + + commandBuilder.AppendFormat(" -framerate {0} -i \"{1}\" ", fps.ToString(), inputFile.Filename); + + return commandBuilder.AppendFormat(" \"{0}\" ", outputFile.Filename).ToString(); + + } private static string Convert(MediaFile inputFile, MediaFile outputFile, ConversionOptions conversionOptions) { @@ -79,6 +104,10 @@ private static string Convert(MediaFile inputFile, MediaFile outputFile, Convers // Audio sample rate if (conversionOptions.AudioSampleRate != AudioSampleRate.Default) commandBuilder.AppendFormat(" -ar {0} ", conversionOptions.AudioSampleRate.Remove("Hz")); + + //Audio downmix + if (conversionOptions.AudioDownmix) + commandBuilder.AppendFormat(" -ac 1 "); // Maximum video duration if (conversionOptions.MaxVideoDuration != null) diff --git a/MediaToolkit src/MediaToolkit/Engine.cs b/MediaToolkit src/MediaToolkit/Engine.cs index 6d623aa..0e9f950 100644 --- a/MediaToolkit src/MediaToolkit/Engine.cs +++ b/MediaToolkit src/MediaToolkit/Engine.cs @@ -119,11 +119,43 @@ public void GetThumbnail(MediaFile inputFile, MediaFile outputFile, ConversionOp this.FFmpegEngine(engineParams); } + /// ------------------------------------------------------------------------------------------------- + /// Extracts all frames from a video. + /// Video file. + public void ExtractFrames(MediaFile inputFile) + { + EngineParameters engineParams = new EngineParameters + { + InputFile = inputFile, + Task = FFmpegTask.ExtractFrames + }; + + this.FFmpegEngine(engineParams); + } + + /// ------------------------------------------------------------------------------------------------- + /// Converts images to a video. + /// Input image files. Must have the format "filename"%XXd.jpg/bmp. For example if there are mutiple jpgs in the directory named frame0001.jpg,frame0002.jpg, input would be "frame%04d.jpg. + /// Output video file. + /// Frame rate of output video file. + public void FramesToVideo(MediaFile inputFile, MediaFile outputFile, int fps) + { + EngineParameters engineParams = new EngineParameters + { + InputFile = inputFile, + OutputFile = outputFile, + fps = fps, + Task = FFmpegTask.FramesToVideo + }; + + this.FFmpegEngine(engineParams); + } + #region Private method - Helpers private void FFmpegEngine(EngineParameters engineParameters) { - if (!engineParameters.InputFile.Filename.StartsWith("http://") && !File.Exists(engineParameters.InputFile.Filename)) + if (!engineParameters.InputFile.Filename.StartsWith("http://") && !File.Exists(engineParameters.InputFile.Filename) &&!engineParameters.InputFile.Filename.Contains("%")) { throw new FileNotFoundException(Resources.Exception_Media_Input_File_Not_Found, engineParameters.InputFile.Filename); } @@ -303,4 +335,4 @@ private void StartFFmpegProcess(EngineParameters engineParameters) } } } -} \ No newline at end of file +} diff --git a/MediaToolkit src/MediaToolkit/EngineParameters.cs b/MediaToolkit src/MediaToolkit/EngineParameters.cs index ee7a984..2ee78e2 100644 --- a/MediaToolkit src/MediaToolkit/EngineParameters.cs +++ b/MediaToolkit src/MediaToolkit/EngineParameters.cs @@ -37,5 +37,10 @@ internal bool HasCustomArguments /// Gets or sets the task. /// The task. internal FFmpegTask Task { get; set; } + + /// ------------------------------------------------------------------------------------------------- + /// Gets or sets the framerate in the FramesToVideo function. + /// The framespersecond. + internal int fps { get; set; } } -} \ No newline at end of file +} diff --git a/MediaToolkit src/MediaToolkit/FFmpegTask.cs b/MediaToolkit src/MediaToolkit/FFmpegTask.cs index 2e858d6..5ddbfb5 100644 --- a/MediaToolkit src/MediaToolkit/FFmpegTask.cs +++ b/MediaToolkit src/MediaToolkit/FFmpegTask.cs @@ -11,6 +11,12 @@ internal enum FFmpegTask GetMetaData, /// An enum constant representing the get thumbnail option. - GetThumbnail + GetThumbnail, + + /// An enum constant representing the extract frames option. + ExtractFrames, + + /// An enum constant representing the convertion of frames to video option. + FramesToVideo } -} \ No newline at end of file +} diff --git a/MediaToolkit src/MediaToolkit/Options/ConversionOptions.cs b/MediaToolkit src/MediaToolkit/Options/ConversionOptions.cs index 5b85d48..f79fae6 100644 --- a/MediaToolkit src/MediaToolkit/Options/ConversionOptions.cs +++ b/MediaToolkit src/MediaToolkit/Options/ConversionOptions.cs @@ -38,6 +38,11 @@ public void CutMedia(TimeSpan seekToPosition, TimeSpan length) /// Audio sample rate /// public AudioSampleRate AudioSampleRate = AudioSampleRate.Default; + + /// + /// Audio sample rate + /// + public bool AudioDownmix = false; /// /// The maximum duration @@ -101,4 +106,4 @@ public void CutMedia(TimeSpan seekToPosition, TimeSpan length) public bool BaselineProfile { get; set; } } -} \ No newline at end of file +}