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 audio downmixing and ExtractFrames function #94

Open
wants to merge 5 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
17 changes: 17 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,8 @@ internal static string Serialize(EngineParameters engineParameters)

case FFmpegTask.GetThumbnail:
return GetThumbnail(engineParameters.InputFile, engineParameters.OutputFile, engineParameters.ConversionOptions);
case FFmpegTask.ExtractFrames:
return ExtractFrames(engineParameters.InputFile);
default:
throw new ArgumentOutOfRangeException();
}
Expand All @@ -42,6 +45,16 @@ 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(@" -r 1/1 " + Path.GetDirectoryName(inputFile.Filename) + "\\" + "frame%05d.jpg ").ToString();

}

private static string Convert(MediaFile inputFile, MediaFile outputFile, ConversionOptions conversionOptions)
{
Expand Down Expand Up @@ -79,6 +92,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
16 changes: 15 additions & 1 deletion MediaToolkit src/MediaToolkit/Engine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,20 @@ 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);
}

#region Private method - Helpers

private void FFmpegEngine(EngineParameters engineParameters)
Expand Down Expand Up @@ -303,4 +317,4 @@ private void StartFFmpegProcess(EngineParameters engineParameters)
}
}
}
}
}
7 changes: 5 additions & 2 deletions MediaToolkit src/MediaToolkit/FFmpegTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ 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
}
}
}
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; }
}

}
}