Skip to content
Open
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
7 changes: 7 additions & 0 deletions LICENSE.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

92 changes: 92 additions & 0 deletions Preview.gif.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 24 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,36 @@
# Unity-Simple-SRT

A super simple SRT subtitle parser

It'll parse most sane SRT's, and will display them in a `Text` element, crossfading between lines depending on a `Fade Time` you set.
It'll parse most sane SRT's and return a `SubtitleBlock` at the provided time.

## To use it
- Add the `Subtitle Displayer` component to something in the world.

```csharp
var parser = new SRTParser(Subtitle);

...

var elapsed = Time.time - startTime;
var subtitle = parser.GetForTime(elapsed);
if (!subtitle.Equals(currentSubtitle))
{
Debug.Log($"{subtitle.From}: {subtitle.Text}");
}
```

## Example

The following example can be found in the `Samples` folder.

- Add the `Subtitle Displayer` component to something in the world.
- Create two `Text` UI elements, both the same, and drag their references to the `Subtitle Displayer` component.
- Rename your `.srt` file to `.txt`, then drag it to the `Subtitle` field on `Subtitle Displayer`.
- Call `StartCoroutine(subtitleDisplayer.Begin())` to start the subtitles.

A sub file like this:
```

```srt
1
00:00:00,000 --> 00:00:02,500
Mary had
Expand All @@ -31,6 +51,7 @@ little <size=40>lamb</size>
00:00:09,000 --> 00:00:12,000
<b><i><color=red>Until she didn't.</color></i></b>
```

Will result in this:

![Totally the best gif evar](https://github.com/roguecode/Unity-Simple-SRT/blob/master/Preview.gif?raw=true)
Expand Down
7 changes: 7 additions & 0 deletions README.md.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions src/Assets/Example.meta → Runtime.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Runtime/Roguecode.Unity-Simple-SRT.asmdef
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "Roguecode.Unity-Simple-SRT"
}
7 changes: 7 additions & 0 deletions Runtime/Roguecode.Unity-Simple-SRT.asmdef.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

132 changes: 132 additions & 0 deletions Runtime/SRTParser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
using System;
using System.Collections.Generic;
using UnityEngine;

namespace Roguecode.UnitySimpleSRT
{
public class SRTParser
{
enum eReadState
{
Index,
Time,
Text
}

private List<SubtitleBlock> _subtitles;

public SRTParser(string textAssetResourcePath)
{
var text = Resources.Load<TextAsset>(textAssetResourcePath);
Load(text);
}

public SRTParser(TextAsset textAsset)
{
this._subtitles = Load(textAsset);
}

static public List<SubtitleBlock> Load(TextAsset textAsset)
{
if (textAsset == null)
{
Debug.LogError("Subtitle file is null");
return null;
}

var lines = textAsset.text.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None);

var currentState = eReadState.Index;

var subs = new List<SubtitleBlock>();

int currentIndex = 0;
double currentFrom = 0, currentTo = 0;
var currentText = string.Empty;
for (var l = 0; l < lines.Length; l++)
{
var line = lines[l];

switch (currentState)
{
case eReadState.Index:
{
int index;
if (Int32.TryParse(line, out index))
{
currentIndex = index;
currentState = eReadState.Time;
}
}
break;
case eReadState.Time:
{
line = line.Replace(',', '.');
var parts = line.Split(new[] { "-->" }, StringSplitOptions.RemoveEmptyEntries);

// Parse the timestamps
if (parts.Length == 2)
{
TimeSpan fromTime;
if (TimeSpan.TryParse(parts[0], out fromTime))
{
TimeSpan toTime;
if (TimeSpan.TryParse(parts[1], out toTime))
{
currentFrom = fromTime.TotalSeconds;
currentTo = toTime.TotalSeconds;
currentState = eReadState.Text;
}
}
}
}
break;
case eReadState.Text:
{
if (currentText != string.Empty)
currentText += "\r\n";

currentText += line;

// When we hit an empty line, consider it the end of the text
if (string.IsNullOrEmpty(line) || l == lines.Length - 1)
{
// Create the SubtitleBlock with the data we've aquired
subs.Add(new SubtitleBlock(currentIndex, currentFrom, currentTo, currentText));

// Reset stuff so we can start again for the next block
currentText = string.Empty;
currentState = eReadState.Index;
}
}
break;
}
}
return subs;
}

public SubtitleBlock GetForTime(float time)
{
if (_subtitles.Count > 0)
{
var subtitle = _subtitles[0];

if (time >= subtitle.To)
{
_subtitles.RemoveAt(0);

if (_subtitles.Count == 0)
return null;

subtitle = _subtitles[0];
}

if (subtitle.From > time)
return SubtitleBlock.Blank;

return subtitle;
}
return null;
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions Runtime/SubtitleBlock.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
namespace Roguecode.UnitySimpleSRT
{
public class SubtitleBlock
{
static SubtitleBlock _blank;
public static SubtitleBlock Blank
{
get { return _blank ?? (_blank = new SubtitleBlock(0, 0, 0, string.Empty)); }
}
public int Index { get; private set; }
public double Length { get; private set; }
public double From { get; private set; }
public double To { get; private set; }
public string Text { get; private set; }

public SubtitleBlock(int index, double from, double to, string text)
{
this.Index = index;
this.From = from;
this.To = to;
this.Length = to - from;
this.Text = text;
}
}
}
11 changes: 11 additions & 0 deletions Runtime/SubtitleBlock.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions src/Assets/SimpleSRT.meta → Samples~/SubtitleDisplayer.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading