Skip to content

Commit

Permalink
Add a CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
Lord-Giganticus committed Sep 7, 2021
1 parent 5017d88 commit 58dd6be
Show file tree
Hide file tree
Showing 12 changed files with 526 additions and 4 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -346,3 +346,5 @@ healthchecksdb
dist/
build/
*.spec
/THP-Converter-CS-CLI/Resources/THPConv.exe
/THP-Converter-CS-CLI/Resources/dsptool.dll
22 changes: 22 additions & 0 deletions THP-Converter-CS-CLI/Classes/Extensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace THP_Converter_CS_CLI.Classes
{
static class Extensions
{
internal static string ReplaceSpacesWithUnderscore(this string str) => str.Replace(' ', '_');

internal static List<T> ToList<T>(this T[] arr)
{
var l = new List<T>();
foreach (var a in arr)
l.Add(a);
return l;
}
}
}
80 changes: 80 additions & 0 deletions THP-Converter-CS-CLI/Classes/MP4Video.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Threading.Tasks;
using static THP_Converter_CS_CLI.Properties.Resources;

namespace THP_Converter_CS_CLI.Classes
{
class MP4Video
{
public MP4Video(ushort width, ushort height, FileInfo inFile, FileInfo outFile, double rate = 29.97, bool useAudio = false)
{
Width = width;
Height = height;
Rate = rate > 1.0 ? (rate < 59.94 ? rate : 59.94) : 1.0;
UseAudio = useAudio;
InFile = inFile.Extension is ".mp4" ? inFile : throw new Exception("The inFile paramater must be a mp4 file.");
OutFile = outFile.Extension is ".thp" ? outFile : new($"{outFile.FullName[0..outFile.FullName.LastIndexOf(".")]}.thp");
}

protected MP4Video()
{

}

protected ushort Width { get; set; }

protected ushort Height { get; set; }

protected double Rate { get; set; } = 29.97;

protected bool UseAudio { get; set; }

protected FileInfo InFile { get; set; }

protected FileInfo OutFile { get; set; }

public void Convert()
{
Directory.SetCurrentDirectory(ProgramData.ProgramDir.FullName);
File.WriteAllBytes("ffmpeg.exe", ffmpeg);
File.Move(InFile.FullName, "video.mp4");
Directory.CreateDirectory("temp");
Process.Start(startInfo: new()
{
CreateNoWindow = true,
UseShellExecute = false,
WindowStyle = ProcessWindowStyle.Hidden,
FileName = "cmd.exe",
Arguments = $"/c ffmpeg.exe -i video.mp4 -r {Rate} -vf scale={Width}:{Height} temp\\frame%03d.jpg"
}).WaitForExit();
File.Delete("ffmpeg.exe");
if (UseAudio)
{
File.WriteAllBytes("mplayer.exe", mplayer);
Process.Start(startInfo: new()
{
FileName = "cmd.exe",
Arguments = "/c mplayer.exe -ao pcm:file=temp.wav video.mp4"
}).WaitForExit();
File.Delete("mplayer.exe");
}
File.WriteAllBytes("THPConv.exe", THPConv);
File.WriteAllBytes("dsptool.dll", dsptool);
Process.Start(startInfo: new()
{
CreateNoWindow = true,
UseShellExecute = false,
WindowStyle = ProcessWindowStyle.Hidden,
FileName = "cmd.exe",
Arguments = UseAudio ? $"/c thpconv.exe -j temp/*.jpg -r {Rate} -s temp.wav -d output.thp" : $"/c thpconv.exe -j temp/*.jpg -r {Rate} -d output.thp"
}).WaitForExit();
File.Move("output.thp", OutFile.FullName);
Console.WriteLine($"Converted {InFile.Name} to {OutFile.Name}");
}
}
}
14 changes: 14 additions & 0 deletions THP-Converter-CS-CLI/Classes/ProgramData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Text;
using System.Threading.Tasks;

namespace THP_Converter_CS_CLI.Classes
{
static class ProgramData
{
internal static DirectoryInfo ProgramDir => new(AppDomain.CurrentDomain.BaseDirectory);
}
}
37 changes: 37 additions & 0 deletions THP-Converter-CS-CLI/Classes/THP.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Threading.Tasks;
using FFmpeg.NET;
using static THP_Converter_CS_CLI.Properties.Resources;

namespace THP_Converter_CS_CLI.Classes
{
class THP
{
protected FileInfo InFile { get; set; }

protected FileInfo OutFile { get; set; }

public THP(FileInfo infile, FileInfo outfile)
{
InFile = infile.Extension is ".thp"? infile : throw new Exception("The infile paramater requires a thp video.");
OutFile = outfile.Extension is ".mp4" ? outfile : throw new Exception("The outfile paramater requires a mp4 video.");
}

public async Task<MediaFile> Convert()
{
Directory.SetCurrentDirectory(ProgramData.ProgramDir.FullName);
await File.WriteAllBytesAsync("ffmpeg.exe", ffmpeg);
var engine = new Engine("ffmpeg.exe");
var infile = new InputFile(InFile);
var outfile = new OutputFile(OutFile);
var r = await engine.ConvertAsync(infile, outfile);
File.Delete("ffmpeg.exe");
return r;
}
}
}
101 changes: 101 additions & 0 deletions THP-Converter-CS-CLI/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
using System;
using System.IO;
using System.Collections.Generic;
using THP_Converter_CS_CLI.Classes;

namespace THP_Converter_CS_CLI
{
static class Program
{
static double Rate { get; set; } = 29.97;

static ushort Width { get; set; } = 0;

static ushort Height { get; set; } = 0;

static bool UseAudio { get; set; } = false;

static FileInfo OutFile { get; set; }

static FileInfo InFile { get; set; }

static void Main(string[] args)
{
for (int i = 0; i < args.Length; i++)
switch (args[i])
{
case "-i":
case "--in":
if (i + 1 > args.Length) throw new Exception("The paramerters were malformed.");
if (!File.Exists(args[i + 1])) throw new FileNotFoundException("Input file must exist.");
InFile = new(args[i + 1]);
i++;
break;
case "-r":
case "--rate":
if (i + 1 > args.Length) throw new Exception("The parameters were malformed.");
Rate = double.Parse(args[i + 1]);
i++;
break;
case "-wi":
case "--width":
if (i + 1 > args.Length) throw new Exception("The parameters were malformed.");
Width = ushort.Parse(args[i + 1]);
i++;
break;
case "-he":
case "--height":
if (i + 1 > args.Length) throw new Exception("The parameters were malformed.");
Height = ushort.Parse(args[i + 1]);
i++;
break;
case "-a":
case "--audio":
if (i + 1 > args.Length)
throw new Exception("The parameters were malformed.");
UseAudio = true;
i++;
break;
case "-h":
case "--help":
ShowHelp();
break;
case "-o":
case "--out":
if (i + 1 > args.Length)
throw new Exception("The parameters were malformed.");
OutFile = new(args[i + 1]);
i++;
break;
}
if (InFile.Extension is ".thp")
{
if (OutFile.Extension is not ".mp4") throw new Exception("Outfile needs to be a mp4 if the Inputfile is a thp.");
new THP(InFile, OutFile).Convert().GetAwaiter().GetResult();
Console.WriteLine($"Converted {InFile.Name} to {OutFile.Name}.");
} else if (InFile.Extension is ".mp4")
{
if (OutFile.Extension is not ".thp") throw new Exception("Outfile needs to be a thp if the Inputfile is a mp4.");
new MP4Video(Width is 0 ? (ushort)640 : Width, Height is 0 ? (ushort)368 : Height, InFile, OutFile, Rate is 0 ? 29.97 : Rate, UseAudio).Convert();
}
}

static void ShowHelp()
{
string[] lines =
{
"THP-Converter-CS-CLI",
"Created by Lord-Giganticus",
"Command Usage:",
"-i/--in (The input file to use)",
"-r/--rate (The frame rate of a new THP Video. Defaults to 29.97)",
"-wi/--width (The width of a new THP Video. Defauts to 640)",
"-he/--height (The height of a new THP Video. Defaults to 368)",
"-a/--audio (Use audio when making a new THP Video. Defaults to False)",
"-h/--help (Shows this)",
"-o/--out (The file to write to)"
};
Console.WriteLine(string.Join("\n", lines));
}
}
}
103 changes: 103 additions & 0 deletions THP-Converter-CS-CLI/Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 58dd6be

Please sign in to comment.