예제 #1
0
        public SubZir ParseStream(Stream xmlStream, Encoding encoding)
        {
            xmlStream.Position = 0;
            var items = new SubZir();

            var        xElement = XElement.Load(xmlStream);
            XNamespace tt       = xElement.GetNamespaceOfPrefix("tt");

            if (xElement != null)
            {
                var nodeList = xElement.Descendants(tt + "p").ToList();

                if (nodeList != null)
                {
                    for (var i = 0; i < nodeList.Count; i++)
                    {
                        var node = nodeList[i];
                        try
                        {
                            var reader = node.CreateReader();
                            reader.MoveToContent();
                            var  beginString = node.Attribute("begin").Value.Replace("t", "");
                            long startTicks  = long.Parse(beginString);
                            var  endString   = node.Attribute("end").Value.Replace("t", "");
                            long endTicks    = long.Parse(endString);
                            var  text        = reader.ReadInnerXml().Replace("<tt:", "<").Replace("</tt:", "</").Replace(string.Format(@" xmlns:tt=""{0}""", tt), "");

                            items.Add(new SubtitleItem()
                            {
                                StartTime = (int)(startTicks / 10000),
                                EndTime   = (int)(endTicks / 10000),
                                Lines     = new List <string>()
                                {
                                    text
                                }
                            });
                        }
                        catch (Exception ex)
                        {
                            System.Diagnostics.Debug.WriteLine(string.Format("Exception raised when parsing xml node {0}: {1}", node, ex));
                        }
                    }
                }
            }

            if (items.Any())
            {
                return(items);
            }
            else
            {
                throw new ArgumentException("Stream is not in a valid TTML format, or represents empty subtitles");
            }
        }
예제 #2
0
        public SubZir ParseStream(Stream subStream, Encoding encoding)
        {
            subStream.Position = 0;
            var    items  = new SubZir();
            var    reader = new StreamReader(subStream, encoding, true);
            string reshte = reader.ReadToEnd();

            JsonArray jsonArray;

            if (JsonArray.TryParse(reshte, out jsonArray))
            {
                //try
                {
                    foreach (JsonValue groupValue in jsonArray)
                    {
                        JsonObject groupObject = groupValue.GetObject();
                        JsonObject itemObject  = groupObject.GetObject();
                        //try
                        {
                            double startText = itemObject["startTime"].GetNumber();
                            double endText   = itemObject["endTime"].GetNumber();
                            int    start     = ParseJSONTimecode(startText.ToString());
                            int    end       = ParseJSONTimecode(endText.ToString());

                            JsonObject itemObject2 =
                                itemObject["metadata"].GetObject();
                            string text = itemObject2["Text"].GetString();                            //<br />
                            items.Add(new SubtitleItem
                            {
                                StartTime = start,
                                EndTime   = end,
                                Lines     = new List <string>(text.Split(new string[] { "<br />", Environment.NewLine,
                                                                                        "<br/>", "/<br>" }, StringSplitOptions.RemoveEmptyEntries))
                            });
                        }
                        //catch { }
                    }
                }
                //catch (Exception ex)
                //{

                //}
            }
            if (items.Any())
            {
                return(items);
            }
            else
            {
                throw new ArgumentException("Stream is not in a valid JSON4 format");
            }
        }
        async public void ImportSubtitle(StorageFile file)
        {
            try
            {
                if (file == null)
                {
                    return;
                }
                Stream stream = await file.OpenStreamForReadAsync();


                var parser   = new SubParser();
                var fileName = Path.GetFileName(file.Path);
                SubtitleEncoding Subencoding = HelperUP.GetSubtitleEncoding(stream);
                Encoding         encoding    = new UTF8Encoding();
                if (Subencoding == SubtitleEncoding.ASCII)
                {
                    encoding = new ASCIIEncoding();
                }
                else if (Subencoding == SubtitleEncoding.Unicode)
                {
                    encoding = new UnicodeEncoding();
                }
                else if (Subencoding == SubtitleEncoding.UTF8)
                {
                    encoding = new UTF8Encoding();
                }
                else if (Subencoding == SubtitleEncoding.Windows1256)
                {
                    encoding = new CustomCP1256Encoding();
                }
                else if (Subencoding == SubtitleEncoding.UTF7)
                {
                    encoding = new UTF7Encoding();
                }
                else if (Subencoding == SubtitleEncoding.UTF32)
                {
                    encoding = Encoding.UTF32;
                }
                else if (Subencoding == SubtitleEncoding.BigEndianUnicode)
                {
                    encoding = Encoding.BigEndianUnicode;
                }
                var mostLikelyFormat = parser.GetMostLikelyFormat(fileName, stream, encoding);

                HelperUP.Output("mostLikelyFormat: " + mostLikelyFormat.Name);
                SubtitleList = parser.ParseStream(await file.OpenStreamForReadAsync(), encoding, mostLikelyFormat);
                dt.Start();
            }
            catch (Exception ex) { HelperUP.Output("ImportSubtitle ex: " + ex.Message); }
        }
예제 #4
0
        public SubZir ParseStream(Stream xmlStream, Encoding encoding)
        {
            xmlStream.Position = 0;
            var          items  = new SubZir();
            StreamReader st     = new StreamReader(xmlStream, encoding, true);
            string       reshte = st.ReadToEnd();

            XmlDocument document = new XmlDocument();

            document.LoadXml(reshte);
            XmlNodeList list = document.SelectSingleNode("USFSubtitles").SelectSingleNode("subtitles").SelectNodes("subtitle");

            for (int j = 0; j < list.Count; j++)
            {
                try
                {
                    string startText = "", endText = "", text = "";
                    startText = list[j].Attributes.GetNamedItem("start").InnerText;
                    endText   = list[j].Attributes.GetNamedItem("stop").InnerText;
                    text      = list[j].SelectSingleNode("text").InnerText;

                    int start = ParseSrtTimecode(startText);
                    int end   = ParseSrtTimecode(endText);
                    items.Add(new SubtitleItem()
                    {
                        StartTime = start,
                        EndTime   = end,
                        Lines     = new List <string>()
                        {
                            text
                        }
                    });
                }
                catch { }
            }
            if (items.Any())
            {
                return(items);
            }
            else
            {
                throw new ArgumentException("Stream is not in a valid Universal Subtitle XML format");
            }
        }
예제 #5
0
        public SubZir ParseStream(Stream stream, Encoding encoding)
        {
            stream.Position = 0;
            var    items  = new SubZir();
            var    reader = new StreamReader(stream, encoding, true);
            string reshte = reader.ReadToEnd();
            SubZir subZir = new SubZir();

            try
            {
                LoadSubtitle(subZir, reshte);
            }
            catch (Exception ex) {
                System.Diagnostics.Debug.WriteLine("SamiParser ex: " + ex.Message);
            }
            if (subZir.Any())
            {
                return(subZir);
            }
            else
            {
                throw new ArgumentException("Stream is not in a valid Sami format");
            }
        }
예제 #6
0
        public SubZir ParseStream(Stream srtStream, Encoding encoding)
        {
            if (!srtStream.CanRead || !srtStream.CanSeek)
            {
                var message = string.Format("Stream must be seekable and readable in a subtitles parser. " +
                                            "Operation interrupted; isSeekable: {0} - isReadable: {1}",
                                            srtStream.CanSeek, srtStream.CanSeek);
                throw new ArgumentException(message);
            }


            srtStream.Position = 0;

            var reader = new StreamReader(srtStream, encoding, true);

            var items       = new SubZir();
            var srtSubParts = GetSrtSubTitleParts(reader).ToList();

            if (srtSubParts.Any())
            {
                foreach (var srtSubPart in srtSubParts)
                {
                    var lines =
                        srtSubPart.Split(new string[] { Environment.NewLine }, StringSplitOptions.None)
                        .Select(s => s.Trim())
                        .Where(l => !string.IsNullOrEmpty(l))
                        .ToList();

                    var item = new SubtitleItem();
                    foreach (var line in lines)
                    {
                        if (item.StartTime == 0 && item.EndTime == 0)
                        {
                            int startTc;
                            int endTc;
                            var success = TryParseTimecodeLine(line, out startTc, out endTc);
                            if (success)
                            {
                                item.StartTime = startTc;
                                item.EndTime   = endTc;
                            }
                        }
                        else
                        {
                            item.Lines.Add(line + Environment.NewLine);
                        }
                    }

                    if ((item.StartTime != 0 || item.EndTime != 0) && item.Lines.Any())
                    {
                        items.Add(item);
                    }
                }

                if (items.Any())
                {
                    return(items);
                }
                else
                {
                    throw new ArgumentException("Stream is not in a valid Srt format");
                }
            }
            else
            {
                throw new FormatException("Parsing as srt returned no srt part.");
            }
        }
        public SubZir ParseStream(Stream stream, Encoding encoding)
        {
            stream.Position = 0;
            SubZir        items          = new SubZir();
            Regex         regexTimeCodes = new Regex(@"^\d\d:\d\d:\d\d:\d\d\\\d\d:\d\d:\d\d:\d\d$");
            StreamReader  st             = new StreamReader(stream, encoding, true);
            string        reshte         = st.ReadToEnd();
            List <string> lines          = new List <string>(reshte.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries));

            var          sb          = new StringBuilder();
            SubtitleItem p           = null;
            int          _errorCount = 0;

            foreach (string line in lines)
            {
                string s = line.Trim();
                if (regexTimeCodes.IsMatch(s))
                {
                    var temp = s.Split('\\');
                    if (temp.Length > 1)
                    {
                        string start = temp[0];
                        string end   = temp[1];

                        string[] startParts = start.Split(new[] { ':', '.' }, StringSplitOptions.RemoveEmptyEntries);
                        string[] endParts   = end.Split(new[] { ':', '.' }, StringSplitOptions.RemoveEmptyEntries);
                        if (startParts.Length == 4 && endParts.Length == 4)
                        {
                            try
                            {
                                p           = new SubtitleItem();
                                p.StartTime = DecodeTimeCode(startParts);
                                p.EndTime   = DecodeTimeCode(endParts);
                                string text = sb.ToString().Trim();

                                Boolean positionTop = false;

                                if (text.StartsWith("}"))
                                {
                                    positionTop = true;

                                    text = text.Remove(0, 1);
                                }

                                text = text.Replace("[", @"<i>");
                                text = text.Replace("]", @"</i>");



                                text = text.Replace("</i>" + Environment.NewLine + "<i>", Environment.NewLine);


                                if (positionTop)
                                {
                                    text = "{\\an8}" + text;
                                }

                                p.Lines = new List <string>()
                                {
                                    text
                                };
                                if (text.Length > 0)
                                {
                                    items.Add(p);
                                }
                                sb = new StringBuilder();
                            }
                            catch (Exception exception)
                            {
                                _errorCount++;
                                System.Diagnostics.Debug.WriteLine(exception.Message);
                            }
                        }
                    }
                }
                else if (string.IsNullOrWhiteSpace(line) || line.StartsWith("*"))
                {
                }
                else if (p != null)
                {
                    sb.AppendLine(line);
                }
            }


            if (items.Any())
            {
                return(items);
            }
            else
            {
                throw new ArgumentException("Stream is not in a valid 'SoftNi colon sub' Subtitle format");
            }
        }
