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

Some improvement in performance and reliability #57

Merged
merged 16 commits into from
Nov 2, 2022
3 changes: 2 additions & 1 deletion appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"Settings": {
"MkvMergePath": "",
"FfmpegPath": "",
"SubsFolder": "./GenshinData/Subtitle"
"SubsFolder": "./GenshinData/Subtitle",
"SubsStyle": "Style: Default,{fontname},12,&H00FFFFFF,&H000000FF,&H00000000,&H00000000,0,0,0,0,100.0,100.0,0.0,0.0,1,0,0.5,2,10,10,14,1"
}
}
7 changes: 4 additions & 3 deletions src/Demuxer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,10 @@ private static ulong EncryptionKeyInFilename(string filename)

private static (ulong, bool) EncryptionKeyInBLK(string videoFilename)
{
if (!File.Exists("versions.json")) throw new FileNotFoundException("File versions.json couldn't be found in the folder of the tool.");
var versionsFilePath = Path.Combine(AppContext.BaseDirectory, "versions.json");
if (!File.Exists(versionsFilePath)) throw new FileNotFoundException("File versions.json couldn't be found in the folder of the tool.");
videoFilename = Path.GetFileNameWithoutExtension(videoFilename);
string jsonString = File.ReadAllText("versions.json");
string jsonString = File.ReadAllText(versionsFilePath);
VersionList? versions = JsonSerializer.Deserialize<VersionList>(jsonString, VersionJson.Default.VersionList);
if (versions?.list == null) throw new JsonException("Json content from versions.json is invalid or couldn't be parsed...");
Version? v = Array.Find(versions.list, x => (x.videos != null && x.videos.Contains(videoFilename)) || (x.videoGroups != null && Array.Exists(x.videoGroups, y => y.videos != null && y.videos.Contains(videoFilename))));
Expand All @@ -56,7 +57,7 @@ private static (ulong, bool) EncryptionKeyInBLK(string videoFilename)
if (v.videoGroups != null)
{
key = Array.Find(v.videoGroups, y => y.videos != null && y.videos.Contains(videoFilename))?.key ?? throw new KeyNotFoundException("Unable to find the second key in versions.json for " + videoFilename);
}
}
return (key, v.encAudio ?? false);
}

Expand Down
123 changes: 71 additions & 52 deletions src/FileTypes/ASS.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using System.Text.RegularExpressions;
using System.Text;
using System.Text.RegularExpressions;

namespace GICutscenes.FileTypes
{
internal class ASS
{
public static readonly string[] SubsExtensions = {".ass", ".srt", ".txt"};
public static readonly string[] SubsExtensions = { ".ass", ".srt", ".txt" };
private readonly string _srt;
private readonly string _fontname;
private readonly List<string> _dialogLines;
Expand Down Expand Up @@ -67,34 +68,52 @@ public void ParseSrt()
}
}

public string ConvertToAss() {

string filename = Path.ChangeExtension(_srt, ".ass");
string header =
@$"[Script Info]
; This is an Advanced Sub Station Alpha v4+ script.
ScriptType: v4.00+
Collisions: Normal
ScaledBorderAndShadow: yes
PlayDepth: 0

[V4+ Styles]
Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, Encoding
Style: Default,{_fontname},18,&H00FFFFFF,&H000000FF,&H00000000,&H00000000,0,0,0,0,100.0,100.0,0.0,0.0,1,0,0.5,2,10,10,20,1
public string ConvertToAss(string outputPath)
{
var sb = new StringBuilder();
sb.AppendLine("""
[Script Info]
; This is an Advanced Sub Station Alpha v4+ script.
ScriptType: v4.00+
Collisions: Normal
ScaledBorderAndShadow: yes
PlayDepth: 0

[V4+ Styles]
Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, Encoding
""");
if (string.IsNullOrWhiteSpace(Program.settings?.SubsStyle))
{
sb.AppendLine($"Style: Default,{_fontname},12,&H00FFFFFF,&H000000FF,&H00000000,&H00000000,0,0,0,0,100.0,100.0,0.0,0.0,1,0,0.5,2,10,10,14,1");
}
else
{
sb.AppendLine(Program.settings?.SubsStyle.Replace("{fontname}", _fontname));
}
sb.AppendLine("""

[Events]
Format: Layer, Start, End, Style, Actor, MarginL, MarginR, MarginV, Effect, Text
""");

[Events]
Format: Layer, Start, End, Style, Actor, MarginL, MarginR, MarginV, Effect, Text" + Environment.NewLine;
File.WriteAllText(filename, header);
string content = string.Join(Environment.NewLine, _dialogLines);
// Correcting styles
content = Regex.Replace(content, @"<([ubi])>", @"{\${1}1}");
content = Regex.Replace(content, @"</([ubi])>", @"{\${1}0}");
content = Regex.Replace(content, @"<font\s+color=""?#(\w{2})(\w{2})(\w{2})""?>", @"{\c&H$3$2$1&}");
content = Regex.Replace(content, @"</font>", "");
foreach (var dialogLine in _dialogLines)
{
if (!string.IsNullOrWhiteSpace(dialogLine))
{
string content = dialogLine;
content = Regex.Replace(content, @"<([ubi])>", @"{\${1}1}");
content = Regex.Replace(content, @"</([ubi])>", @"{\${1}0}");
content = Regex.Replace(content, @"<font\s+color=""?#(\w{2})(\w{2})(\w{2})""?>", @"{\c&H$3$2$1&}");
content = Regex.Replace(content, @"</font>", "");
sb.AppendLine(content);
}
}

File.AppendAllText(filename, content);
var filename = Path.Combine(outputPath, "Subs", Path.GetFileNameWithoutExtension(_srt) + ".ass");
Directory.CreateDirectory(Path.Combine(outputPath, "Subs"));
File.WriteAllText(filename, sb.ToString());
Console.WriteLine($"{_srt} converted to ASS");
File.Delete(_srt); // Can be commented if you don't want to delete the original
//File.Delete(_srt); // Can be commented if you don't want to delete the original
return filename;
}

Expand Down Expand Up @@ -122,30 +141,30 @@ public string ConvertToAss() {
return null;
}

public static void ConvertAllSrt(string subsFolder)
{
string? file = null;
//file = "ID/Cs_Inazuma_EQ4002207_ShikishogunRecalling_Boy_ID.txt";
if (file == null)
{
foreach (string langDir in Directory.EnumerateDirectories(subsFolder))
{
foreach (string srtFile in Directory.GetFiles(langDir, "*.txt"))
{
ASS newAss = new(srtFile, Path.GetDirectoryName(langDir) ?? "unk");
newAss.ParseSrt();
newAss.ConvertToAss();
}
}
}
else
{
ASS newAss = new(Path.Combine(subsFolder, file), Path.GetDirectoryName(file) ?? "unk");
newAss.ParseSrt();
newAss.ConvertToAss();
}

Environment.Exit(0);
}
//public static void ConvertAllSrt(string subsFolder)
//{
// string? file = null;
// //file = "ID/Cs_Inazuma_EQ4002207_ShikishogunRecalling_Boy_ID.txt";
// if (file == null)
// {
// foreach (string langDir in Directory.EnumerateDirectories(subsFolder))
// {
// foreach (string srtFile in Directory.GetFiles(langDir, "*.txt"))
// {
// ASS newAss = new(srtFile, Path.GetDirectoryName(langDir) ?? "unk");
// newAss.ParseSrt();
// newAss.ConvertToAss();
// }
// }
// }
// else
// {
// ASS newAss = new(Path.Combine(subsFolder, file), Path.GetDirectoryName(file) ?? "unk");
// newAss.ParseSrt();
// newAss.ConvertToAss();
// }

// Environment.Exit(0);
//}
}
}
87 changes: 44 additions & 43 deletions src/GICutscenes.csproj
Original file line number Diff line number Diff line change
@@ -1,46 +1,47 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<Version>0.4.1</Version>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<!--<RuntimeIdentifier>win-x64</RuntimeIdentifier>-->
<PublishTrimmed>true</PublishTrimmed>
<Authors>ToaHartor</Authors>
<RootNamespace>GICutscenes</RootNamespace>
<Description>A command line program playing with the cutscenes files (USM) from Genshin Impact.</Description>
<PackageLicenseExpression>GPL-3.0-only</PackageLicenseExpression>
<RepositoryUrl>https://github.com/ToaHartor/GI-cutscenes</RepositoryUrl>
<PropertyGroup>
<Version>0.4.1</Version>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<!--<RuntimeIdentifier>win-x64</RuntimeIdentifier>-->
<PublishTrimmed>true</PublishTrimmed>
<Authors>ToaHartor</Authors>
<RootNamespace>GICutscenes</RootNamespace>
<Description>A command line program playing with the cutscenes files (USM) from Genshin Impact.</Description>
<PackageLicenseExpression>GPL-3.0-only</PackageLicenseExpression>
<RepositoryUrl>https://github.com/ToaHartor/GI-cutscenes</RepositoryUrl>

<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<SatelliteResourceLanguages>en</SatelliteResourceLanguages>
<DebugType>embedded</DebugType>
<PackageReadmeFile>README.md</PackageReadmeFile>
<RepositoryType>git</RepositoryType>
<AnalysisLevel>latest-all</AnalysisLevel>
<EnableTrimAnalyzer>true</EnableTrimAnalyzer>
</PropertyGroup>
<Nullable>enable</Nullable>
<LangVersion>preview</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>
<SatelliteResourceLanguages>en</SatelliteResourceLanguages>
<DebugType>embedded</DebugType>
<PackageReadmeFile>README.md</PackageReadmeFile>
<RepositoryType>git</RepositoryType>
<AnalysisLevel>latest-all</AnalysisLevel>
<EnableTrimAnalyzer>true</EnableTrimAnalyzer>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<Optimize>False</Optimize>
<DebugType>full</DebugType>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<Optimize>False</Optimize>
<DebugType>full</DebugType>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<DebugType>full</DebugType>
<PublishSingleFile>true</PublishSingleFile>
<TieredCompilation>true</TieredCompilation>
<PublishReadyToRun>true</PublishReadyToRun>
<!--<RunAOTCompilation>true</RunAOTCompilation>-->
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="7.0.0-preview.5.22301.12" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="7.0.0-preview.5.22301.12" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="7.0.0-preview.5.22301.12" />
<PackageReference Include="System.CommandLine" Version="2.0.0-beta3.22114.1" />
</ItemGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<DebugType>full</DebugType>
<PublishSingleFile>true</PublishSingleFile>
<TieredCompilation>true</TieredCompilation>
<PublishReadyToRun>true</PublishReadyToRun>
<!--<RunAOTCompilation>true</RunAOTCompilation>-->
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="7.0.0-preview.5.22301.12" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="7.0.0-preview.5.22301.12" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="7.0.0-preview.5.22301.12" />
<PackageReference Include="System.CommandLine" Version="2.0.0-beta3.22114.1" />
</ItemGroup>

<ItemGroup>
<Content Include="../appsettings.json">
Expand All @@ -55,10 +56,10 @@
</ItemGroup>

<ItemGroup>
<None Include="..\.editorconfig" Link=".editorconfig" />
<None Include="..\README.md">
<Pack>True</Pack>
<PackagePath>\</PackagePath>
</None>
<None Include="..\.editorconfig" Link=".editorconfig" />
<None Include="..\README.md">
<Pack>True</Pack>
<PackagePath>\</PackagePath>
</None>
</ItemGroup>
</Project>
9 changes: 9 additions & 0 deletions src/Mergers/FFMPEG.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,5 +107,14 @@ public void Merge()
Process process = Process.Start(_ffmpeg, _command);
process.WaitForExit();
}

public void Merge(string audioFormat, string videoFormat)
{
_command += string.Join(" ", _inputOptions) + string.Join(" ", _mapOptions) + string.Join(" ", _metadataOptions);
_command += $" -c:a \"{(string.IsNullOrWhiteSpace(audioFormat) ? "copy" : audioFormat)}\" -c:v \"{(string.IsNullOrWhiteSpace(videoFormat) ? "copy" : videoFormat)}\" \"{_output}\"";
//Console.WriteLine(_ffmpeg + _command);
Process process = Process.Start(_ffmpeg, _command);
process.WaitForExit();
}
}
}
2 changes: 2 additions & 0 deletions src/Mergers/Merger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,7 @@ internal interface Merger

void Merge();

void Merge(string audioFormat, string videoFormat) { }

}
}
Loading