Пример #1
0
        public static string SAMI2SRT(string SAMITxt)
        {
            if (string.IsNullOrEmpty(SAMITxt))
            {
                return(null);
            }
            Match m = new Regex("<Body[^>]*>.*?</Body>", RegexOptions.Singleline).Match(SAMITxt);

            if (!m.Success)
            {
                return(null);
            }
            XmlDocument doc = new XmlDocument();

            doc.LoadXml(m.Value.Replace("<br>", "<br />"));
            List <SAMIItem> items = new List <SAMIItem>();
            XmlNodeList     syncs = doc.SelectNodes("//Sync");

            foreach (XmlNode sync in syncs)
            {
                int start;
                if (sync.Attributes["Start"] == null || !int.TryParse(sync.Attributes["Start"].Value, out start))
                {
                    continue;
                }
                SAMIItem item = new SAMIItem();
                item.StartTime = start;
                foreach (XmlNode p in sync.SelectNodes("./P"))
                {
                    item.Subtitle += GetSubtitleTxt(p);
                }
                items.Add(item);
            }
            string subtitle = "";
            int    id       = 1;

            for (int x = 0; x < items.Count - 1; x++)
            {
                if (string.IsNullOrEmpty(items[x].Subtitle))
                {
                    continue;
                }
                System.TimeSpan ts        = TimeSpan.FromMilliseconds(items[x].StartTime);
                string          startTime = new DateTime(ts.Ticks).ToString("HH:mm:ss,fff");
                ts = TimeSpan.FromMilliseconds(items[x + 1].StartTime);
                string endTime = new DateTime(ts.Ticks).ToString("HH:mm:ss,fff");
                subtitle += string.Format(srtFormat, id, startTime, endTime, items[x].Subtitle);
                id++;
            }
            return(subtitle);
        }
 public static string SAMI2SRT(string SAMITxt)
 {
     if (string.IsNullOrEmpty(SAMITxt))
         return null;
     Match m = new Regex("<Body[^>]*>.*?</Body>", RegexOptions.Singleline).Match(SAMITxt);
     if (!m.Success)
         return null;
     XmlDocument doc = new XmlDocument();
     doc.LoadXml(m.Value.Replace("<br>", "<br />"));
     List<SAMIItem> items = new List<SAMIItem>();
     XmlNodeList syncs = doc.SelectNodes("//Sync");
     foreach (XmlNode sync in syncs)
     {
         int start;
         if (sync.Attributes["Start"] == null || !int.TryParse(sync.Attributes["Start"].Value, out start))
             continue;
         SAMIItem item = new SAMIItem();
         item.StartTime = start;
         foreach (XmlNode p in sync.SelectNodes("./P"))
             item.Subtitle += GetSubtitleTxt(p);
         items.Add(item);
     }
     string subtitle = "";
     int id = 1;
     for (int x = 0; x < items.Count - 1; x++)
     {
         if (string.IsNullOrEmpty(items[x].Subtitle))
             continue;
         System.TimeSpan ts = TimeSpan.FromMilliseconds(items[x].StartTime);
         string startTime = new DateTime(ts.Ticks).ToString("HH:mm:ss,fff");
         ts = TimeSpan.FromMilliseconds(items[x + 1].StartTime);
         string endTime = new DateTime(ts.Ticks).ToString("HH:mm:ss,fff");
         subtitle += string.Format(srtFormat, id, startTime, endTime, items[x].Subtitle);
         id++;
     }
     return subtitle;
 }