-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Implement MP4 and ffmpeg converter
- Loading branch information
Showing
6 changed files
with
161 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
namespace PlayMobic; | ||
|
||
using System.Diagnostics; | ||
using System.Text; | ||
using Yarhl.FileFormat; | ||
using Yarhl.FileSystem; | ||
using Yarhl.IO; | ||
|
||
public class FfmpegConverter : IConverter<NodeContainerFormat, BinaryFormat> | ||
{ | ||
private readonly FfmpegConverterParameters parameters; | ||
|
||
public FfmpegConverter(FfmpegConverterParameters parameters) | ||
{ | ||
this.parameters = parameters; | ||
} | ||
|
||
public BinaryFormat Convert(NodeContainerFormat source) | ||
{ | ||
ArgumentNullException.ThrowIfNull(source); | ||
|
||
var process = new Process(); | ||
process.StartInfo.FileName = parameters.ExecutablePath; | ||
process.StartInfo.Arguments = GetArguments(); | ||
process.StartInfo.UseShellExecute = false; | ||
process.StartInfo.CreateNoWindow = true; | ||
|
||
_ = process.Start(); | ||
process.WaitForExit(); | ||
|
||
if (process.ExitCode != 0) { | ||
throw new FormatException($"Error running: {parameters.ExecutablePath} {process.StartInfo.Arguments}"); | ||
} | ||
|
||
return new BinaryFormat(DataStreamFactory.FromFile(parameters.OutputPath, FileOpenMode.ReadWrite)); | ||
} | ||
|
||
private string GetArguments() | ||
{ | ||
var arguments = new StringBuilder(); | ||
|
||
var videoInfo = parameters.VideoInfo; | ||
if (videoInfo.AudioChannelsCount > 0) { | ||
_ = arguments.AppendFormat( | ||
"-f s16le -channel_layout {0} -ar {1} -ac {2} -i {3} ", | ||
videoInfo.AudioChannelsCount > 1 ? "stereo" : "mono", | ||
videoInfo.AudioFrequency, | ||
videoInfo.AudioChannelsCount, | ||
parameters.RawAudioPath); | ||
} | ||
|
||
_ = arguments.AppendFormat( | ||
"-f rawvideo -pix_fmt yuv420p -r {0:F1} -s {1}x{2} -i {3} ", | ||
videoInfo.FramesPerSecond, | ||
videoInfo.Width, | ||
videoInfo.Height, | ||
parameters.RawVideoPath); | ||
_ = arguments.AppendFormat( | ||
"-y -hide_banner -ac {0} {1}", | ||
videoInfo.AudioChannelsCount, | ||
parameters.OutputPath); | ||
|
||
return arguments.ToString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
namespace PlayMobic; | ||
|
||
using PlayMobic.Containers.Mods; | ||
|
||
public record FfmpegConverterParameters( | ||
string ExecutablePath, | ||
string RawVideoPath, | ||
string RawAudioPath, | ||
string OutputPath, | ||
ModsInfo VideoInfo); |