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

128 lines
2.9 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 bool ConcatMultilines { get; set; }
public bool AddColorTag { get; set; }
public SubtitleList()
{
ConcatMultilines = false;
AddColorTag = false;
}
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();
Sort();
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
2017-05-10 11:45:23 +00:00
var message = "";
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.Trim()
});
return true;
}
catch
{
return false;
}
2013-10-25 00:59:34 +00:00
}
return false;
2013-10-25 00:59:34 +00:00
}
2016-06-30 16:14:12 +00:00
public new void Sort()
{
Sort((x, y) =>
2016-06-30 16:14:12 +00:00
{
int result = x.Frame.CompareTo(y.Frame);
return result != 0 ? result : x.Y.CompareTo(y.Y);
});
}
2017-04-15 20:37:30 +00:00
public string ToSubRip(double fps)
{
int index = 1;
var sb = new StringBuilder();
List<Subtitle> subs = new List<Subtitle>();
foreach (var subtitle in this)
2017-05-17 18:18:26 +00:00
{
subs.Add(subtitle);
2017-05-17 18:18:26 +00:00
}
// absense of line wrap forces miltiline subtitle macros
// so we sort them just in case and optionally concat back to a single unit
// todo: instead of making this pretty, add the line wrap feature to subtitles
if (ConcatMultilines)
{
int lastframe = 0;
subs = subs.OrderBy(s => s.Frame).ThenBy(s => s.Y).ToList();
2017-05-09 18:19:55 +00:00
for (int i = 0;; i++)
{
2017-05-09 18:19:55 +00:00
if (i == subs.Count) // we're modifying it
{
break;
2017-05-09 18:19:55 +00:00
}
subs[i].Message = subs[i].Message.Trim();
if (i > 0 && lastframe == subs[i].Frame)
{
2019-03-20 05:01:12 +00:00
subs[i].Message = $"{subs[i - 1].Message} {subs[i].Message}";
subs.Remove(subs[i - 1]);
i--;
}
lastframe = subs[i].Frame;
}
}
else
{
// srt stacks musltilines upwards
subs = subs.OrderBy(s => s.Frame).ThenByDescending(s => s.Y).ToList();
}
foreach (var subtitle in subs)
2017-05-17 18:18:26 +00:00
{
sb.Append(subtitle.ToSubRip(index++, fps, AddColorTag));
2017-05-17 18:18:26 +00:00
}
2017-04-15 20:37:30 +00:00
return sb.ToString();
}
2013-10-25 00:59:34 +00:00
}
}