Description: class
support adjust time on subscene file (*.srt)
Support AddHours,
AddMinutes, AddSeconds, AddMiniseconds (Milliseconds)
Ex: 00:01:46,273 --> 00:01:48,400
AddSeconds(3) then 00:01:49,273 --> 00:01:51,400
Class: SrtParser.cs
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SubsceneParser
{
///
/// Class Subscene file Srt Parser.
///
public class SrtParser
{
private string _filePath;
public SrtParser(string filePath)
{
_filePath = filePath;
}
///
/// To the list.
///
/// List<Sub>.
public List<Sub> ToList()
{
List<Sub> result = new List<Sub>();
Sub sub = null;
string[] allLines = File.ReadAllLines(_filePath, Encoding.UTF8);
ParseStatus status = ParseStatus.Number;
foreach (string str in allLines)
{
switch (status)
{
case ParseStatus.Number:
if (IsNumber(str))
{
sub = new Sub() { Position = int.Parse(str) };
status = ParseStatus.Time;
}
else if (sub != null)
{
sub.Content += str;
}
break;
case ParseStatus.Time:
string[] arrTime = str.Split(new string[] { " --> " }, StringSplitOptions.None);
sub.StartTime = new Time(arrTime[0]);
sub.EndTime = new Time(arrTime[1]);
status = ParseStatus.Content;
break;
case ParseStatus.Content:
if (!string.IsNullOrEmpty(sub.Content))
sub.Content += Environment.NewLine;
sub.Content += str;
if (string.IsNullOrEmpty(str))
{
result.Add(sub);
status = ParseStatus.Number;
}
break;
}
}
return result;
}
public bool IsNumber(string strNum)
{
if (string.IsNullOrEmpty(strNum))
return false;
for (int i = 0; i < strNum.Length; i++)
{
if (i == 0 && strNum[i] == '-')
continue;
if (!char.IsDigit(strNum[i]))
return false;
}
return true;
}
}
///
/// 7
///00:01:46,273 --> 00:01:48,400
///cho chúng tôi biết nó là cái gì
///
///
public class Sub
{
public int Position { get; set; }
public Time StartTime { get; set; }
public Time EndTime { get; set; }
public string Content { get; set; }
public override string ToString()
{
return string.Format("{0}\n{1} --> {2}\n{3}",
Position, StartTime, EndTime, Content);
}
}
///
/// Enum ParseStatus
///
public enum ParseStatus
{
Number,
Time,
Content
}
///
/// Class Time.
///
public class Time
{
public Time(string time)
{
try
{
string[] arrTime = time.Split(new char[] { ':', ',' });
Hours = int.Parse(arrTime[0]);
Minutes = int.Parse(arrTime[1]);
Seconds = int.Parse(arrTime[2]);
Miniseconds = int.Parse(arrTime[3]);
}
catch
{
//Exception
}
}
public int Hours { get; set; }
public int Minutes { get; set; }
public int Seconds { get; set; }
public int Miniseconds { get; set; }
public Time AddMiniseconds(int miniseconds)
{
Miniseconds += miniseconds;
if (Miniseconds >= 1000)
{
int seconds = Miniseconds / 1000;
Miniseconds %= 1000;
AddSeconds(seconds);
}
else if (Miniseconds < 0)
{
AddSeconds(-1);
AddMiniseconds(1000);
}
return this;
}
public Time AddSeconds(int seconds)
{
Seconds += seconds;
if (Seconds >= 60)
{
int minutes = Seconds / 60;
Seconds %= 60;
AddMinutes(minutes);
}
else if (Seconds < 0)
{
AddMinutes(-1);
AddSeconds(60);
}
return this;
}
public Time AddMinutes(int minutes)
{
Minutes += minutes;
if (Minutes >= 60)
{
int hours = Minutes / 60;
Minutes %= 60;
AddHours(hours);
}
else if (Minutes < 0)
{
AddHours(-1);
AddMinutes(60);
}
return this;
}
public Time AddHours(int hours)
{
Hours += hours;
return this;
}
public override string ToString()
{
return string.Format("{0:00}:{1:00}:{2:00},{3:000}",
Hours, Minutes, Seconds, Miniseconds);
}
}
}
Usage
SrtParser parser = new SrtParser(e);
List<Sub> _lstSubs = parser.ToList();
int seconds = int.Parse(txtSecond.Text);
foreach (Sub sub in _lstSubs)
{
sub.StartTime.AddSeconds(seconds);
sub.EndTime.AddSeconds(seconds);
}
Không có nhận xét nào:
Đăng nhận xét