예제 #8
0
        public void LoadSubtitle(SubZir subtitle, string contents, List <string> liness = null)
        {
            string allInput      = contents;
            string allInputLower = allInput.ToLower();

            if (!allInputLower.Contains("<sync "))
            {
                return;
            }

            int styleStart = allInputLower.IndexOf("<style", StringComparison.Ordinal);

            const string syncTag      = "<sync start=";
            const string syncTagEnc   = "<sync encrypted=\"true\" start=";
            int          syncStartPos = allInputLower.IndexOf(syncTag, StringComparison.Ordinal);
            int          index        = syncStartPos + syncTag.Length;

            int syncStartPosEnc = allInputLower.IndexOf(syncTagEnc, StringComparison.Ordinal);

            if ((syncStartPosEnc >= 0 && syncStartPosEnc < syncStartPos) || syncStartPos == -1)
            {
                syncStartPos = syncStartPosEnc;
                index        = syncStartPosEnc + syncTagEnc.Length;
            }

            var p = new SubtitleItem();

            while (syncStartPos >= 0)
            {
                string millisecAsString = string.Empty;
                while (index < allInput.Length && @"""'0123456789".Contains(allInput[index]))
                {
                    if (allInput[index] != '"' && allInput[index] != '\'')
                    {
                        millisecAsString += allInput[index];
                    }
                    index++;
                }

                while (index < allInput.Length && allInput[index] != '>')
                {
                    index++;
                }
                if (index < allInput.Length && allInput[index] == '>')
                {
                    index++;
                }

                int syncEndPos    = allInputLower.IndexOf(syncTag, index, StringComparison.Ordinal);
                int syncEndPosEnc = allInputLower.IndexOf(syncTagEnc, index, StringComparison.Ordinal);
                if ((syncStartPosEnc >= 0 && syncStartPosEnc < syncStartPos) || syncEndPos == -1)
                {
                    syncEndPos = syncEndPosEnc;
                }

                string text;
                if (syncEndPos >= 0)
                {
                    text = allInput.Substring(index, syncEndPos - index);
                }
                else
                {
                    text = allInput.Substring(index);
                }

                string textToLower = text.ToLower();
                if (textToLower.Contains(" class="))
                {
                    var className  = new StringBuilder();
                    int startClass = textToLower.IndexOf(" class=", StringComparison.Ordinal);
                    int indexClass = startClass + 7;
                    while (indexClass < textToLower.Length)
                    {
                        className.Append(text[indexClass]);
                        indexClass++;
                    }
                }

                if (text.Contains("ID=\"Source\"") || text.Contains("ID=Source"))
                {
                    int sourceIndex = text.IndexOf("ID=\"Source\"", StringComparison.Ordinal);
                    if (sourceIndex < 0)
                    {
                        sourceIndex = text.IndexOf("ID=Source", StringComparison.Ordinal);
                    }
                    int st = sourceIndex - 1;
                    while (st > 0 && text.Substring(st, 2).ToUpper() != "<P")
                    {
                        st--;
                    }
                    if (st > 0)
                    {
                        text = text.Substring(0, st) + text.Substring(sourceIndex);
                    }
                    int et = st;
                    while (et < text.Length - 5 && text.Substring(et, 3).ToUpper() != "<P>" && text.Substring(et, 4).ToUpper() != "</P>")
                    {
                        et++;
                    }
                    text = text.Substring(0, st) + text.Substring(et);
                }
                text = text.Replace(Environment.NewLine, " ");
                text = text.Replace("  ", " ");

                text = text.TrimEnd();
                text = Regex.Replace(text, @"<br {0,2}/?>", Environment.NewLine, RegexOptions.IgnoreCase);

                while (text.Contains("  "))
                {
                    text = text.Replace("  ", " ");
                }
                text = text.Replace("</BODY>", string.Empty).Replace("</SAMI>", string.Empty).TrimEnd();

                int endSyncPos = text.ToUpper().IndexOf("</SYNC>", StringComparison.Ordinal);
                if (text.IndexOf('>') > 0 && (text.IndexOf('>') < endSyncPos || endSyncPos == -1))
                {
                    text = text.Remove(0, text.IndexOf('>') + 1);
                }
                text = text.TrimEnd();

                if (text.EndsWith("</sync>", StringComparison.OrdinalIgnoreCase))
                {
                    text = text.Substring(0, text.Length - 7).TrimEnd();
                }

                if (text.EndsWith("</p>", StringComparison.Ordinal) || text.EndsWith("</P>", StringComparison.Ordinal))
                {
                    text = text.Substring(0, text.Length - 4).TrimEnd();
                }

                text = text.Replace("&nbsp;", " ").Replace("&NBSP;", " ");

                if (text.Contains("<font color=") && !text.Contains("</font>"))
                {
                    text += "</font>";
                }
                if (text.StartsWith("<FONT COLOR=") && !text.Contains("</font>") && !text.Contains("</FONT>"))
                {
                    text += "</FONT>";
                }

                if (text.Contains('<') && text.Contains('>'))
                {
                    var  total   = new StringBuilder();
                    var  partial = new StringBuilder();
                    bool tagOn   = false;
                    for (int i = 0; i < text.Length; i++)
                    {
                        string tmp = text.Substring(i);
                        if (tmp.StartsWith("<") &&
                            (tmp.StartsWith("<font", StringComparison.Ordinal) ||
                             tmp.StartsWith("<div", StringComparison.Ordinal) ||
                             tmp.StartsWith("<i", StringComparison.Ordinal) ||
                             tmp.StartsWith("<b", StringComparison.Ordinal) ||
                             tmp.StartsWith("<s", StringComparison.Ordinal) ||
                             tmp.StartsWith("</", StringComparison.Ordinal)))
                        {
                            total.Append(WebUtility.HtmlDecode(partial.ToString()));
                            partial = new StringBuilder();
                            tagOn   = true;
                            total.Append('<');
                        }
                        else if (text.Substring(i).StartsWith(">") && tagOn)
                        {
                            tagOn = false;
                            total.Append('>');
                        }
                        else if (!tagOn)
                        {
                            partial.Append(text[i]);
                        }
                        else
                        {
                            total.Append(text[i]);
                        }
                    }
                    total.Append(WebUtility.HtmlDecode(partial.ToString()));
                    text = total.ToString();
                }
                else
                {
                    text = WebUtility.HtmlDecode(text);
                }

                string cleanText = text;
                while (cleanText.Contains("  "))
                {
                    cleanText = cleanText.Replace("  ", " ");
                }
                while (cleanText.Contains(Environment.NewLine + " "))
                {
                    cleanText = cleanText.Replace(Environment.NewLine + " ", Environment.NewLine);
                }
                while (cleanText.Contains(" " + Environment.NewLine))
                {
                    cleanText = cleanText.Replace(" " + Environment.NewLine, Environment.NewLine);
                }
                cleanText = cleanText.Trim();

                if (p.Lines.Count > 0 && !string.IsNullOrEmpty(millisecAsString) &&
                    p.Lines[0] != null && p.Lines[0].Length > 0)
                {
                    p.EndTime = int.Parse(millisecAsString);
                    subtitle.Add(p);
                    p = new SubtitleItem();
                }

                p.Lines = new List <string> {
                    cleanText
                };
                int l;
                if (int.TryParse(millisecAsString, out l))
                {
                    p.StartTime = l;
                }

                if (syncEndPos <= 0)
                {
                    syncStartPos = -1;
                }
                else
                {
                    syncStartPos = allInputLower.IndexOf(syncTag, syncEndPos, StringComparison.Ordinal);
                    index        = syncStartPos + syncTag.Length;

                    syncStartPosEnc = allInputLower.IndexOf(syncTagEnc, syncEndPos, StringComparison.Ordinal);
                    if ((syncStartPosEnc >= 0 && syncStartPosEnc < syncStartPos) || syncStartPos == -1)
                    {
                        syncStartPos = syncStartPosEnc;
                        index        = syncStartPosEnc + syncTagEnc.Length;
                    }
                }
            }
            if (!string.IsNullOrEmpty(p.Lines[0]) && !subtitle.Contains(p) &&
                p.Lines[0] != null && p.Lines[0].Length > 0)
            {
                p.EndTime = p.StartTime;
                subtitle.Add(p);
            }

            if (subtitle.Count > 0 &&
                (subtitle[subtitle.Count - 1].Lines[0].ToUpper().Trim() == "</BODY>" ||
                 subtitle[subtitle.Count - 1].Lines[0].ToUpper().Trim() == "<BODY>"))
            {
                subtitle.RemoveAt(subtitle.Count - 1);
            }

            System.Diagnostics.Debug.WriteLine(subtitle.Count);
        }
        public SubZir ParseStream(Stream stream, Encoding encoding)
        {
            stream.Position = 0;
            SubZir        items                   = new SubZir();
            Regex         regexTimeCodes          = new Regex(@"^\d\d\d\d  \d\d:\d\d:\d\d:\d\d  \d\d:\d\d:\d\d:\d\d");
            Regex         regex1DigitMilliseconds = new Regex(@"^\d\d\d\d  \d\d\d:\d\d:\d\d:\d  \d\d\d:\d\d:\d\d:\d");
            StreamReader  st     = new StreamReader(stream, encoding, true);
            string        reshte = st.ReadToEnd();
            List <string> lines  = new List <string>(reshte.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries));

            var          sb            = new StringBuilder();
            SubtitleItem lastParagraph = null;
            int          _errorCount   = 0;

            foreach (string line in lines)
            {
                if (string.IsNullOrWhiteSpace(line))
                {
                    continue;
                }

                bool success = false;
                if (line.IndexOf(':') > 0)
                {
                    string s     = line;
                    var    match = regexTimeCodes.Match(s);
                    var    match1DigitMilliseconds = regex1DigitMilliseconds.Match(s);
                    if (s.Length > 31 && match.Success)
                    {
                        s = s.Substring(5, match.Length - 5).TrimStart();
                        s = s.Replace("  ", ":");
                        s = s.Replace(" ", string.Empty);
                        string[] parts = s.Split(':');
                        if (parts.Length == 8)
                        {
                            int hours        = int.Parse(parts[0]);
                            int minutes      = int.Parse(parts[1]);
                            int seconds      = int.Parse(parts[2]);
                            int milliseconds = int.Parse(parts[3]) * 10;
                            int start        = int.Parse(new TimeSpan(0, hours, minutes, seconds, milliseconds).TotalMilliseconds.ToString());

                            hours        = int.Parse(parts[4]);
                            minutes      = int.Parse(parts[5]);
                            seconds      = int.Parse(parts[6]);
                            milliseconds = int.Parse(parts[7]) * 10;
                            int end = int.Parse(new TimeSpan(0, hours, minutes, seconds, milliseconds).TotalMilliseconds.ToString());

                            string text = line.Replace("\0", string.Empty).Substring(match.Length).TrimStart();
                            text = text.Replace("|", Environment.NewLine);

                            lastParagraph           = new SubtitleItem();
                            lastParagraph.StartTime = start;
                            lastParagraph.EndTime   = end;
                            lastParagraph.Lines     = new List <string>()
                            {
                                text
                            };
                            items.Add(lastParagraph);
                            success = true;
                        }
                    }
                    else if (s.Length > 29 && match1DigitMilliseconds.Success)
                    {
                        s = s.Substring(5, match1DigitMilliseconds.Length - 5).TrimStart();
                        s = s.Replace("  ", ":");
                        s = s.Replace(" ", string.Empty);
                        string[] parts = s.Split(':');
                        if (parts.Length == 8)
                        {
                            int hours        = int.Parse(parts[0]);
                            int minutes      = int.Parse(parts[1]);
                            int seconds      = int.Parse(parts[2]);
                            int milliseconds = int.Parse(parts[3]) * 10;
                            int start        = int.Parse(new TimeSpan(0, hours, minutes, seconds, milliseconds).TotalMilliseconds.ToString());

                            hours        = int.Parse(parts[4]);
                            minutes      = int.Parse(parts[5]);
                            seconds      = int.Parse(parts[6]);
                            milliseconds = int.Parse(parts[7]) * 10;
                            int end = int.Parse(new TimeSpan(0, hours, minutes, seconds, milliseconds).TotalMilliseconds.ToString());

                            string text = line.Replace("\0", string.Empty).Substring(match1DigitMilliseconds.Length).TrimStart();
                            text = text.Replace("|", Environment.NewLine);

                            lastParagraph           = new SubtitleItem();
                            lastParagraph.StartTime = start;
                            lastParagraph.EndTime   = end;
                            lastParagraph.Lines     = new List <string>()
                            {
                                text
                            };
                            items.Add(lastParagraph);
                            success = true;
                        }
                    }
                }
                if (!success)
                {
                    _errorCount++;
                }
            }
            if (items.Any())
            {
                return(items);
            }
            else
            {
                throw new ArgumentException("Stream is not in a valid 'Sony DVDArchitect w. line#' Subtitle format");
            }
        }
예제 #10
0
        public SubZir ParseStream(Stream subStream, Encoding encoding)
        {
            if (!subStream.CanRead || !subStream.CanSeek)
            {
                var message = string.Format("Stream must be seekable and readable in a subtitles parser. " +
                                            "Operation interrupted; isSeekable: {0} - isReadable: {1}",
                                            subStream.CanSeek, subStream.CanSeek);
                throw new ArgumentException(message);
            }


            subStream.Position = 0;
            var reader = new StreamReader(subStream, encoding, true);

            var items = new SubZir();
            var line  = reader.ReadLine();

            while (line != null && !IsMicroDvdLine(line))
            {
                line = reader.ReadLine();
            }

            if (line != null)
            {
                float frameRate;

                var firstItem = ParseLine(line, defaultFrameRate);
                if (firstItem.Lines != null && firstItem.Lines.Any())
                {
                    var success = TryExtractFrameRate(firstItem.Lines[0], out frameRate);
                    if (!success)
                    {
                        System.Diagnostics.Debug.WriteLine(string.Format("Couldn't extract frame rate of sub file with first line {0}. " +
                                                                         "We use the default frame rate: {1}", line, defaultFrameRate));
                        frameRate = defaultFrameRate;


                        items.Add(firstItem);
                    }
                }
                else
                {
                    frameRate = defaultFrameRate;
                }


                line = reader.ReadLine();
                while (line != null)
                {
                    if (!string.IsNullOrEmpty(line))
                    {
                        var item = ParseLine(line, frameRate);
                        items.Add(item);
                    }
                    line = reader.ReadLine();
                }
            }

            if (items.Any())
            {
                return(items);
            }
            else
            {
                throw new ArgumentException("Stream is not in a valid MicroDVD format");
            }
        }
예제 #11
0
        public SubZir ParseStream(Stream stream, Encoding encoding)
        {
            stream.Position = 0;


            Regex        regexTimeCodes = new Regex(@"^\{T\ \d+:\d+:\d+:\d+$");
            StreamReader st             = new StreamReader(stream, encoding, true);
            string       reshte         = st.ReadToEnd();

            List <string> lines = new List <string>(reshte.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries));

            //{T 00:00:14:01
            //تقديم به تمام پارسي زبانان جهان
            //}
            //{T 00:00:20:97

            //}
            items.Clear();
            int    _errorCount = 0;
            bool   textOn      = false;
            string text        = string.Empty;
            double start       = 0;
            double end         = 0;

            foreach (string line in lines)
            {
                if (textOn)
                {
                    if (line.Trim() == "}")
                    {
                        SubtitleItem s = new SubtitleItem();
                        s.StartTime = int.Parse(start.ToString());
                        s.EndTime   = int.Parse(end.ToString());
                        s.Lines     = new List <string>()
                        {
                            text
                        };
                        items.Add(s);

                        text   = string.Empty;
                        start  = 0;
                        end    = 0;
                        textOn = false;
                    }
                    else
                    {
                        if (text.Length == 0)
                        {
                            text = line;
                        }
                        else
                        {
                            text += Environment.NewLine + line;
                        }
                    }
                }
                else
                {
                    if (regexTimeCodes.Match(line).Success)
                    {
                        try
                        {
                            textOn = true;
                            string[] arr = line.Substring(3).Trim().Split(':');
                            if (arr.Length == 4)
                            {
                                int hours        = int.Parse(arr[0]);
                                int minutes      = int.Parse(arr[1]);
                                int seconds      = int.Parse(arr[2]);
                                int milliseconds = int.Parse(arr[3]);
                                if (arr[3].Length == 2)
                                {
                                    milliseconds *= 10;
                                }
                                TimeSpan t = new TimeSpan(0, hours, minutes, seconds, milliseconds);
                                start = t.TotalMilliseconds;
                            }
                        }
                        catch
                        {
                            textOn = false;
                            _errorCount++;
                        }
                    }
                }
            }

            int index = 1;

            foreach (SubtitleItem s in items)
            {
                foreach (string line in s.Lines)
                {
                    SubtitleItem next = GetParagraphOrDefault(index);
                    if (next != null)
                    {
                        s.EndTime = next.StartTime;
                    }
                }
                index++;
            }

            int    counter    = 0;
            SubZir customList = new SubZir();

            foreach (var item in items)
            {
                if (counter % 2 == 0)
                {
                    customList.Add(item);
                }
                counter++;
            }
            items.Clear();
            items.AddRange(customList);

            if (items.Any())
            {
                return(items);
            }
            else
            {
                throw new ArgumentException("Stream is not in a valid 'DVD Subtitle' format");
            }
        }
예제 #12
0
        public SubZir ParseStream(Stream ssaStream, Encoding encoding)
        {
            if (!ssaStream.CanRead || !ssaStream.CanSeek)
            {
                var message = string.Format("Stream must be seekable and readable in a subtitles parser. " +
                                            "Operation interrupted; isSeekable: {0} - isReadable: {1}",
                                            ssaStream.CanSeek, ssaStream.CanSeek);
                throw new ArgumentException(message);
            }

            ssaStream.Position = 0;

            var reader = new StreamReader(ssaStream, encoding, true);

            var line       = reader.ReadLine();
            var lineNumber = 1;

            while (line != null && line != EventLine)
            {
                line = reader.ReadLine();
                lineNumber++;
            }

            if (line != null)
            {
                var headerLine = reader.ReadLine();
                if (!string.IsNullOrEmpty(headerLine))
                {
                    var columnHeaders = headerLine.Split(Separator).Select(head => head.Trim()).ToList();

                    var startIndexColumn = columnHeaders.IndexOf(StartColumn);
                    var endIndexColumn   = columnHeaders.IndexOf(EndColumn);
                    var textIndexColumn  = columnHeaders.IndexOf(TextColumn);

                    if (startIndexColumn > 0 && endIndexColumn > 0 && textIndexColumn > 0)
                    {
                        var items = new SubZir();

                        line = reader.ReadLine();
                        while (line != null)
                        {
                            if (!string.IsNullOrEmpty(line))
                            {
                                var columns   = line.Split(Separator);
                                var startText = columns[startIndexColumn];
                                var endText   = columns[endIndexColumn];


                                var textLine = string.Join(",", columns.Skip(textIndexColumn));

                                var start = ParseAssTimecode(startText);
                                var end   = ParseAssTimecode(endText);


                                if (start > 0 && end > 0 && !string.IsNullOrEmpty(textLine))
                                {
                                    var item = new SubtitleItem()
                                    {
                                        StartTime = start,
                                        EndTime   = end,
                                        Lines     = new List <string>()
                                        {
                                            textLine.Replace("\\N", Environment.NewLine).Replace("\\n", Environment.NewLine)
                                        }
                                    };
                                    items.Add(item);
                                }
                            }
                            line = reader.ReadLine();
                        }

                        if (items.Any())
                        {
                            return(items);
                        }
                        else
                        {
                            throw new ArgumentException("Stream is not in a valid Ssa format");
                        }
                    }
                    else
                    {
                        var message = string.Format("Couldn't find all the necessary columns " +
                                                    "headers ({0}, {1}, {2}) in header line {3}",
                                                    StartColumn, EndColumn, TextColumn, headerLine);
                        throw new ArgumentException(message);
                    }
                }
                else
                {
                    var message = string.Format("The header line after the line '{0}' was null -> " +
                                                "no need to continue parsing", line);
                    throw new ArgumentException(message);
                }
            }
            else
            {
                var message = string.Format("We reached line '{0}' with line number #{1} without finding to " +
                                            "Event section ({2})", line, lineNumber, EventLine);
                throw new ArgumentException(message);
            }
        }
예제 #13
0
        public SubZir ParseStream(Stream stream, Encoding encoding)
        {
            stream.Position = 0;
            SubZir        items          = new SubZir();
            Regex         regexTimeCodes = new Regex(@"^\[\d\d:\d\d:\d\d\]$");
            StreamReader  st             = new StreamReader(stream, encoding, true);
            string        reshte         = st.ReadToEnd();
            List <string> lines          = new List <string>(reshte.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries));

            var          sb        = new StringBuilder();
            SubtitleItem paragraph = new SubtitleItem();

            ExpectingLine expecting = ExpectingLine.TimeStart;

            int _errorCount = 0;

            foreach (string line in lines)
            {
                if (line.StartsWith("[") && regexTimeCodes.IsMatch(line))
                {
                    string[] parts = line.Split(new[] { ':', ']', '[', ' ' }, StringSplitOptions.RemoveEmptyEntries);
                    if (parts.Length == 3)
                    {
                        try
                        {
                            int startHours   = int.Parse(parts[0]);
                            int startMinutes = int.Parse(parts[1]);
                            int startSeconds = int.Parse(parts[2]);
                            var tc           = int.Parse(new TimeSpan(0, startHours, startMinutes, startSeconds, 0).TotalMilliseconds.ToString());
                            if (expecting == ExpectingLine.TimeStart)
                            {
                                paragraph           = new SubtitleItem();
                                paragraph.StartTime = tc;
                                expecting           = ExpectingLine.Text;
                            }
                            else if (expecting == ExpectingLine.TimeEnd)
                            {
                                paragraph.EndTime = tc;
                                expecting         = ExpectingLine.TimeStart;
                                items.Add(paragraph);
                                paragraph = new SubtitleItem();
                            }
                        }
                        catch
                        {
                            _errorCount++;
                            expecting = ExpectingLine.TimeStart;
                        }
                    }
                }
                else
                {
                    if (expecting == ExpectingLine.Text)
                    {
                        if (line.Length > 0)
                        {
                            string text = line.Replace("|", Environment.NewLine);
                            paragraph.Lines = new List <string>()
                            {
                                text
                            };
                            expecting = ExpectingLine.TimeEnd;
                        }
                    }
                }
            }

            if (items.Any())
            {
                return(items);
            }
            else
            {
                throw new ArgumentException("Stream is not in a valid 'SubViewer 1.0' Subtitle format");
            }
        }
        public SubZir ParseStream(Stream stream, Encoding encoding)
        {
            stream.Position = 0;
            SubZir        items          = new SubZir();
            Regex         regexTimeCodes = new Regex(@"^\d\d:\d\d:\d\d.\d+,\d\d:\d\d:\d\d.\d+$");
            StreamReader  st             = new StreamReader(stream, encoding, true);
            string        reshte         = st.ReadToEnd();
            List <string> lines          = new List <string>(reshte.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries));

            var           sb        = new StringBuilder();
            SubtitleItem  paragraph = new SubtitleItem();
            ExpectingLine expecting = ExpectingLine.TimeCodes;


            foreach (string line in lines)
            {
                if (regexTimeCodes.IsMatch(line))
                {
                    string[] parts = line.Split(new[] { ':', ',', '.' }, StringSplitOptions.RemoveEmptyEntries);
                    if (parts.Length == 8)
                    {
                        try
                        {
                            int startHours        = int.Parse(parts[0]);
                            int startMinutes      = int.Parse(parts[1]);
                            int startSeconds      = int.Parse(parts[2]);
                            int startMilliseconds = int.Parse(parts[3]);
                            int endHours          = int.Parse(parts[4]);
                            int endMinutes        = int.Parse(parts[5]);
                            int endSeconds        = int.Parse(parts[6]);
                            int endMilliseconds   = int.Parse(parts[7]);
                            paragraph.StartTime = int.Parse(new TimeSpan(0, startHours, startMinutes, startSeconds, startMilliseconds).TotalMilliseconds.ToString());
                            paragraph.EndTime   = int.Parse(new TimeSpan(0, endHours, endMinutes, endSeconds, endMilliseconds).TotalMilliseconds.ToString());
                            expecting           = ExpectingLine.Text;
                        }
                        catch
                        {
                            expecting = ExpectingLine.TimeCodes;
                        }
                    }
                }
                else
                {
                    if (expecting == ExpectingLine.Text)
                    {
                        if (line.Length > 0)
                        {
                            string text = line.Replace("[br]", Environment.NewLine);
                            text = text.Replace("{\\i1}", "<i>");
                            text = text.Replace("{\\i0}", "</i>");
                            text = text.Replace("{\\i}", "</i>");
                            text = text.Replace("{\\b1}", "<b>'");
                            text = text.Replace("{\\b0}", "</b>");
                            text = text.Replace("{\\b}", "</b>");
                            text = text.Replace("{\\u1}", "<u>");
                            text = text.Replace("{\\u0}", "</u>");
                            text = text.Replace("{\\u}", "</u>");
                            //<font color="">... آنچه گذشت</font>[br]{\i1}Ramtin{\b1}'Jokar{\b0}{\i0}[br]{\i1}SalAM{\i0}
                            text = text.Replace("[br]", Environment.NewLine);


                            paragraph.Lines = new List <string>()
                            {
                                text
                            };
                            items.Add(paragraph);
                            paragraph = new SubtitleItem();
                            expecting = ExpectingLine.TimeCodes;
                        }
                    }
                }
            }

            if (items.Any())
            {
                return(items);
            }
            else
            {
                throw new ArgumentException("Stream is not in a valid 'SubViewer 2.0' Subtitle format");
            }
        }
        public SubZir ParseStream(Stream stream, Encoding encoding)
        {
            stream.Position = 0;
            SubZir        items          = new SubZir();
            Regex         regexTimeCodes = new Regex(@"^\d\d:\d\d:\d\d\.\d\d\d[ \t]+\d\d:\d\d:\d\d\.\d\d\d[ \t]+\d\d:\d\d:\d\d\.\d\d\d[ \t]+");
            StreamReader  st             = new StreamReader(stream, encoding, true);
            string        reshte         = st.ReadToEnd();
            List <string> lines          = new List <string>(reshte.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries));

            SubtitleItem lastParagraph = null;
            int          _errorCount   = 0;

            foreach (string line in lines)
            {
                if (string.IsNullOrWhiteSpace(line))
                {
                    continue;
                }

                string s       = line;
                bool   success = false;

                if (s.Length > 26 && s.IndexOf(':') == 2)
                {
                    var match = regexTimeCodes.Match(s);
                    if (match.Success)
                    {
                        s = s.Substring(0, match.Length);
                        s = s.Replace("\t", ":");
                        s = s.Replace(".", ":");
                        s = s.Replace(" ", string.Empty);
                        s = s.Trim().TrimEnd(':').TrimEnd();
                        string[] parts = s.Split(':');
                        if (parts.Length == 12)
                        {
                            int hours        = int.Parse(parts[0]);
                            int minutes      = int.Parse(parts[1]);
                            int seconds      = int.Parse(parts[2]);
                            int milliseconds = int.Parse(parts[3]);
                            int start        = int.Parse(new TimeSpan(0, hours, minutes, seconds, milliseconds).TotalMilliseconds.ToString());

                            hours        = int.Parse(parts[4]);
                            minutes      = int.Parse(parts[5]);
                            seconds      = int.Parse(parts[6]);
                            milliseconds = int.Parse(parts[7]);
                            int end = int.Parse(new TimeSpan(0, hours, minutes, seconds, milliseconds).TotalMilliseconds.ToString());

                            string text = line.Substring(match.Length).TrimStart();
                            text = text.Replace("|", Environment.NewLine);

                            lastParagraph           = new SubtitleItem();
                            lastParagraph.StartTime = start;
                            lastParagraph.EndTime   = end;
                            lastParagraph.Lines     = new List <string>()
                            {
                                text
                            };
                            items.Add(lastParagraph);
                            success = true;
                        }
                    }
                }
                if (!success)
                {
                    _errorCount++;
                }
            }

            if (items.Any())
            {
                System.Diagnostics.Debug.WriteLine("items count: " + items.Count);
                return(items);
            }
            else
            {
                throw new ArgumentException("Stream is not in a valid 'Sony DVDArchitect Explicit duration' Subtitle format");
            }
        }