Skip to content

Commit

Permalink
[feat] 新增 srt 转换
Browse files Browse the repository at this point in the history
  • Loading branch information
kengwang committed Jul 6, 2024
1 parent 3a3913e commit 5064f19
Show file tree
Hide file tree
Showing 4 changed files with 159 additions and 18 deletions.
88 changes: 88 additions & 0 deletions examples/csharp/ALRC/ALRC.Converters/SrtConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
using System.Text;
using System.Text.RegularExpressions;
using ALRC.Abstraction;

namespace ALRC.Converters;

public class SrtConverter : ILyricConverter<string>
{
public ALRCFile Convert(string input)
{
var lines = new List<ALRCLine>();
var alrc = new ALRCFile
{
Schema = "https://github.com/kengwang/ALRC/blob/main/schemas/v1.json",
LyricInfo = null,
SongInfo = null,
Header = null,
Lines = lines
};
const string regex = @"(?<id>\d+)\n(?<start>[\d\:,]*)\s?-->\s?(?<end>[\d\:,]*)\n(?<line>(?>.+\r?\n)+(?=(?>\r?\n)?))";
var matches = Regex.Matches(input, regex);
foreach (Match match in matches)
{
var line = match.Groups["line"].Value;
var start = match.Groups["start"].Value;
var end = match.Groups["end"].Value;
// 00:00:11,269 to Milliseconds
start = start.Replace(',', '.');
end = end.Replace(',', '.');
var startTime = TimeSpan.Parse(start).TotalMilliseconds;
var endTime = TimeSpan.Parse(end).TotalMilliseconds;
var lineContents = line.Replace("\r\n", "\n").Split('\n');
var alrcLine = new ALRCLine
{
Id = match.Groups["id"].Value,
Start = (int)startTime,
End = (int)endTime
};
if (lineContents.Length > 0)
{
alrcLine.RawText = lineContents[0];
if (lineContents.Length > 1)
{
alrcLine.Translation = lineContents[1];
}

if (lineContents.Length > 2)
{
alrcLine.Comment = lineContents[2];
}
}
lines.Add(alrcLine);
}

return alrc;
}

public string ConvertBack(ALRCFile input)
{
var builder = new StringBuilder();
if (input.Lines is { Count: > 0 })
{
var id = 1;
for (var index = 0; index < input.Lines.Count; index++)
{
var inputLine = input.Lines[index];
var startTimeString = TimeSpan.FromMilliseconds(inputLine.Start ?? 0)
.ToString(@"hh\:mm\:ss\,fff");
var endTimeString = TimeSpan.FromMilliseconds(inputLine.End ?? 0)
.ToString(@"hh\:mm\:ss\,fff");
builder.AppendLine($"{id++}\n{startTimeString} --> {endTimeString}\n{inputLine.RawText}");
if (!string.IsNullOrWhiteSpace(inputLine.Translation))
{
builder.AppendLine(inputLine.Translation);
}

if (!string.IsNullOrWhiteSpace(inputLine.Comment))
{
builder.AppendLine(inputLine.Comment);
}

builder.AppendLine();
}
}

return builder.ToString();
}
}
6 changes: 3 additions & 3 deletions examples/csharp/ALRC/ALRC.Creator/ALRC.Creator.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
<UseWPF>true</UseWPF>
<Platforms>AnyCPU;x64</Platforms>
<LangVersion>default</LangVersion>
<Version>1.2.0</Version>
<AssemblyVersion>1.1.0</AssemblyVersion>
<FileVersion>1.1.0</FileVersion>
<Version>1.2.1</Version>
<AssemblyVersion>1.2.1</AssemblyVersion>
<FileVersion>1.2.1</FileVersion>
</PropertyGroup>

<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
Expand Down
53 changes: 38 additions & 15 deletions examples/csharp/ALRC/ALRC.Creator/Views/Pages/ConvertPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Border CornerRadius="8" Grid.Row="0" Grid.Column="0" Background="Gray" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Margin="8">
<Border CornerRadius="8" Grid.Row="0" Grid.Column="0" Background="Gray" VerticalAlignment="Stretch"
HorizontalAlignment="Stretch" Margin="8">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
Expand All @@ -36,8 +37,9 @@
</StackPanel>
</Grid>
</Border>

<Border Grid.Row="0" CornerRadius="8" Grid.Column="1" Background="Gray" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Margin="8">

<Border Grid.Row="0" CornerRadius="8" Grid.Column="1" Background="Gray" VerticalAlignment="Stretch"
HorizontalAlignment="Stretch" Margin="8">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
Expand All @@ -56,8 +58,9 @@
</Grid>
</Grid>
</Border>

<Border CornerRadius="8" Grid.Row="0" Grid.Column="2" Background="Gray" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Margin="8">

<Border CornerRadius="8" Grid.Row="0" Grid.Column="2" Background="Gray" VerticalAlignment="Stretch"
HorizontalAlignment="Stretch" Margin="8">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
Expand All @@ -72,9 +75,10 @@
</StackPanel>
</Grid>
</Border>


<Border CornerRadius="8" Grid.Row="1" Grid.Column="0" Background="Gray" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Margin="8">


<Border CornerRadius="8" Grid.Row="1" Grid.Column="0" Background="Gray" VerticalAlignment="Stretch"
HorizontalAlignment="Stretch" Margin="8">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
Expand All @@ -89,8 +93,9 @@
</StackPanel>
</Grid>
</Border>

<Grid Grid.Row="1" Grid.Column="1" VerticalAlignment="Stretch" Background="Gray" HorizontalAlignment="Stretch" Margin="8">

<Border CornerRadius="8" Grid.Row="1" Grid.Column="1" VerticalAlignment="Stretch" Background="Gray"
HorizontalAlignment="Stretch" Margin="8">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
Expand All @@ -104,8 +109,9 @@
<Button Click="ConvertToTTML_Click">To TTML</Button>
</StackPanel>
</Grid>
</Grid>
<Grid Grid.Row="1" Grid.Column="2" VerticalAlignment="Stretch" Background="Gray" HorizontalAlignment="Stretch" Margin="8">
</Border>
<Border CornerRadius="8" Grid.Row="1" Grid.Column="2" VerticalAlignment="Stretch" Background="Gray"
HorizontalAlignment="Stretch" Margin="8">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
Expand All @@ -119,8 +125,9 @@
<Button Click="ConvertToLyricifySyllable_Click">Convert To</Button>
</StackPanel>
</Grid>
</Grid>
<Grid Grid.Row="2" Grid.Column="0" VerticalAlignment="Stretch" Background="Gray" HorizontalAlignment="Stretch" Margin="8">
</Border>
<Border CornerRadius="8" Grid.Row="2" Grid.Column="0" VerticalAlignment="Stretch" Background="Gray"
HorizontalAlignment="Stretch" Margin="8">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
Expand All @@ -133,6 +140,22 @@
<Button Click="ConvertTranslationToLrc_Click">Export LRC</Button>
</StackPanel>
</Grid>
</Grid>
</Border>
<Border CornerRadius="8" Grid.Row="2" Grid.Column="1" VerticalAlignment="Stretch" Background="Gray"
HorizontalAlignment="Stretch" Margin="8">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<fa:FontAwesome FontSize="80" Grid.Row="0" Icon="FileText" />
<TextBlock Grid.Row="1" TextAlignment="Center" FontSize="35" Text="SRT" />
<StackPanel Grid.Row="2" Orientation="Horizontal">
<Button Click="ConvertFromSRT_Click">From SRT</Button>
<Button Click="ConvertToSRT_Click">To SRT</Button>
</StackPanel>
</Grid>
</Border>
</Grid>
</Page>
30 changes: 30 additions & 0 deletions examples/csharp/ALRC/ALRC.Creator/Views/Pages/ConvertPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -239,4 +239,34 @@ private void ConvertToLyricifySyllable_Click(object sender, RoutedEventArgs e)
File.WriteAllText(dialog.FileName, lrcText);
}
}

private void ConvertFromSRT_Click(object sender, RoutedEventArgs e)
{
var dialog = new OpenFileDialog();
dialog.Filter = "SRT 文件|*.srt";
if (dialog.ShowDialog() is true)
{
var lrc = File.ReadAllText(dialog.FileName);
var converter = new SrtConverter();
var alrcFile = converter.Convert(lrc);
var result = new EditableALRCConverter().ConvertBack(alrcFile);
_viewModel.Alrc.Lines = result.Lines;
_viewModel.Alrc.Styles = result.Styles;
_viewModel.Alrc.SongInfos = result.SongInfos;
_viewModel.Alrc.LyricInfo = result.LyricInfo;
}
}

private void ConvertToSRT_Click(object sender, RoutedEventArgs e)
{
var alrc = new EditableALRCConverter().Convert(_viewModel.Alrc);
var converter = new SrtConverter();
var lrcText = converter.ConvertBack(alrc);
var dialog = new SaveFileDialog();
dialog.Filter = "SRT 文件|*.srt";
if (dialog.ShowDialog() is true)
{
File.WriteAllText(dialog.FileName, lrcText);
}
}
}

0 comments on commit 5064f19

Please sign in to comment.