BizHawk/BizHawk.Client.Common/movie/SubtitleList.cs

70 lines
1.6 KiB
C#
Raw Normal View History

using System.Collections.Generic;
2013-10-25 00:59:34 +00:00
using System.Globalization;
using System.Linq;
using System.Text;
2013-10-25 00:59:34 +00:00
namespace BizHawk.Client.Common
{
public class SubtitleList : List<Subtitle>
2013-10-25 00:59:34 +00:00
{
public IEnumerable<Subtitle> GetSubtitles(int frame)
2013-10-25 00:59:34 +00:00
{
return this.Where(t => frame >= t.Frame && frame <= t.Frame + t.Duration);
2013-10-25 00:59:34 +00:00
}
public override string ToString()
{
var sb = new StringBuilder();
ForEach(subtitle => sb.AppendLine(subtitle.ToString()));
return sb.ToString();
}
public bool AddFromString(string subtitleStr)
2013-10-25 00:59:34 +00:00
{
if (!string.IsNullOrWhiteSpace(subtitleStr))
2013-10-25 00:59:34 +00:00
{
try
{
var subparts = subtitleStr.Split(' ');
2013-10-25 00:59:34 +00:00
// Unfortunately I made the file format space delminated so this hack is necessary to get the message
var message = string.Empty;
for (var i = 6; i < subparts.Length; i++)
{
message += subparts[i] + ' ';
}
2013-10-25 00:59:34 +00:00
Add(new Subtitle
{
Frame = int.Parse(subparts[1]),
X = int.Parse(subparts[2]),
Y = int.Parse(subparts[3]),
Duration = int.Parse(subparts[4]),
Color = uint.Parse(subparts[5], NumberStyles.HexNumber),
Message = message
});
return true;
}
catch
{
return false;
}
2013-10-25 00:59:34 +00:00
}
return false;
2013-10-25 00:59:34 +00:00
}
public string ToSubRip(double fps)
{
int index = 1;
var sb = new StringBuilder();
foreach (var subtitle in this)
sb.Append(subtitle.ToSubRip(index++, fps));
return sb.ToString();
}
2013-10-25 00:59:34 +00:00
}
}