SpongeEngine.SubtitleSharp
2.0.2
See the version list below for details.
dotnet add package SpongeEngine.SubtitleSharp --version 2.0.2
NuGet\Install-Package SpongeEngine.SubtitleSharp -Version 2.0.2
<PackageReference Include="SpongeEngine.SubtitleSharp" Version="2.0.2" />
paket add SpongeEngine.SubtitleSharp --version 2.0.2
#r "nuget: SpongeEngine.SubtitleSharp, 2.0.2"
// Install SpongeEngine.SubtitleSharp as a Cake Addin #addin nuget:?package=SpongeEngine.SubtitleSharp&version=2.0.2 // Install SpongeEngine.SubtitleSharp as a Cake Tool #tool nuget:?package=SpongeEngine.SubtitleSharp&version=2.0.2
SubtitleSharp
SubtitleSharp is a robust C# library for parsing and writing subtitle files. It supports multiple subtitle formats—including SubRip (SRT), WebVTT (VTT), and SubStation Alpha (SSA)—and provides a unified API for both synchronous and asynchronous subtitle processing.
Features
- Multi-Format Support: Parse and write subtitles in SRT, VTT, and SSA formats.
- Unified Parsing API: Automatically detect and parse subtitles via file extension or by specifying a preferred format using the
SubtitleParser
class. - Customizable Options: Configure parsing behavior using
SubtitleParserOptions
and control timecode requirements withSubtitleTimecodeMode
. - Asynchronous Processing: Fully supports async/await for non-blocking I/O operations.
- Cross-Platform Compatibility: Works with .NET 6.0, .NET 7.0, and .NET 8.0+.
Installation
Install via NuGet:
dotnet add package SpongeEngine.SubtitleSharp
Examples
Automatic Format Detection
using System.Text;
using SpongeEngine.SubtitleSharp;
// Open the subtitle file as a stream.
using var fileStream = new FileStream("path_to_subtitle.srt", FileMode.Open, FileAccess.Read);
// Initialize the parser with default options.
var parser = new SubtitleParser();
// Parse the stream into a list of SubtitleItem objects.
var subtitleItems = parser.ParseStream(fileStream, new SubtitleParserOptions { Encoding = Encoding.UTF8 });
// Output subtitle start and end times.
foreach (var item in subtitleItems)
{
Console.WriteLine($"Start: {item.StartTime} ms, End: {item.EndTime} ms");
foreach (var line in item.Lines)
{
Console.WriteLine(line);
}
}
Parsing from Text
using System.Text;
using SpongeEngine.SubtitleSharp;
string subtitleContent = File.ReadAllText("path_to_subtitle.vtt", Encoding.UTF8);
var subtitleItems = new SubtitleParser().ParseText(subtitleContent, Encoding.UTF8);
Specifying a Preferred Format
using System.Text;
using SpongeEngine.SubtitleSharp;
// Specify the preferred format (e.g., SubRip for SRT files).
var preferredFormat = SubtitlesFormat.SubRipFormat;
using var fileStream = new FileStream("path_to_subtitle.srt", FileMode.Open, FileAccess.Read);
var subtitleItems = new SubtitleParser().ParseStream(fileStream, new SubtitleParserOptions { Encoding = Encoding.UTF8 }, preferredFormat);
Asynchronous Parsing
using System.Text;
using SpongeEngine.SubtitleSharp;
using var fileStream = new FileStream("path_to_subtitle.ssa", FileMode.Open, FileAccess.Read);
var parser = new SubtitleParser();
var subtitleItems = await parser.ParseStreamAsync(fileStream, new SubtitleParserOptions { Encoding = Encoding.UTF8 });
Writing SRT Files
using SpongeEngine.SubtitleSharp;
using SpongeEngine.SubtitleSharp.Writers;
// Assume subtitleItems is a List<SubtitleItem> obtained from parsing.
using var outputStream = new FileStream("output.srt", FileMode.Create, FileAccess.Write);
var srtWriter = new SrtWriter();
srtWriter.WriteStream(outputStream, subtitleItems);
Writing SSA Files
using SpongeEngine.SubtitleSharp;
using SpongeEngine.SubtitleSharp.Writers;
using var outputStream = new FileStream("output.ssa", FileMode.Create, FileAccess.Write);
var ssaWriter = new SsaWriter();
await ssaWriter.WriteStreamAsync(outputStream, subtitleItems);
Logging and Error Handling
using Microsoft.Extensions.Logging;
using SpongeEngine.SubtitleSharp;
ILogger logger = LoggerFactory
.Create(builder => builder
.AddConsole()
.SetMinimumLevel(LogLevel.Debug))
.CreateLogger<SubtitleParser>();
try
{
var parser = new SubtitleParser(logger);
using var fileStream = new FileStream("path_to_subtitle.srt", FileMode.Open, FileAccess.Read);
var subtitleItems = parser.ParseStream(fileStream, new SubtitleParserOptions { Encoding = Encoding.UTF8 });
}
catch (ArgumentException ex)
{
Console.WriteLine($"Error parsing subtitle: {ex.Message}");
}
Testing
To run the unit tests, execute:
dotnet test
License
This project is licensed under the MIT License - see the LICENSE file for details.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Support
For issues and feature requests, please use the GitHub issues page.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. |
-
net6.0
- Microsoft.Extensions.AI (>= 9.1.0-preview.1.25064.3)
- Microsoft.Extensions.Caching.Memory (>= 9.0.1)
- Microsoft.Extensions.Logging.Abstractions (>= 9.0.1)
- Polly (>= 8.5.1)
- System.Linq.Async (>= 6.0.1)
-
net7.0
- Microsoft.Extensions.AI (>= 9.1.0-preview.1.25064.3)
- Microsoft.Extensions.Caching.Memory (>= 9.0.1)
- Microsoft.Extensions.Logging.Abstractions (>= 9.0.1)
- Polly (>= 8.5.1)
- System.Linq.Async (>= 6.0.1)
-
net8.0
- Microsoft.Extensions.AI (>= 9.1.0-preview.1.25064.3)
- Microsoft.Extensions.Caching.Memory (>= 9.0.1)
- Microsoft.Extensions.Logging.Abstractions (>= 9.0.1)
- Polly (>= 8.5.1)
- System.Linq.Async (>= 6.0.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.