mirror of
https://github.com/jellyfin/jellyfin.git
synced 2026-03-19 16:46:36 +00:00
implemented SRT Parser
This commit is contained in:
@@ -1,17 +1,61 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace MediaBrowser.MediaEncoding.Subtitles
|
||||
{
|
||||
public class SrtParser : ISubtitleParser
|
||||
{
|
||||
public SubtitleTrackInfo Parse(Stream stream)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
private readonly CultureInfo _usCulture = new CultureInfo("en-US");
|
||||
public SubtitleTrackInfo Parse(Stream stream) {
|
||||
var trackInfo = new SubtitleTrackInfo();
|
||||
using ( var reader = new StreamReader(stream))
|
||||
{
|
||||
string line;
|
||||
while ((line = reader.ReadLine()) != null)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(line))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
var subEvent = new SubtitleTrackEvent {Id = line};
|
||||
line = reader.ReadLine();
|
||||
var time = Regex.Split(line, @"[\t ]*-->[\t ]*");
|
||||
subEvent.StartPositionTicks = GetTicks(time[0]);
|
||||
var endTime = time[1];
|
||||
var idx = endTime.IndexOf(" ", StringComparison.Ordinal);
|
||||
if (idx > 0)
|
||||
endTime = endTime.Substring(0, idx);
|
||||
subEvent.EndPositionTicks = GetTicks(endTime);
|
||||
var multiline = new List<string>();
|
||||
while ((line = reader.ReadLine()) != null)
|
||||
{
|
||||
if (string.IsNullOrEmpty(line))
|
||||
{
|
||||
break;
|
||||
}
|
||||
multiline.Add(line);
|
||||
}
|
||||
subEvent.Text = string.Join(@"\N", multiline);
|
||||
subEvent.Text = Regex.Replace(subEvent.Text, "\\{(\\\\[\\w]+\\(?([\\w\\d]+,?)+\\)?)+\\}", string.Empty, RegexOptions.IgnoreCase);
|
||||
subEvent.Text = Regex.Replace(subEvent.Text, "<", "<", RegexOptions.IgnoreCase);
|
||||
subEvent.Text = Regex.Replace(subEvent.Text, ">", ">", RegexOptions.IgnoreCase);
|
||||
subEvent.Text = Regex.Replace(subEvent.Text, "<(\\/?(font|b|u|i|s))((\\s+(\\w|\\w[\\w\\-]*\\w)(\\s*=\\s*(?:\\\".*?\\\"|'.*?'|[^'\\\">\\s]+))?)+\\s*|\\s*)(\\/?)>", "<$1$3$7>", RegexOptions.IgnoreCase);
|
||||
subEvent.Text = Regex.Replace(subEvent.Text, @"\\N", "<br />",RegexOptions.IgnoreCase);
|
||||
trackInfo.TrackEvents.Add(subEvent);
|
||||
}
|
||||
}
|
||||
return trackInfo;
|
||||
}
|
||||
|
||||
long GetTicks(string time) {
|
||||
TimeSpan span;
|
||||
return TimeSpan.TryParseExact(time, @"hh\:mm\:ss\.fff", _usCulture, out span)
|
||||
? span.Ticks
|
||||
: (TimeSpan.TryParseExact(time, @"hh\:mm\:ss\,fff", _usCulture, out span)
|
||||
? span.Ticks : 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user