SubtitlesParserV2 2.3.0
dotnet add package SubtitlesParserV2 --version 2.3.0
NuGet\Install-Package SubtitlesParserV2 -Version 2.3.0
<PackageReference Include="SubtitlesParserV2" Version="2.3.0" />
<PackageVersion Include="SubtitlesParserV2" Version="2.3.0" />
<PackageReference Include="SubtitlesParserV2" />
paket add SubtitlesParserV2 --version 2.3.0
#r "nuget: SubtitlesParserV2, 2.3.0"
#:package SubtitlesParserV2@2.3.0
#addin nuget:?package=SubtitlesParserV2&version=2.3.0
#tool nuget:?package=SubtitlesParserV2&version=2.3.0
SubtitlesParserV2
Universal subtitles parser which aims at supporting parsing for all subtitle formats. For more info on subtitles formats, see this page: http://en.wikipedia.org/wiki/Category:Subtitle_file_formats
This is a fork/continuation of the original SubtitlesParser that seems to be a bit outdated / not updated anymore on nuget. Since I needed to parse subtitles in one of my projects, I decided to take this existing library, update the dependencies, and rewrite some parts of it in my own way at the same time, fixing / improving some parsers.
Supported Parsers/Writers
Subtitle Format | Parser | Writer | Extensions Detection | Supported Specs / Versions |
---|---|---|---|---|
SubRip | ✅ | ❌ | .srt |
srt |
LRC | ✅ | ❌ | .lrc |
Core LRC , Enhanced LRC format (A2 extension) |
TMPlayer | ✅ | ❌ | .tmp |
long & short codes |
MicroDvd | ✅ | ❌ | .sub |
MicroDVD |
SubViewer | ✅ | ❌ | .sbv |
SubViewer2 , SubViewer1 |
SubStationAlpha | ✅ | ❌ | .ssa , .ass |
v4.00 + backward |
Timed Text Markup Language (TTML) | ✅ | ❌ | .ttml , .dfxp , .xml , .itt |
TTML 1.0 , TTML 2.0 |
WebVTT | ✅ | ❌ | .vtt |
WebVTT |
Synchronized Accessible Media Interchange (SAMI) | ✅ | ❌ | .smi , .sami |
Only Support Time in MS |
YouTube Timed Text (YoutubeXml) | ✅ | ❌ | .ytt , .srv3 , .srv2 , .srv1 |
srv3 , srv2 , srv1 |
MPL2 | ✅ | ❌ | .mpl , .mpl2 |
MPL |
Universal Subtitle Format (USF) | ✅ | ❌ | .usf |
v1.1 |
Quickstart
Universal parser
If you don't specify the subtitle format, SubtitlesParserV2 will try all the registered parsers with the default configuration
using (FileStream fileStream = File.OpenRead(pathToSrtFile)){
// Try to parse with all supported parsers
SubtitleParserResultModel? result = SubtitleParser.ParseStream(fileStream, Encoding.UTF8)
// Access the Subtitles with: result.Subtitles
// Access the Subtitle parser that was used with: result.FormatType
// Note that if all parsers fail, the method will return: null
}
When accessing subtitles with result.Subtitles
, if the start or end time value is negative (generally -1
), it means there was no way to determine the timestamp, or the extension failed to parse it correctly (and didn't fail).
Having a -1
does not mean your file is corrupted. This behavior is expected in some formats like LRC and TMPlayer, where only the start time of a subtitle is provided in the file itself. In these cases, a -1
at the end time of the very last subtitle is normal. (Normal because for theses formats, the library use the next subtitle start time to know the previous subtitle end time)
Get Subtitle format by extension (Extensions detection)
string fileName = Path.GetFileName(file);
// Get format enum
SubtitleFormatType? mostLikelyFormat = SubtitleFormat.GetFormatTypeByFileExtensionName(Path.GetExtension(fileName).Replace(".",""));
// Get format instance
if (mostLikelyFormat != null)
{
SubtitleFormat format = SubtitleFormat.GetFormat(mostLikelyFormat.Value);
Console.WriteLine($"Matching format is : {format.Name}");
}
Specific parser (default configuration)
You can use one/multiples specific parser:
using (FileStream fileStream = File.OpenRead(pathToSrtFile)){
// Try to parse with one specific parser using default configuration
SubtitleParserResultModel result = SubtitleParser.ParseStream(fileStream, Encoding.UTF8, SubtitleFormatType.SubStationAlpha)
// Try to parse with multiple parser using default configuration
SubtitleParserResultModel result = SubtitleParser.ParseStream(fileStream, Encoding.UTF8, new[] { SubtitleFormatType.SubStationAlpha, SubtitleFormatType.LRC });
}
Specific parser (Advanced configurations)
You can also specify advanced configurations for parsers that support it.
Defining advanced configurations is not possible inside the SubtitleParser.ParseStream
method. You need to get the parser instance as a ISubtitlesParser<TConfig>
(interface).
TConfig refers to the parser name followed by ParserConfig
. As a example, MicroDvd
would become MicroDvdParserConfig
.
When using a parser directly from its instance, you must handle any thrown errors using a try-catch statement. Otherwise, your code may fail when the parser cannot parse the given stream.
// Get the format
SubtitleFormat format = SubtitleFormat.GetFormat(SubtitleFormatType.MicroDvd);
// Get the instance as a advanced parser
ISubtitlesParser<MicroDvdParserConfig> microDvdParserInstance = format.ParserInstance as ISubtitlesParser<MicroDvdParserConfig>;
// Parse
microDvdParserInstance.ParseStream(fileStream, Encoding.UTF8, new MicroDvdParserConfig()
{
Framerate = 30, // Force the parser to use a framerate of 30
});
Logging
SubtitlesParserV2 implements microsoft.extensions.logging.abstractions to allow you to redirect the log output to your own logging method (using a LoggerFactory)
Base example
// Assuming a LoggerFactory already exist
ILoggerFactory existingLoggerFactory = LoggerFactory.Create(builder =>
{
builder.AddConsole(); // Adding the default console logging
});
// Set SubtitlesParserV2 LoggerFactory to the existing one
LoggerManager.LoggerFactory = existingLoggerFactory;
ASP.NET Core Websites
// Get the app loggerFactory
ILoggerFactory loggerFactory = app.Services.GetRequiredService<ILoggerFactory>();
LoggerManager.LoggerFactory = loggerFactory; // Redirect SubtitlesParserV2 logs to our LoggerFactory
Licenses / Acknowledgements
Current code is licensed (sublicensed as allowed per the original project MIT license) under the GNU Lesser General Public License v3.0 (LGPLv3).
While this is not legal advice, a common misconception is that the LGPL license, due to having "GPL" in its name, requires you to license your program under the GPL or the same license. This is not necessarily true. Since this project is a library, the requirements depend on how you use the library and the compatibility between this project's license and your project's license. However, please note that this tip does not override the specific requirements of the LGPL license. There may be additional obligations based on your use case. For further clarification, you can refer to the LGPL FAQ about static vs dynamic linking requirements, this Reddit post, or this blog post. Again, this is not legal advice.
⚠️ The original version, available here (link redirects you to the exact commit from which the project was forked, the commit history was discarded due to concerns about some files in the commit history.), is licensed under the original project made by AlexPoint, license/credits:
The MIT License (MIT)
Copyright (c) 2015
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. 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 was computed. 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. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
.NET Core | netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.1 is compatible. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.1
- Microsoft.Extensions.Logging.Abstractions (>= 9.0.1)
-
net8.0
- Microsoft.Extensions.Logging.Abstractions (>= 9.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.
## What's Changed
### Fix
* fix(yttParser): Reduced memory usage by @kitsumed in https://github.com/kitsumed/SubtitlesParserV2/pull/3
* fix(ttmlParser): Reduced memory usage by @kitsumed in https://github.com/kitsumed/SubtitlesParserV2/pull/4
* fix(MicroDvd): Prevent reading the whole file for no reasons
**By default, MicroDvd parser will abort parsing if it can't find any valid lines in the first 20 lines. Can be changed using the custom settings interface.**
### Refractor
* refractor(lrcParser): Make it simple, add line timeout
_Rewrote Lrc Parser_.
**By default, Lrc parser will abort parsing if it can't find any valid lines in the first 20 lines. Can be changed using the custom settings interface.**
* chore: Improved methods and class comments.
### Features
* feature(ttmlParser): Improved TTML parser
**TTML now support:**
1. FrameRate time (SMPTE without sub-frames)
2. All available timecode formats and missing formats. (Ignore sub-frames)
3. `itt` and `xml` files (iTunes Timed Text)
4. Multi-lines subtitles.
* feature(usfParser): Added USF parser, rework xml child parser
**Universal Subtitle Format (USF) v1.1 with the `usf` file extension** .
* feature: Added units test
**The binary is available on [NuGet](SubtitlesParserV2)**.
**Full Changelog**: https://github.com/kitsumed/SubtitlesParserV2/compare/2.2.3...2.3.0