Beispiel #1
0
        public static SrtSubtitle ConvertDFXPToSubrip(string dfxpSubtitle)
        {
            var subtitle = new SrtSubtitle();

            dfxpSubtitle = dfxpSubtitle.Replace("<tt:br/>", "breakline;;");

            XmlDocument xmlDoc = new XmlDocument();

            xmlDoc.LoadXml(dfxpSubtitle);

            foreach (XmlElement div in xmlDoc["tt:tt"]["tt:body"].Cast <XmlElement>().Where(x => x.Name == "tt:div"))
            {
                foreach (XmlElement p in div)
                {
                    var srtItem = new SrtSubtitleItem();

                    srtItem.Start = DateTime.Parse(p.GetAttribute("begin"));
                    srtItem.End   = DateTime.Parse(p.GetAttribute("end"));

                    string text = p.InnerText.Replace("breakline;;", "\r\n");
                    srtItem.Text.AppendLine(text);
                    srtItem.Text.Append("\r\n");

                    subtitle.Items.Add(srtItem);
                }
            }

            return(subtitle);
        }
Beispiel #2
0
        void DownloadAndConvertSubtitle(SubtitleTrack subtitle)
        {
            string languageCode = subtitle.LanguageCode;
            string id           = subtitle.Id;

            Logger.Verbose($"Downloading {languageCode}_{id} subtitles");
            string outputFile = Path.Join(Constants.TEMP_FOLDER, string.Join("_", Filename, languageCode, id, Config.SubtitleFormat));

            if (Config.Overwrite && File.Exists(outputFile))
            {
                File.Delete(outputFile);
            }
            else if (!Config.Overwrite && File.Exists(outputFile))
            {
                return;
            }

            List <string> subtitleSegments = new List <string>(new string[subtitle.Segments]);

            Parallel.For(0, subtitle.Segments, (i) =>
            {
                subtitleSegments[i] = Requests.Request(subtitle.Urls[i], new Dictionary <string, string>()
                {
                    ["keep-alive"] = "timeout=10, max=1000"
                });
            });

            string      subtitleText = string.Join("\r\n", subtitleSegments);
            SrtSubtitle srtSubtitle  = null;

            if (subtitle.GetTrackType() == "webvtt")
            {
                srtSubtitle = SubtitleUtils.ConvertVTTToSubrip(string.Join("\r\n", subtitleSegments));
            }
            else if (subtitle.GetTrackType() == "dfxp")
            {
                srtSubtitle = SubtitleUtils.ConvertDFXPToSubrip(subtitleText);
            }

            if (srtSubtitle != null)
            {
                File.WriteAllText(outputFile, srtSubtitle.ToString());
            }
        }
Beispiel #3
0
        public static SrtSubtitle ConvertVTTToSubrip(string webvttSubtitle)
        {
            var subtitle = new SrtSubtitle();

            StringReader reader = new StringReader(webvttSubtitle);

            double timeOffset = 0.0;

            int    lineNumber = 1;
            string line;

            while ((line = reader.ReadLine()) != null)
            {
                if (line.Contains("X-TIMESTAMP-MAP=MPEGTS"))
                {
                    timeOffset = (double.Parse(line.Substring(line.IndexOf(":") + 1, line.IndexOf(",") - line.IndexOf(":") - 1)) - 900000) / 90000.0;
                }
                else if (IsTimecode(line))
                {
                    var srtItem = new SrtSubtitleItem();

                    lineNumber++;

                    line = line.Replace('.', ',');

                    line = DeleteCueSettings(line);

                    string   timeSrt1 = line.Substring(0, line.IndexOf('-'));
                    string   timeSrt2 = line.Substring(line.IndexOf('>') + 1);
                    DateTime timeAux1 = DateTime.ParseExact(timeSrt1.Trim(), "hh:mm:ss,fff", CultureInfo.InvariantCulture).AddSeconds(timeOffset);
                    DateTime timeAux2 = DateTime.ParseExact(timeSrt2.Trim(), "hh:mm:ss,fff", CultureInfo.InvariantCulture).AddSeconds(timeOffset);

                    srtItem.Start = timeAux1;
                    srtItem.End   = timeAux2;

                    bool foundCaption = false;
                    while (true)
                    {
                        if ((line = reader.ReadLine()) == null)
                        {
                            if (foundCaption)
                            {
                                break;
                            }
                            else
                            {
                                throw new Exception("Invalid file");
                            }
                        }
                        if (string.IsNullOrEmpty(line) || string.IsNullOrWhiteSpace(line))
                        {
                            srtItem.Text.AppendLine();
                            break;
                        }
                        foundCaption = true;
                        srtItem.Text.AppendLine(line);
                    }

                    subtitle.Items.Add(srtItem);
                }
            }

            return(subtitle);
        }