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

添加自定义字幕后缀 #55

Merged
merged 5 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions SubRenamer/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public partial class Config
public bool Backup { get; set; } = true;
public bool UpdateCheck { get; set; } = true;
public bool KeepLangExt { get; set; } = false;
public string CustomLangExt { get; set; } = "";
public string VideoExtAppend { get; set; } = "";
public string SubtitleExtAppend { get; set; } = "";

Expand Down
12 changes: 10 additions & 2 deletions SubRenamer/Services/RenameService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,25 @@ public void UpdateRenameTaskList(IReadOnlyList<MatchItem> matchList, Collection<
// @file https://github.com/qwqcode/SubRenamer/blob/main/SubRenamer.Tests/MatcherTests/MergeSameKeysItemsTests.cs
var hasDuplicateKey = matchList.GroupBy(x => x.Key).Any(g => g.Count() > 1);
var keepLangExt = Config.Get().KeepLangExt || hasDuplicateKey;
var customLangExt = Config.Get().CustomLangExt.Trim();
var hasCustomLangExt = !string.IsNullOrEmpty(customLangExt);

foreach (var item in matchList)
{
if (string.IsNullOrEmpty(item.Subtitle) || string.IsNullOrEmpty(item.Video)) continue;

// 提取字幕文件语言后缀
// 字幕文件语言后缀
var subSuffix = "";
if (keepLangExt) {
if (keepLangExt)
{
// 从原始字幕文件名中提取语言后缀
var subSplit = Path.GetFileNameWithoutExtension(item.Subtitle).Split('.');
if (subSplit.Length > 1) subSuffix = "." + subSplit[^1];
}
if (hasCustomLangExt) {
// 自定义追加的后缀
subSuffix += "." + customLangExt.TrimStart('.');
}

// 拼接新的字幕文件路径
var videoFolder = Path.GetDirectoryName(item.Video) ?? "";
Expand Down
24 changes: 23 additions & 1 deletion SubRenamer/ViewModels/SettingsViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public partial class SettingsViewModel : ViewModelBase
private bool _backupEnabled = Config.Get().Backup;
private bool _updateCheckEnabled = Config.Get().UpdateCheck;
private bool _keepLangExt = Config.Get().KeepLangExt;
private bool _customLangExtEnabled = !string.IsNullOrEmpty(Config.Get().CustomLangExt);
private string _customLangExt = Config.Get().CustomLangExt;
private string _videoExtAppend = Config.Get().VideoExtAppend;
private string _subtitleExtAppend = Config.Get().SubtitleExtAppend;

Expand Down Expand Up @@ -52,6 +54,26 @@ public bool KeepLangExt
}
}

public bool CustomLangExtEnabled
{
get => _customLangExtEnabled;
set
{
if (!value) CustomLangExt = "";
else KeepLangExt = false;
SetProperty(ref _customLangExtEnabled, value);
}
}

public string CustomLangExt
{
get => _customLangExt;
set
{
Config.Get().CustomLangExt = value;
SetProperty(ref _customLangExt, value);
}
}
public string VideoExtAppend
{
get => _videoExtAppend;
Expand All @@ -61,7 +83,7 @@ public string VideoExtAppend
SetProperty(ref _videoExtAppend, value);
}
}

public string SubtitleExtAppend
{
get => _subtitleExtAppend;
Expand Down
11 changes: 8 additions & 3 deletions SubRenamer/Views/SettingsWindow.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,13 @@
<Label Foreground="Gray" Margin="30 0 30 10" FontSize="13" Padding="0">程序会复制字幕到视频目录,若目录相同则备份到 SubBackup</Label>
<!-- <CheckBox>确认删除对话框</CheckBox> -->
<!-- <CheckBox>显示文件完整路径</CheckBox> -->
<CheckBox IsChecked="{Binding KeepLangExt}">保留语言后缀</CheckBox>
<CheckBox IsChecked="{Binding KeepLangExt}" IsEnabled="{Binding !CustomLangExtEnabled}">保留语言后缀</CheckBox>
<Label Foreground="Gray" Margin="30 0 30 10" FontSize="13" Padding="0">例如 "subtitle.sc.srt" 保留语言后缀 ".sc"</Label>
<CheckBox IsChecked="{Binding UpdateCheckEnabled}">程序升级检查</CheckBox>
<CheckBox IsChecked="{Binding CustomLangExtEnabled}">追加字幕后缀</CheckBox>
<StackPanel Margin="30 0 0 0">
<Label Foreground="Gray" FontSize="13" Padding="0" Margin="0 0 0 10">在字幕文件扩展名前添加自定义后缀</Label>
<TextBox IsEnabled="{Binding CustomLangExtEnabled}" Watermark="输入后缀,例如:zh-Hans" Text="{Binding CustomLangExt}" />
</StackPanel>
<Border Height="15" />
<Grid HorizontalAlignment="Stretch" ColumnDefinitions="*,10,*">
<StackPanel Grid.Column="0">
Expand All @@ -53,8 +57,9 @@
<Label Foreground="Gray" Margin="0 0 0 10" Padding="0" FontSize="13">用于识别字幕文件</Label>
<TextBox Watermark="以逗号分隔" Text="{Binding SubtitleExtAppend}" />
</StackPanel>

</Grid>

<Border Height="30" />
<TextBlock TextWrapping="Wrap" FontSize="13" LineHeight="24" Foreground="#5f6b7c">
|´・ω・)ノ 嗨!这是开源程序<LineBreak />
Expand Down