Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Nikse.SubtitleEdit.Core.Common;
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Text.RegularExpressions;
- namespace Nikse.SubtitleEdit.Core.SubtitleFormats
- {
- /// <summary>
- /// Implements https://web.archive.org/web/20080213213927/http://trac.annodex.net/wiki/PJS
- /// https://github.com/SubtitleEdit/subtitleedit/pull/1964#issuecomment-249379407
- /// </summary>
- public class PhoenixSubtitle : SubtitleFormat
- {
- //2447, 2513, "You should come to the Drama Club, too."
- //2513, 2594, "Yeah. The Drama Club is worried|that you haven't been coming."
- //2603, 2675, "I see. Sorry, I'll drop by next time."
- private static readonly Regex RegexTimeCodes = new Regex(@"^(\d+),\s*(\d+),", RegexOptions.Compiled);
- private static readonly char[] TrimChars = { ' ', '"' };
- public override string Extension => ".pjs";
- public override bool IsTimeBased => false;
- public override string Name => "Phoenix Subtitle";
- public override bool IsMine(List<string> lines, string fileName)
- {
- if (fileName?.EndsWith(".pjs", StringComparison.OrdinalIgnoreCase) == false)
- {
- return false;
- }
- return base.IsMine(lines, fileName);
- }
- private Paragraph ToParagraph(string line) => StartTime(line, 0);
- // 5710,5754,"There isn't anything|in sector 14."
- private Paragraph StartTime(string line, int index)
- {
- // 3144,3217,"Alien VS. Predator"
- var ch = line[index];
- int from = index;
- if (!char.IsDigit(ch)) return null;
- while (char.IsDigit(line[index])) index++;
- return EndTime(int.Parse(line.Substring(from, index - from)), line, index);
- }
- private Paragraph EndTime(int startTime, string line, int index)
- {
- var ch = line[index];
- if (ch != ',') return null;
- index++;
- while(char.IsWhiteSpace(line[index])) index++;
- int from = index;
- while (index < line.Length && char.IsDigit(line[index])) index++;
- if(index == line.Length) return null;
- return Text(startTime, int.Parse(line.Substring(from, index - from)), line, index);
- }
- private Paragraph Text(int startTime, int endTime, string line, int index)
- {
- // skip:,"
- if (line[index] == ',') index++;
- if (line[index] == '"') index++;
- int len = line.Length;
- if (line.Length - 1 > 0 && line[len - 1] == '"') len--;
- var text = string.Join(Environment.NewLine, line.Substring(index, len - index).Split('|'));
- return new Paragraph(text, FramesToMilliseconds(startTime), FramesToMilliseconds(endTime));
- }
- public override void LoadSubtitle(Subtitle subtitle, List<string> lines, string fileName)
- {
- subtitle.Paragraphs.Clear();
- _errorCount = 0;
- for (var i = 0; i < lines.Count; i++)
- {
- string line = lines[i].Trim();
- // too short line
- if (line.Length < 4)
- {
- _errorCount++;
- continue;
- }
- var paragraph = ToParagraph(line);
- if (paragraph != null)
- {
- subtitle.Paragraphs.Add(paragraph);
- }
- else
- {
- _errorCount++;
- }
- }
- }
- public override string ToText(Subtitle subtitle, string title)
- {
- const string writeFormat = "{0},{1},\"{2}\"{3}";
- var sb = new StringBuilder();
- foreach (var p in subtitle.Paragraphs)
- {
- string text = HtmlUtil.RemoveHtmlTags(p.Text, true);
- // Pipe character for forced line breaks.
- text = text.Replace(Environment.NewLine, "|");
- sb.AppendFormat(writeFormat, MillisecondsToFrames(p.StartTime.TotalMilliseconds), MillisecondsToFrames(p.EndTime.TotalMilliseconds), text, Environment.NewLine);
- }
- return sb.ToString();
- }
- }
- }
- https://github.com/SubtitleEdit/subtitleedit/blob/1ee22c8f6b95402286bbec47bf1fd70a1d5812d7/src/libse/SubtitleFormats/PhoenixSubtitle.cs
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement