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

Feature:将改名后的字幕文件复制到视频路径下 #47

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
6 changes: 6 additions & 0 deletions SubRenamer/Common/RenameStrategy.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace SubRenamer.Common;
public enum RenameStrategy
{
Copy, // Copy the renamed subtitle file to the folder where the video is located
Directly // Directly modify the file name of the selected subtitle file
}
3 changes: 2 additions & 1 deletion SubRenamer/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ public partial class Config
public ThemeMode ThemeMode { get; set; } = ThemeMode.Default;
public bool Backup { get; set; } = true;
public bool UpdateCheck { get; set; } = true;


public RenameStrategy RenameStrategy { get; set; } = RenameStrategy.Copy;
public string VideoExtAppend { get; set; } = "";
public string SubtitleExtAppend { get; set; } = "";

Expand Down
7 changes: 6 additions & 1 deletion SubRenamer/Helper/FileHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ public static void RenameFile(string originPath, string alterPath)
{
File.Move(originPath, alterPath);
}


public static void CopyFile(string originPath, string alterPath)
{
File.Copy(originPath, alterPath);
}

public static void BackupFile(string path, string folderName = "SubtitleBackup")
{
// create new 'SubtitleBackup' directory if not exists
Expand Down
2 changes: 1 addition & 1 deletion SubRenamer/Matcher/Diff.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ private static string FindCommonSuffix(string prefix, string a, string b)

return "";

bool IsSymbol(char c) => !char.IsLetterOrDigit(c) && c != ' '; // skip whitespace
bool IsSymbol(char c) => !char.IsAsciiLetterOrDigit(c) && c != ' '; // skip whitespace
}

public static string? ExtractMatchKeyByDiff(DiffResult? diff, string filename)
Expand Down
22 changes: 17 additions & 5 deletions SubRenamer/Services/RenameService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,17 @@ public void UpdateRenameTaskList(IEnumerable<MatchItem> matchList, Collection<Re
{
if (string.IsNullOrEmpty(item.Subtitle) || string.IsNullOrEmpty(item.Video)) continue;

var alter = Path.GetDirectoryName(item.Subtitle) +
"/" + Path.GetFileNameWithoutExtension(item.Video) +
var alter = "/" + Path.GetFileNameWithoutExtension(item.Video) +
Path.GetExtension(item.Subtitle);

if(Config.Get().RenameStrategy == Common.RenameStrategy.Copy)
{
alter = Path.GetDirectoryName(item.Video) + alter;
} else
{
alter = Path.GetDirectoryName(item.Subtitle) + alter;
}

destList.Add(new RenameTask(item.Subtitle, alter, item.Status == "已修改" ? "已修改" : "待修改")
{
MatchItem = item
Expand All @@ -41,9 +48,14 @@ public void ExecuteRename(IEnumerable<RenameTask> taskList)

try
{
if (Config.Get().Backup) FileHelper.BackupFile(task.Origin);
FileHelper.RenameFile(task.Origin, task.Alter);

if (Config.Get().RenameStrategy == Common.RenameStrategy.Copy)
{
FileHelper.CopyFile(task.Origin, task.Alter);
} else
{
if (Config.Get().Backup) FileHelper.BackupFile(task.Origin);
FileHelper.RenameFile(task.Origin, task.Alter);
}
task.Status = "已修改";
if (task.MatchItem != null) task.MatchItem.Status = "已修改";
}
Expand Down
12 changes: 12 additions & 0 deletions SubRenamer/ViewModels/SettingsViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using Microsoft.Extensions.DependencyInjection;
using SubRenamer.Helper;
using SubRenamer.Model;
using SubRenamer.Common;

namespace SubRenamer.ViewModels;

Expand All @@ -20,6 +21,17 @@ public partial class SettingsViewModel : ViewModelBase
private bool _updateCheckEnabled = Config.Get().UpdateCheck;
private string _videoExtAppend = Config.Get().VideoExtAppend;
private string _subtitleExtAppend = Config.Get().SubtitleExtAppend;
private RenameStrategy _renameStrategy = Config.Get().RenameStrategy;

public RenameStrategy RenameStrategy
{
get => _renameStrategy;
set
{
Config.Get().RenameStrategy = value;
SetProperty(ref _renameStrategy, value);
}
}

public bool BackupEnabled
{
Expand Down
132 changes: 76 additions & 56 deletions SubRenamer/Views/SettingsWindow.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,67 +3,87 @@
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:vm="using:SubRenamer.ViewModels"
xmlns:helper="clr-namespace:SubRenamer.Helper"
xmlns:common="clr-namespace:SubRenamer.Common"
mc:Ignorable="d"
WindowStartupLocation="CenterOwner"
Width="500"
SizeToContent="Height"
x:Class="SubRenamer.Views.SettingsWindow"
x:DataType="vm:SettingsViewModel"
Title="设置">

<Design.DataContext>
<vm:SettingsViewModel/>
</Design.DataContext>

<Window.Styles>
<Style Selector="Button.hyperlink">
<Setter Property="Template">
<ControlTemplate>
<TextBlock Text="{TemplateBinding Content}" Foreground="{StaticResource SystemAccentColor}" TextDecorations="Underline">
<TextBlock.Styles>
<Style Selector="TextBlock:pointerover">
<Setter Property="Foreground" Value="{StaticResource SystemAccentColorLight1}"/>
</Style>
</TextBlock.Styles>
</TextBlock>
</ControlTemplate>
</Setter>
<Setter Property="Background" Value="Transparent"/>
<Setter Property="BorderThickness" Value="0"/>
</Style>
</Window.Styles>

<StackPanel Margin="20">
<CheckBox IsChecked="{Binding BackupEnabled}">备份原始文件</CheckBox>
<Label Foreground="Gray" Margin="20 0 20 10">(备份字幕文件到 SubBackup 文件夹中)</Label>
<!-- <CheckBox>确认删除对话框</CheckBox> -->
<!-- <CheckBox>显示文件完整路径</CheckBox> -->
<CheckBox IsChecked="{Binding UpdateCheckEnabled}">程序升级检查</CheckBox>
<Border Height="15" />
<Grid HorizontalAlignment="Stretch" ColumnDefinitions="*,10,*">
<StackPanel Grid.Column="0">
<TextBlock Margin="0 5">视频格式扩充</TextBlock>
<TextBox Watermark="以逗号分隔" Text="{Binding VideoExtAppend}" />
</StackPanel>
<StackPanel Grid.Column="2">
<TextBlock Margin="0 5">字幕格式扩充</TextBlock>
<TextBox Watermark="以逗号分隔" Text="{Binding SubtitleExtAppend}" />
</StackPanel>
</Grid>

<Border Height="30" />
<TextBlock TextWrapping="Wrap" FontSize="13" LineHeight="24" Foreground="#5f6b7c">
|´・ω・)ノ 嗨!这是开源程序<LineBreak />
你可以在 <Button Classes="hyperlink" Content="GitHub" Command="{Binding OpenLinkCommand}" CommandParameter="https://github.com/qwqcode/SubRenamer" /> 找到源代码,请考虑点个 Star 🌟这会对我们很有帮助!
</TextBlock>

<Border Height="5" />
<Border BorderThickness=".5" BorderBrush="#D2D4D5"></Border>

<Border Height="15" />
<StackPanel Orientation="Horizontal" Spacing="15">
<Button Classes="hyperlink" Content="反馈问题" Command="{Binding OpenLinkCommand}" CommandParameter="https://github.com/qwqcode/SubRenamer/issues/new" />
<Button Classes="hyperlink" Content="更新日志" Command="{Binding OpenLinkCommand}" CommandParameter="https://github.com/qwqcode/SubRenamer/releases" />
</StackPanel>
</StackPanel>

<Design.DataContext>
<vm:SettingsViewModel/>
</Design.DataContext>

<Window.Resources>
<helper:EnumToBooleanConverter x:Key="EnumBoolConverter"/>
</Window.Resources>

<Window.Styles>
<Style Selector="Button.hyperlink">
<Setter Property="Template">
<ControlTemplate>
<TextBlock Text="{TemplateBinding Content}" Foreground="{StaticResource SystemAccentColor}" TextDecorations="Underline">
<TextBlock.Styles>
<Style Selector="TextBlock:pointerover">
<Setter Property="Foreground" Value="{StaticResource SystemAccentColorLight1}"/>
</Style>
</TextBlock.Styles>
</TextBlock>
</ControlTemplate>
</Setter>
<Setter Property="Background" Value="Transparent"/>
<Setter Property="BorderThickness" Value="0"/>
</Style>
</Window.Styles>

<StackPanel Margin="20">
<Canvas HorizontalAlignment="Left">
<TextBlock Canvas.Left="12" Canvas.Top="-5" Height="14" TextWrapping="Wrap" FontSize="11" FontWeight="Bold" LineHeight="24" Foreground="#5f6b7c" Background="White">
重命名设置
</TextBlock>
</Canvas>
<Border ZIndex="-1" CornerRadius="3" BorderThickness=".5" BorderBrush="#D2D4D5" Padding="5 0 0 0">
<StackPanel>
<RadioButton IsChecked="{Binding RenameStrategy, Converter={StaticResource EnumBoolConverter}, ConverterParameter={x:Static common:RenameStrategy.Copy}}" GroupName="howToRename" Content="将改名后的字幕文件复制到视频路径下" Margin="0 5 0 0"/>
<RadioButton IsChecked="{Binding RenameStrategy, Converter={StaticResource EnumBoolConverter}, ConverterParameter={x:Static common:RenameStrategy.Directly}}" GroupName="howToRename" Content="直接重命名字幕文件"/>
<TextBlock IsVisible="{Binding RenameStrategy, Converter={StaticResource EnumBoolConverter}, ConverterParameter={x:Static common:RenameStrategy.Directly}}">
<CheckBox IsChecked="{Binding BackupEnabled}" Margin="30 0 20 0">备份原始文件</CheckBox>
<Label VerticalAlignment="Center" Margin="-30 0 0 0" HorizontalAlignment="Left" Foreground="Gray">(备份字幕文件到 SubBackup 文件夹中)</Label>
</TextBlock>
</StackPanel>
</Border>
<Border Height="5" />
<!-- <CheckBox>确认删除对话框</CheckBox> -->
<!-- <CheckBox>显示文件完整路径</CheckBox> -->
<CheckBox IsChecked="{Binding UpdateCheckEnabled}">程序升级检查</CheckBox>
<Border Height="15" />
<Grid HorizontalAlignment="Stretch" ColumnDefinitions="*,10,*">
<StackPanel Grid.Column="0">
<TextBlock Margin="0 5">视频格式扩充</TextBlock>
<TextBox Watermark="以逗号分隔" Text="{Binding VideoExtAppend}" />
</StackPanel>
<StackPanel Grid.Column="2">
<TextBlock Margin="0 5">字幕格式扩充</TextBlock>
<TextBox Watermark="以逗号分隔" Text="{Binding SubtitleExtAppend}" />
</StackPanel>
</Grid>

<Border Height="30" />
<TextBlock TextWrapping="Wrap" FontSize="13" LineHeight="24" Foreground="#5f6b7c">
|´・ω・)ノ 嗨!这是开源程序<LineBreak />
你可以在 <Button Classes="hyperlink" Content="GitHub" Command="{Binding OpenLinkCommand}" CommandParameter="https://github.com/qwqcode/SubRenamer" /> 找到源代码,请考虑点个 Star 🌟这会对我们很有帮助!
</TextBlock>

<Border Height="5" />
<Border BorderThickness=".5" BorderBrush="#D2D4D5"></Border>

<Border Height="15" />
<StackPanel Orientation="Horizontal" Spacing="15">
<Button Classes="hyperlink" Content="反馈问题" Command="{Binding OpenLinkCommand}" CommandParameter="https://github.com/qwqcode/SubRenamer/issues/new" />
<Button Classes="hyperlink" Content="更新日志" Command="{Binding OpenLinkCommand}" CommandParameter="https://github.com/qwqcode/SubRenamer/releases" />
</StackPanel>
</StackPanel>
</Window>