Skip to content

Commit

Permalink
Added external subtitles support
Browse files Browse the repository at this point in the history
  • Loading branch information
Mantas-2155X committed Jun 29, 2024
1 parent b208fc5 commit e7c40f4
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 10 deletions.
4 changes: 2 additions & 2 deletions HardSubberGUI/HardSubberGUI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
<TrimMode>copyused</TrimMode>
<BuiltInComInteropSupport>true</BuiltInComInteropSupport>
<PackageVersion>1.0.1</PackageVersion>
<AssemblyVersion>1.5.2.0</AssemblyVersion>
<FileVersion>1.5.2.0</FileVersion>
<AssemblyVersion>1.6.0.0</AssemblyVersion>
<FileVersion>1.6.0.0</FileVersion>
</PropertyGroup>
<ItemGroup>
<AvaloniaResource Include="Assets\**" />
Expand Down
3 changes: 3 additions & 0 deletions HardSubberGUI/Structs/SConversionOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public struct SConversionOptions
public bool FastStart;
public bool Resize;
public bool CloseAfterwards;
public bool ExternalSubs;

public ESupportedFormat Format;

Expand All @@ -46,6 +47,7 @@ public static SConversionOptions ReadFromUI()
options.UsePGS = MainWindow.Instance.PGSSubsControl.IsChecked.Value;

Check warning on line 47 in HardSubberGUI/Structs/SConversionOptions.cs

View workflow job for this annotation

GitHub Actions / build-linux

Nullable value type may be null.

Check warning on line 47 in HardSubberGUI/Structs/SConversionOptions.cs

View workflow job for this annotation

GitHub Actions / build-windows

Nullable value type may be null.
options.Resize = MainWindow.Instance.ApplyResizeControl.IsChecked.Value;

Check warning on line 48 in HardSubberGUI/Structs/SConversionOptions.cs

View workflow job for this annotation

GitHub Actions / build-linux

Nullable value type may be null.

Check warning on line 48 in HardSubberGUI/Structs/SConversionOptions.cs

View workflow job for this annotation

GitHub Actions / build-windows

Nullable value type may be null.
options.CloseAfterwards = MainWindow.Instance.ExitAfterwardsControl.IsChecked.Value;

Check warning on line 49 in HardSubberGUI/Structs/SConversionOptions.cs

View workflow job for this annotation

GitHub Actions / build-linux

Nullable value type may be null.

Check warning on line 49 in HardSubberGUI/Structs/SConversionOptions.cs

View workflow job for this annotation

GitHub Actions / build-windows

Nullable value type may be null.
options.ExternalSubs = MainWindow.Instance.ExternalSubsControl.IsChecked.Value;
options.ResizeResolution = new[] { (int)MainWindow.Instance.ResolutionOverrideWidthControl.Value, (int)MainWindow.Instance.ResolutionOverrideHeightControl.Value };
options.SubtitleIndex = (int)MainWindow.Instance.SubtitleIndexControl.Value;
options.AudioIndex = (int)MainWindow.Instance.AudioIndexControl.Value;
Expand All @@ -69,6 +71,7 @@ public void ApplyToUI()
MainWindow.Instance.PGSSubsControl.IsChecked = UsePGS;
MainWindow.Instance.ApplyResizeControl.IsChecked = Resize;
MainWindow.Instance.ExitAfterwardsControl.IsChecked = CloseAfterwards;
MainWindow.Instance.ExternalSubsControl.IsChecked = ExternalSubs;
MainWindow.Instance.ResolutionOverrideWidthControl.Value = ResizeResolution[0];
MainWindow.Instance.ResolutionOverrideHeightControl.Value = ResizeResolution[1];
MainWindow.Instance.SubtitleIndexControl.Value = SubtitleIndex;
Expand Down
34 changes: 29 additions & 5 deletions HardSubberGUI/Tools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -256,11 +256,35 @@ public static void ActFile(string file, SConversionOptions conversionOptions, in

process.StartInfo.Arguments += $"-i '{info.FullName}' ";

var subsFilter = "";
var streamIndex = "";

var scaleString = conversionOptions.Resize ? $"scale={conversionOptions.ResizeResolution[0]}:{conversionOptions.ResizeResolution[1]}," : "";

var escaped = info.FullName;
escaped = !OSTools.IsWindows ? FileTools.Escape.Aggregate(info.FullName, (current, str) => current.Replace(str, "\\\\\\" + str)) : FileTools.EscapeWindowsString(escaped);

if (conversionOptions.ExternalSubs)
{
var newPath = escaped[..^info.Extension.Length];

if (File.Exists(newPath + ".srt"))
{
escaped = newPath + ".srt";
subsFilter = "subtitles";
}
else if (File.Exists(newPath + ".ass"))
{
escaped = newPath + ".ass";
subsFilter = "ass";
}
}
else
{
subsFilter = "subtitles";
streamIndex = $":stream_index={conversionOptions.SubtitleIndex}";
}

if (conversionOptions.UseHWAccel)
{
switch (OSTools.CurrentGPU)
Expand All @@ -270,7 +294,7 @@ public static void ActFile(string file, SConversionOptions conversionOptions, in
if (OSTools.IsWindows)
{
if (conversionOptions.BurnSubsAndAudio)
process.StartInfo.Arguments += $"-filter_complex {scaleString}subtitles={escaped}:stream_index={conversionOptions.SubtitleIndex},format=nv12 ";
process.StartInfo.Arguments += $"-filter_complex {scaleString}{subsFilter}={escaped}{streamIndex},format=nv12 ";
else
process.StartInfo.Arguments += $"-filter_complex {scaleString}format=nv12 ";

Expand All @@ -279,7 +303,7 @@ public static void ActFile(string file, SConversionOptions conversionOptions, in
else
{
if (conversionOptions.BurnSubsAndAudio)
process.StartInfo.Arguments += $"-filter_complex {scaleString}subtitles={escaped}:stream_index={conversionOptions.SubtitleIndex},format=nv12,hwupload ";
process.StartInfo.Arguments += $"-filter_complex {scaleString}{subsFilter}={escaped}{streamIndex},format=nv12,hwupload ";
else
process.StartInfo.Arguments += $"-filter_complex {scaleString}format=nv12,hwupload ";

Expand All @@ -290,7 +314,7 @@ public static void ActFile(string file, SConversionOptions conversionOptions, in
case EGPU.NVIDIA:
{
if (conversionOptions.BurnSubsAndAudio)
process.StartInfo.Arguments += $"-filter_complex {scaleString}subtitles={escaped}:stream_index={conversionOptions.SubtitleIndex},format=nv12,hwupload_cuda ";
process.StartInfo.Arguments += $"-filter_complex {scaleString}{subsFilter}={escaped}{streamIndex},format=nv12,hwupload_cuda ";
else
process.StartInfo.Arguments += $"-filter_complex {scaleString}format=nv12,hwupload_cuda ";

Expand All @@ -310,7 +334,7 @@ public static void ActFile(string file, SConversionOptions conversionOptions, in
if (conversionOptions.UsePGS)
process.StartInfo.Arguments += $"-filter_complex [0:v][0:s:{conversionOptions.SubtitleIndex}]overlay[v] -map [v] ";
else
process.StartInfo.Arguments += $"-filter_complex {scaleString}subtitles={escaped}:stream_index={conversionOptions.SubtitleIndex} ";
process.StartInfo.Arguments += $"-filter_complex {scaleString}{subsFilter}={escaped}:stream_index={conversionOptions.SubtitleIndex} ";
}

process.StartInfo.Arguments += "-c:v libx264 ";
Expand Down
2 changes: 2 additions & 0 deletions HardSubberGUI/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public class MainWindowViewModel : ViewModelBase
public static string ApplySubsTooltip => "Apply subtitles and audio";
public static string ApplyResizeTooltip => "Resize the video\nHardware Acceleration is not used for this";
public static string AACTooltip => "Convert audio codec to AAC\nCan fix not loading in cytu.be";
public static string ExternalSubsTooltip => "Use external subtitles\nMust be an .ass or .srt file next to the video file with the same name";

#endregion

Expand All @@ -61,6 +62,7 @@ public class MainWindowViewModel : ViewModelBase
public static string Resize => "Resize Video";
public static string Colorspace => "Reset Colorspace";
public static string ExitAfterwards => "Close after conversion";
public static string ExternalSubs => "External Subtitles";

#endregion

Expand Down
8 changes: 5 additions & 3 deletions HardSubberGUI/Views/MainWindow.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,13 @@

<ToggleSwitch Name="PGSSubsControl" IsChecked="False" Content="{Binding PGSSubs}" PropertyChanged="PGSSubsControl_OnPropertyChanged"
Grid.Row="3" Grid.Column="0" ToolTip.Tip="{Binding PGSSubsTooltip}"/>

<ToggleSwitch Name="HardwareAccelerationControl" IsEnabled = "False" Content="{Binding HardwareAcceleration}"
Grid.Row="4" Grid.Column="0" ToolTip.Tip="{Binding HardwareAccelerationTooltip}"/>
<ToggleSwitch Name="ExitAfterwardsControl" Content="{Binding ExitAfterwards}"
Grid.Row="3" Grid.Column="1" ToolTip.Tip="{Binding ExitAfterwardsTooltip}"/>

<ToggleSwitch Name="HardwareAccelerationControl" IsEnabled = "False" Content="{Binding HardwareAcceleration}"
Grid.Row="4" Grid.Column="0" ToolTip.Tip="{Binding HardwareAccelerationTooltip}"/>
<ToggleSwitch Name="ExternalSubsControl" IsEnabled = "False" Content="{Binding ExternalSubs}" PropertyChanged="ExternalSubsControl_OnPropertyChanged"
Grid.Row="4" Grid.Column="1" ToolTip.Tip="{Binding ExternalSubsTooltip}"/>
</Grid>
</Border>
</Panel>
Expand Down
18 changes: 18 additions & 0 deletions HardSubberGUI/Views/MainWindow.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,24 @@ private void PGSSubsControl_OnPropertyChanged(object? sender, AvaloniaPropertyCh

if (!ApplyResizeControl.IsEnabled)
ApplyResizeControl.IsChecked = false;

ExternalSubsControl.IsEnabled = !(bool)PGSSubsControl.IsChecked!;

if (!ExternalSubsControl.IsEnabled)
ExternalSubsControl.IsChecked = false;
}

private void ExternalSubsControl_OnPropertyChanged(object? sender, AvaloniaPropertyChangedEventArgs e)
{
if (!initialized)
return;

PGSSubsControl.IsEnabled = !(bool)ExternalSubsControl.IsChecked!;

if (!PGSSubsControl.IsEnabled)
PGSSubsControl.IsChecked = false;

SubtitleIndexControl.IsEnabled = !(bool)ExternalSubsControl.IsChecked!;
}

public void BackgroundTasks()
Expand Down
Binary file modified image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit e7c40f4

Please sign in to comment.