Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Frames to Video function #95

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions MediaToolkit src/MediaToolkit/CommandBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Globalization;
using System.Text;
using System.IO;

namespace MediaToolkit
{
Expand All @@ -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();
}
Expand All @@ -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)
{
Expand Down Expand Up @@ -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)
Expand Down
36 changes: 34 additions & 2 deletions MediaToolkit src/MediaToolkit/Engine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,43 @@ public void GetThumbnail(MediaFile inputFile, MediaFile outputFile, ConversionOp
this.FFmpegEngine(engineParams);
}

/// -------------------------------------------------------------------------------------------------
/// <summary> Extracts all frames from a video. </summary>
/// <param name="inputFile"> Video file. </param>
public void ExtractFrames(MediaFile inputFile)
{
EngineParameters engineParams = new EngineParameters
{
InputFile = inputFile,
Task = FFmpegTask.ExtractFrames
};

this.FFmpegEngine(engineParams);
}

/// -------------------------------------------------------------------------------------------------
/// <summary> Converts images to a video. </summary>
/// <param name="inputFile"> 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. </param>
/// <param name="ouputFile"> Output video file. </param>
/// <param name="fps"> Frame rate of output video file. </param>
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);
}
Expand Down Expand Up @@ -303,4 +335,4 @@ private void StartFFmpegProcess(EngineParameters engineParameters)
}
}
}
}
}
7 changes: 6 additions & 1 deletion MediaToolkit src/MediaToolkit/EngineParameters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,10 @@ internal bool HasCustomArguments
/// <summary> Gets or sets the task. </summary>
/// <value> The task. </value>
internal FFmpegTask Task { get; set; }

/// -------------------------------------------------------------------------------------------------
/// <summary> Gets or sets the framerate in the FramesToVideo function. </summary>
/// <value> The framespersecond. </value>
internal int fps { get; set; }
}
}
}
10 changes: 8 additions & 2 deletions MediaToolkit src/MediaToolkit/FFmpegTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ internal enum FFmpegTask
GetMetaData,

/// <summary> An enum constant representing the get thumbnail option. </summary>
GetThumbnail
GetThumbnail,

/// <summary> An enum constant representing the extract frames option. </summary>
ExtractFrames,

/// <summary> An enum constant representing the convertion of frames to video option. </summary>
FramesToVideo
}
}
}
7 changes: 6 additions & 1 deletion MediaToolkit src/MediaToolkit/Options/ConversionOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ public void CutMedia(TimeSpan seekToPosition, TimeSpan length)
/// Audio sample rate
/// </summary>
public AudioSampleRate AudioSampleRate = AudioSampleRate.Default;

/// <summary>
/// Audio sample rate
/// </summary>
public bool AudioDownmix = false;

/// <summary>
/// The maximum duration
Expand Down Expand Up @@ -101,4 +106,4 @@ public void CutMedia(TimeSpan seekToPosition, TimeSpan length)
public bool BaselineProfile { get; set; }
}

}
}