| .. | ||
| README.md | ||
| README.md.meta | ||
| Subtitle.cs | ||
| Subtitle.cs.meta | ||
| SubtitlePackage.cs | ||
| SubtitlePackage.cs.meta | ||
SrtParser
SrtParser is a lightweight and optimized parser for the SubRip text file format, also known as .srt files.
Parsing SRT content
using SrtParser;
var srt = SubtitlePackage.Parse("1\n00:20:41,150 --> 00:20:45,109\nHow did he do that?\n\n");
The resulting object is of class SrtParser.SubtitlePackage, and can be used to extract subtitles:
var currentSubtitle = srt.FindSubtitle(currentVideoTime);
if (currentSubtitle != null)
{
// display a sub
Debug.Log(currentSubtitle.Text);
}
else
{
// no subtitle to display at this time
}
To convert back to an SRT form, use the Stringify() method:
var srtText = SubtitlePackage.Stringify(srt);
File.WriteAllText("output.srt", srtText);
Subtitle data manipulation
Subtitles are actually exposed publicly in the SubtitlePackage, so it is pretty easy to read an srt file, alter its content, and write it back.
var inputText = File.ReadAllText("input.srt", Encoding.UTF8);
var srt = SubtitlePackage.Parse(inputText);
// add one subtitle at the end
var subtitle = new Subtitle();
subtitle.SequenceNumber = srt.Subtitles.Count;
subtitle.StartTime = 10000.0f;
subtitle.EndTime = 10005.0f;
subtitle.Text = "Secret hidden sub";
srt.Subtitles.Add(subtitle);
var outputText = SubtitlePackage.Stringify(srt);
File.WriteAllText("output.srt", outputText);
License
MIT?