Skip to content

Commit

Permalink
Removed Code to create Directory as MKVMerge handles output Directory…
Browse files Browse the repository at this point in the history
… creation.
  • Loading branch information
DineshSolanki committed Sep 3, 2020
1 parent b25e1b8 commit 6893b60
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 51 deletions.
1 change: 1 addition & 0 deletions BatchMuxer_SubEd_Console/BatchMuxer_SubEd_Console.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.0</TargetFramework>
<AssemblyName>BatchMuxer_Subtitle_Console</AssemblyName>
</PropertyGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion BatchMuxer_SubEd_Console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ internal class Program
/// <param name="args">The directory name</param>
private static void Main(string[] args)
{

DirectoryInfo folder = new DirectoryInfo(args[0]);

IConfiguration config = new ConfigurationBuilder()
.AddJsonFile("appsettings.json", true, true)
.Build();
Expand Down
93 changes: 43 additions & 50 deletions BatchMuxer_SubEd_Console/classes/util.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,34 @@ namespace BatchMuxer_SubEd_Console.Classes
public static class util
{
/// <summary>
/// Renames other extension to MKV
/// Deletes original Media files and move files from "muxed" to original folder.
/// </summary>
/// <param name="fi">File info array containing files to rename</param>
/// <returns></returns>
public static bool RenameFile(FileInfo[] fi)
/// <param name="files">Files to move</param>
/// <param name="path">Full folder path</param>
public static void DeleteAndMove(FileInfo[] files, string path)
{
bool hasRenamed = false;
foreach (var fl in fi)
foreach (var fl in files)
{
if (fl.Extension != ".mkv")
var subtitle = fl.Name.Replace(fl.Extension, ".srt");
var subtitlePath = fl.FullName.Replace(fl.Extension, ".srt");
if (File.Exists(path + @"\" + subtitle) && File.Exists(path + @"\muxed\" + fl.Name))
{
File.Move(fl.FullName, fl.FullName.Replace(fl.Extension, ".mkv")); //caution!
hasRenamed = true;
File.Delete(fl.FullName);
File.Delete(subtitlePath);
File.Move(path + @"\muxed\" + fl.Name, fl.FullName);
}
}

return hasRenamed;
if (IsDirectoryEmpty(path + @"\muxed"))
Directory.Delete(path + @"\muxed");
}

/// <summary>
/// Checks if a diretory is empty by enumerating through path
/// </summary>
/// <param name="path">Directory to check</param>
/// <returns></returns>
public static bool IsDirectoryEmpty(string path) => !Directory.EnumerateFileSystemEntries(path).Any();

/// <summary>
/// Muxes file using MKVMerge
/// </summary>
Expand All @@ -45,22 +54,18 @@ public static void ProcessFile(FileInfo fl, string path, string mkvmergePath)
{
File.Move(fl.FullName, fl.FullName.Replace(fl.Extension, ".mkv"));//caution!
}
if(!Directory.Exists(Path.Combine(path + "muxed")))
{
Directory.CreateDirectory(Path.Combine(path + "muxed"));
}
if (File.Exists(subtitle.Replace("\"", "")) &&
!File.Exists(Path.Combine(path + "muxed" + fl.Name)))
{
string output = @$"""{Path.Combine(path,"muxed",fl.Name)}""";
string output = @$"""{Path.Combine(path, "muxed", fl.Name)}""";
string mkvPath = ThisOperatingSystem.IsWindows() ? Path.Combine(mkvmergePath, "mkvmerge.exe") : "mkvmerge";
ProcessStartInfo startInfo = new ProcessStartInfo(mkvPath)
{
WindowStyle = ProcessWindowStyle.Hidden,
//WorkingDirectory = mkvmergePath,
Arguments = $@"-o {output} --default-track 0:yes {subtitle} {fileName}",
CreateNoWindow = true,
RedirectStandardOutput=true,
RedirectStandardOutput = true,
};
try
{
Expand All @@ -70,7 +75,7 @@ public static void ProcessFile(FileInfo fl, string path, string mkvmergePath)
};
oProcess.Start();
oProcess.WaitForExit();
var r=oProcess.StandardOutput;
var r = oProcess.StandardOutput;
oProcess.Dispose();
}
catch (Exception ex)
Expand All @@ -81,6 +86,25 @@ public static void ProcessFile(FileInfo fl, string path, string mkvmergePath)
}
}

/// <summary>
/// Renames other extension to MKV
/// </summary>
/// <param name="fi">File info array containing files to rename</param>
/// <returns></returns>
public static bool RenameFile(FileInfo[] fi)
{
bool hasRenamed = false;
foreach (var fl in fi)
{
if (fl.Extension != ".mkv")
{
File.Move(fl.FullName, fl.FullName.Replace(fl.Extension, ".mkv")); //caution!
hasRenamed = true;
}
}

return hasRenamed;
}
/// <summary>
/// Update appsetting.json
/// </summary>
Expand All @@ -96,36 +120,5 @@ public static void WriteToConfig(string key, string value)
string output = Newtonsoft.Json.JsonConvert.SerializeObject(jsonObj, Formatting.Indented);
File.WriteAllText("appsettings.json", output);
}

/// <summary>
/// Deletes original Media files and move files from "muxed" to original folder.
/// </summary>
/// <param name="files">Files to move</param>
/// <param name="path">Full folder path</param>
public static void DeleteAndMove(FileInfo[] files, string path)
{
foreach (var fl in files)
{
var subtitle = fl.Name.Replace(fl.Extension, ".srt");
var subtitlePath = fl.FullName.Replace(fl.Extension, ".srt");
if (File.Exists(path + @"\" + subtitle) && File.Exists(path + @"\muxed\" + fl.Name))
{
File.Delete(fl.FullName);
File.Delete(subtitlePath);
File.Move(path + @"\muxed\" + fl.Name, fl.FullName);
}
}
if (IsDirectoryEmpty(path + @"\muxed"))
Directory.Delete(path + @"\muxed");
}

/// <summary>
/// Checks if a diretory is empty by enumerating through path
/// </summary>
/// <param name="path">Directory to check</param>
/// <returns></returns>
public static bool IsDirectoryEmpty(string path) => !Directory.EnumerateFileSystemEntries(path).Any();
}
}
//TODO: Parse JSON paths
//FIX directory for linux
}

0 comments on commit 6893b60

Please sign in to comment.