Advertisement
ivandrofly

PhoenixSubtitle

Oct 24th, 2024
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.51 KB | None | 0 0
  1. using Nikse.SubtitleEdit.Core.Common;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6.  
  7. namespace Nikse.SubtitleEdit.Core.SubtitleFormats
  8. {
  9.     /// <summary>
  10.     /// Implements https://web.archive.org/web/20080213213927/http://trac.annodex.net/wiki/PJS
  11.     /// https://github.com/SubtitleEdit/subtitleedit/pull/1964#issuecomment-249379407
  12.     /// </summary>
  13.     public class PhoenixSubtitle : SubtitleFormat
  14.     {
  15.         //2447,   2513, "You should come to the Drama Club, too."
  16.         //2513,   2594, "Yeah. The Drama Club is worried|that you haven't been coming."
  17.         //2603,   2675, "I see. Sorry, I'll drop by next time."
  18.  
  19.         private static readonly Regex RegexTimeCodes = new Regex(@"^(\d+),\s*(\d+),", RegexOptions.Compiled);
  20.         private static readonly char[] TrimChars = { ' ', '"' };
  21.  
  22.         public override string Extension => ".pjs";
  23.  
  24.         public override bool IsTimeBased => false;
  25.  
  26.         public override string Name => "Phoenix Subtitle";
  27.  
  28.         public override bool IsMine(List<string> lines, string fileName)
  29.         {
  30.             if (fileName?.EndsWith(".pjs", StringComparison.OrdinalIgnoreCase) == false)
  31.             {
  32.                 return false;
  33.             }
  34.             return base.IsMine(lines, fileName);
  35.         }
  36.  
  37.         private Paragraph ToParagraph(string line) => StartTime(line, 0);
  38.  
  39.         // 5710,5754,"There isn't anything|in sector 14."
  40.         private Paragraph StartTime(string line, int index)
  41.         {
  42.             // 3144,3217,"Alien VS. Predator"
  43.             var ch = line[index];
  44.             int from = index;
  45.             if (!char.IsDigit(ch)) return null;
  46.             while (char.IsDigit(line[index])) index++;
  47.             return EndTime(int.Parse(line.Substring(from, index - from)), line, index);
  48.         }
  49.  
  50.         private Paragraph EndTime(int startTime, string line, int index)
  51.         {
  52.             var ch = line[index];
  53.             if (ch != ',') return null;
  54.             index++;
  55.            
  56.             while(char.IsWhiteSpace(line[index])) index++;
  57.            
  58.             int from = index;
  59.            
  60.             while (index < line.Length && char.IsDigit(line[index])) index++;
  61.             if(index == line.Length) return null;
  62.            
  63.             return Text(startTime, int.Parse(line.Substring(from, index - from)), line, index);
  64.         }
  65.  
  66.         private Paragraph Text(int startTime, int endTime, string line, int index)
  67.         {
  68.             // skip:,"
  69.             if (line[index] == ',') index++;
  70.             if (line[index] == '"') index++;
  71.             int len = line.Length;
  72.             if (line.Length - 1 > 0 && line[len - 1] == '"') len--;
  73.             var text = string.Join(Environment.NewLine, line.Substring(index, len - index).Split('|'));
  74.             return new Paragraph(text, FramesToMilliseconds(startTime), FramesToMilliseconds(endTime));
  75.         }
  76.  
  77.         public override void LoadSubtitle(Subtitle subtitle, List<string> lines, string fileName)
  78.         {
  79.             subtitle.Paragraphs.Clear();
  80.             _errorCount = 0;
  81.             for (var i = 0; i < lines.Count; i++)
  82.             {
  83.                 string line = lines[i].Trim();
  84.  
  85.                 // too short line
  86.                 if (line.Length < 4)
  87.                 {
  88.                     _errorCount++;
  89.                     continue;
  90.                 }
  91.  
  92.                 var paragraph = ToParagraph(line);
  93.                 if (paragraph != null)
  94.                 {
  95.                     subtitle.Paragraphs.Add(paragraph);
  96.                 }
  97.                 else
  98.                 {
  99.                     _errorCount++;
  100.                 }
  101.             }
  102.         }
  103.  
  104.         public override string ToText(Subtitle subtitle, string title)
  105.         {
  106.             const string writeFormat = "{0},{1},\"{2}\"{3}";
  107.             var sb = new StringBuilder();
  108.             foreach (var p in subtitle.Paragraphs)
  109.             {
  110.                 string text = HtmlUtil.RemoveHtmlTags(p.Text, true);
  111.                 // Pipe character for forced line breaks.
  112.                 text = text.Replace(Environment.NewLine, "|");
  113.                 sb.AppendFormat(writeFormat, MillisecondsToFrames(p.StartTime.TotalMilliseconds), MillisecondsToFrames(p.EndTime.TotalMilliseconds), text, Environment.NewLine);
  114.             }
  115.             return sb.ToString();
  116.         }
  117.  
  118.     }
  119. }
  120.  
  121. https://github.com/SubtitleEdit/subtitleedit/blob/1ee22c8f6b95402286bbec47bf1fd70a1d5812d7/src/libse/SubtitleFormats/PhoenixSubtitle.cs
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement