Example #1
0
        private void ButtonOpenSubtitle1Click(object sender, EventArgs e)
        {
            openFileDialog1.FileName = string.Empty;

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                if (FileUtil.IsVobSub(openFileDialog1.FileName) || FileUtil.IsBluRaySup(openFileDialog1.FileName))
                {
                    MessageBox.Show(Configuration.Settings.Language.CompareSubtitles.CannotCompareWithImageBasedSubtitles);
                    return;
                }
                _subtitle1 = new Subtitle();
                Encoding encoding;
                var format = _subtitle1.LoadSubtitle(openFileDialog1.FileName, out encoding, null);
                if (format == null)
                {
                    var pac = new Pac();
                    if (pac.IsMine(null, openFileDialog1.FileName))
                    {
                        pac.BatchMode = true;
                        pac.LoadSubtitle(_subtitle1, null, openFileDialog1.FileName);
                        format = pac;
                    }
                }
                if (format == null)
                {
                    var cavena890 = new Cavena890();
                    if (cavena890.IsMine(null, openFileDialog1.FileName))
                    {
                        cavena890.LoadSubtitle(_subtitle1, null, openFileDialog1.FileName);
                    }
                }
                if (format == null)
                {
                    var spt = new Spt();
                    if (spt.IsMine(null, openFileDialog1.FileName))
                    {
                        spt.LoadSubtitle(_subtitle1, null, openFileDialog1.FileName);
                    }
                }
                if (format == null)
                {
                    var cheetahCaption = new CheetahCaption();
                    if (cheetahCaption.IsMine(null, openFileDialog1.FileName))
                    {
                        cheetahCaption.LoadSubtitle(_subtitle1, null, openFileDialog1.FileName);
                    }
                }
                if (format == null)
                {
                    var chk = new Chk();
                    if (chk.IsMine(null, openFileDialog1.FileName))
                    {
                        chk.LoadSubtitle(_subtitle1, null, openFileDialog1.FileName);
                    }
                }
                if (format == null)
                {
                    var asc = new TimeLineAscii();
                    if (asc.IsMine(null, openFileDialog1.FileName))
                    {
                        asc.LoadSubtitle(_subtitle1, null, openFileDialog1.FileName);
                    }
                }
                if (format == null)
                {
                    var asc = new TimeLineFootageAscii();
                    if (asc.IsMine(null, openFileDialog1.FileName))
                    {
                        asc.LoadSubtitle(_subtitle1, null, openFileDialog1.FileName);
                    }
                }
                subtitleListView1.Fill(_subtitle1);
                subtitleListView1.SelectIndexAndEnsureVisible(0);
                subtitleListView2.SelectIndexAndEnsureVisible(0);
                labelSubtitle1.Text = openFileDialog1.FileName;
                _language1 = Utilities.AutoDetectGoogleLanguage(_subtitle1);
                if (_subtitle1.Paragraphs.Count > 0)
                    CompareSubtitles();
            }
        }
Example #2
0
        public override void LoadSubtitle(Subtitle subtitle, List <string> lines, string fileName)
        {
            Paragraph paragraph = null;
            var       expecting = ExpectingLine.Number;

            _errorCount = 0;
            byte firstLineCode  = 0;
            byte secondLineCode = 0;

            subtitle.Paragraphs.Clear();
            IEnumerable <byte[]> byteLines = TimeLineAscii.SplitBytesToLines(File.ReadAllBytes(fileName));

            foreach (byte[] bytes in byteLines)
            {
                var line = Encoding.ASCII.GetString(bytes);
                if (line.EndsWith('.') && Utilities.IsInteger(line.TrimEnd('.')))
                {
                    if (paragraph != null && !string.IsNullOrEmpty(paragraph.Text))
                    {
                        subtitle.Paragraphs.Add(paragraph);
                    }

                    paragraph = new Paragraph();
                    expecting = ExpectingLine.TimeStart;
                }
                else if (paragraph != null && expecting == ExpectingLine.TimeStart && RegexTimeCode.IsMatch(line))
                {
                    string[] parts = line.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                    if (parts.Length == 2)
                    {
                        try
                        {
                            var tc = DecodeTimeCode(parts);
                            paragraph.StartTime = tc;
                            expecting           = ExpectingLine.TimeEnd;
                        }
                        catch
                        {
                            _errorCount++;
                            expecting = ExpectingLine.Number;
                        }
                    }
                }
                else if (paragraph != null && expecting == ExpectingLine.TimeEnd && RegexTimeCode.IsMatch(line))
                {
                    string[] parts = line.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                    if (parts.Length == 2)
                    {
                        try
                        {
                            var tc = DecodeTimeCode(parts);
                            paragraph.EndTime = tc;
                            expecting         = ExpectingLine.Text;
                        }
                        catch
                        {
                            _errorCount++;
                            expecting = ExpectingLine.Number;
                        }
                    }
                }
                else
                {
                    if (paragraph != null && expecting == ExpectingLine.Text)
                    {
                        if (bytes.Length > 1)
                        {
                            // get text from encoding
                            var    enc = GetEncodingFromLanguage(bytes[0]);
                            string s   = enc.GetString(bytes, 1, bytes.Length - 1).Trim();

                            // italic text
                            if (s.StartsWith('#'))
                            {
                                s = "<i>" + s.Remove(0, 1) + "</i>";
                            }

                            paragraph.Text = (paragraph.Text + Environment.NewLine + s).Trim();
                            if (paragraph.Text.Length > 2000)
                            {
                                _errorCount += 100;
                                return;
                            }

                            if (paragraph.Text.Contains(Environment.NewLine))
                            {
                                if (secondLineCode == 0)
                                {
                                    secondLineCode = bytes[0];
                                }

                                if (secondLineCode != bytes[0])
                                {
                                    _errorCount++;
                                }
                            }
                            else
                            {
                                if (firstLineCode == 0)
                                {
                                    firstLineCode = bytes[0];
                                }

                                if (firstLineCode != bytes[0])
                                {
                                    _errorCount++;
                                }
                            }
                        }
                    }
                }
            }
            if (paragraph != null && !string.IsNullOrEmpty(paragraph.Text))
            {
                subtitle.Paragraphs.Add(paragraph);
            }

            subtitle.Renumber();
        }
Example #3
0
        private void OpenSubtitle(string fileName, Encoding encoding, string videoFileName, string originalFileName)
        {
            if (File.Exists(fileName))
            {
                bool videoFileLoaded = false;
                var file = new FileInfo(fileName);
                var ext = file.Extension.ToLowerInvariant();

                // save last first visible index + first selected index from listview
                if (!string.IsNullOrEmpty(_fileName))
                    Configuration.Settings.RecentFiles.Add(_fileName, FirstVisibleIndex, FirstSelectedIndex, _videoFileName, originalFileName);

                openFileDialog1.InitialDirectory = file.DirectoryName;

                if (ext == ".sub" && IsVobSubFile(fileName, false))
                {
                    if (MessageBox.Show(this, _language.ImportThisVobSubSubtitle, _title, MessageBoxButtons.YesNo) == DialogResult.Yes)
                    {
                        ImportAndOcrVobSubSubtitleNew(fileName, _loading);
                    }
                    return;
                }

                if (ext == ".sup")
                {
                    if (FileUtil.IsBluRaySup(fileName))
                    {
                        ImportAndOcrBluRaySup(fileName, _loading);
                        return;
                    }
                    else if (FileUtil.IsSpDvdSup(fileName))
                    {
                        ImportAndOcrSpDvdSup(fileName, _loading);
                        return;
                    }
                }

                if (ext == ".mkv" || ext == ".mks")
                {
                    ImportSubtitleFromMatroskaFile(fileName);
                    return;
                }

                if (ext == ".divx" || ext == ".avi")
                {
                    if (ImportSubtitleFromDivX(fileName))
                        return;
                }

                if ((ext == ".ts" || ext == ".rec" || ext == ".mpeg" || ext == ".mpg") && file.Length > 10000 && FileUtil.IsTransportStream(fileName))
                {
                    ImportSubtitleFromTransportStream(fileName);
                    return;
                }

                if (((ext == ".m2ts" || ext == ".ts") && file.Length > 10000 && FileUtil.IsM2TransportStream(fileName)) ||
                    (ext == ".textst" && FileUtil.IsMpeg2PrivateStream2(fileName)))
                {
                    bool isTextSt = false;
                    if (file.Length < 2000000)
                    {
                        var textSt = new TextST();
                        isTextSt = textSt.IsMine(null, fileName);
                    }

                    if (!isTextSt)
                    {
                        ImportSubtitleFromTransportStream(fileName);
                        return;
                    }
                }

                if ((ext == ".mp4" || ext == ".m4v" || ext == ".3gp") && file.Length > 10000)
                {
                    if (ImportSubtitleFromMp4(fileName))
                        OpenVideo(fileName);
                    return;
                }

                if (ext == ".mxf")
                {
                    if (FileUtil.IsMaterialExchangeFormat(fileName))
                    {
                        var parser = new MxfParser(fileName);
                        if (parser.IsValid)
                        {
                            var subtitles = parser.GetSubtitles();
                            if (subtitles.Count > 0)
                            {
                                SetEncoding(Configuration.Settings.General.DefaultEncoding);
                                encoding = GetCurrentEncoding();
                                var list = new List<string>(subtitles[0].SplitToLines());
                                _subtitle = new Subtitle();
                                var mxfFormat = _subtitle.ReloadLoadSubtitle(list, null, null);
                                SetCurrentFormat(mxfFormat);
                                _fileName = Path.GetFileNameWithoutExtension(fileName);
                                SetTitle();
                                ShowStatus(string.Format(_language.LoadedSubtitleX, _fileName));
                                _sourceViewChange = false;
                                _changeSubtitleToString = _subtitle.GetFastHashCode();
                                ResetHistory();
                                SetUndockedWindowsTitle();
                                _converted = true;
                                ShowStatus(string.Format(_language.LoadedSubtitleX, _fileName) + " - " + string.Format(_language.ConvertedToX, mxfFormat.FriendlyName));

                                ShowSource();
                                SubtitleListview1.Fill(_subtitle, _subtitleAlternate);
                                _subtitleListViewIndex = -1;
                                SubtitleListview1.FirstVisibleIndex = -1;
                                SubtitleListview1.SelectIndexAndEnsureVisible(0);

                                return;
                            }
                            MessageBox.Show("No subtitles found!");
                            return;
                        }
                    }
                }

                if (file.Length > 1024 * 1024 * 10) // max 10 mb
                {
                    // retry Blu-ray sup (file with wrong extension)
                    if (FileUtil.IsBluRaySup(fileName))
                    {
                        ImportAndOcrBluRaySup(fileName, _loading);
                        return;
                    }

                    // retry vobsub (file with wrong extension)
                    if (IsVobSubFile(fileName, false))
                    {
                        if (MessageBox.Show(this, _language.ImportThisVobSubSubtitle, _title, MessageBoxButtons.YesNo) == DialogResult.Yes)
                        {
                            ImportAndOcrVobSubSubtitleNew(fileName, _loading);
                        }
                        return;
                    }

                    var text = string.Format(_language.FileXIsLargerThan10MB + Environment.NewLine + Environment.NewLine + _language.ContinueAnyway, fileName);
                    if (MessageBox.Show(this, text, Title, MessageBoxButtons.YesNoCancel) != DialogResult.Yes)
                        return;
                }

                if (_subtitle.HistoryItems.Count > 0 || _subtitle.Paragraphs.Count > 0)
                    MakeHistoryForUndo(string.Format(_language.BeforeLoadOf, Path.GetFileName(fileName)));

                bool change = _changeSubtitleToString != _subtitle.GetFastHashCode();
                if (change)
                    change = _lastDoNotPrompt != _subtitle.GetFastHashCode();

                SubtitleFormat format = _subtitle.LoadSubtitle(fileName, out encoding, encoding);
                if (!change)
                    _changeSubtitleToString = _subtitle.GetFastHashCode();

                ShowHideTextBasedFeatures(format);

                bool justConverted = false;
                if (format == null)
                {
                    var ebu = new Ebu();
                    if (ebu.IsMine(null, fileName))
                    {
                        ebu.LoadSubtitle(_subtitle, null, fileName);
                        _oldSubtitleFormat = ebu;
                        SetCurrentFormat(Configuration.Settings.General.DefaultSubtitleFormat);
                        SetEncoding(Configuration.Settings.General.DefaultEncoding);
                        encoding = GetCurrentEncoding();
                        justConverted = true;
                        format = GetCurrentSubtitleFormat();
                    }
                }

                if (format == null)
                {
                    var pac = new Pac();
                    if (pac.IsMine(null, fileName))
                    {
                        pac.LoadSubtitle(_subtitle, null, fileName);
                        _oldSubtitleFormat = pac;
                        SetCurrentFormat(Configuration.Settings.General.DefaultSubtitleFormat);
                        SetEncoding(Configuration.Settings.General.DefaultEncoding);
                        encoding = GetCurrentEncoding();
                        justConverted = true;
                        format = GetCurrentSubtitleFormat();
                    }
                }

                if (ext == ".m2ts" || ext == ".textst")
                {
                    var textST = new TextST();
                    if (textST.IsMine(null, fileName))
                    {
                        textST.LoadSubtitle(_subtitle, null, fileName);
                        _oldSubtitleFormat = textST;
                        SetCurrentFormat(Configuration.Settings.General.DefaultSubtitleFormat);
                        SetEncoding(Configuration.Settings.General.DefaultEncoding);
                        encoding = GetCurrentEncoding();
                        justConverted = true;
                        format = GetCurrentSubtitleFormat();
                    }
                }

                if (format == null)
                {
                    var pns = new Pns();
                    if (pns.IsMine(null, fileName))
                    {
                        pns.LoadSubtitle(_subtitle, null, fileName);
                        _oldSubtitleFormat = pns;
                        SetCurrentFormat(Configuration.Settings.General.DefaultSubtitleFormat);
                        SetEncoding(Configuration.Settings.General.DefaultEncoding);
                        encoding = GetCurrentEncoding();
                        justConverted = true;
                        format = GetCurrentSubtitleFormat();
                    }
                }


                if (format == null)
                {
                    var cavena890 = new Cavena890();
                    if (cavena890.IsMine(null, fileName))
                    {
                        cavena890.LoadSubtitle(_subtitle, null, fileName);
                        _oldSubtitleFormat = cavena890;
                        SetCurrentFormat(Configuration.Settings.General.DefaultSubtitleFormat);
                        SetEncoding(Configuration.Settings.General.DefaultEncoding);
                        encoding = GetCurrentEncoding();
                        justConverted = true;
                        format = GetCurrentSubtitleFormat();
                    }
                }

                if (format == null)
                {
                    var spt = new Spt();
                    if (spt.IsMine(null, fileName))
                    {
                        spt.LoadSubtitle(_subtitle, null, fileName);
                        _oldSubtitleFormat = spt;
                        SetCurrentFormat(Configuration.Settings.General.DefaultSubtitleFormat);
                        SetEncoding(Configuration.Settings.General.DefaultEncoding);
                        encoding = GetCurrentEncoding();
                        justConverted = true;
                        format = GetCurrentSubtitleFormat();
                    }
                }

                if (format == null && ext == ".wsb")
                {
                    var wsb = new Wsb();
                    var list = new List<string>(File.ReadAllLines(fileName, LanguageAutoDetect.GetEncodingFromFile(fileName)));
                    if (wsb.IsMine(list, fileName))
                    {
                        wsb.LoadSubtitle(_subtitle, list, fileName);
                        _oldSubtitleFormat = wsb;
                        SetCurrentFormat(Configuration.Settings.General.DefaultSubtitleFormat);
                        SetEncoding(Configuration.Settings.General.DefaultEncoding);
                        encoding = GetCurrentEncoding();
                        justConverted = true;
                        format = GetCurrentSubtitleFormat();
                    }
                }

                if (format == null)
                {
                    var cheetahCaption = new CheetahCaption();
                    if (cheetahCaption.IsMine(null, fileName))
                    {
                        cheetahCaption.LoadSubtitle(_subtitle, null, fileName);
                        _oldSubtitleFormat = cheetahCaption;
                        SetCurrentFormat(Configuration.Settings.General.DefaultSubtitleFormat);
                        SetEncoding(Configuration.Settings.General.DefaultEncoding);
                        encoding = GetCurrentEncoding();
                        justConverted = true;
                        format = GetCurrentSubtitleFormat();
                    }
                }

                if (format == null)
                {
                    var capMakerPlus = new CapMakerPlus();
                    if (capMakerPlus.IsMine(null, fileName))
                    {
                        capMakerPlus.LoadSubtitle(_subtitle, null, fileName);
                        _oldSubtitleFormat = capMakerPlus;
                        SetCurrentFormat(Configuration.Settings.General.DefaultSubtitleFormat);
                        SetEncoding(Configuration.Settings.General.DefaultEncoding);
                        encoding = GetCurrentEncoding();
                        justConverted = true;
                        format = GetCurrentSubtitleFormat();
                    }
                }

                if (format == null)
                {
                    var captionsInc = new CaptionsInc();
                    if (captionsInc.IsMine(null, fileName))
                    {
                        captionsInc.LoadSubtitle(_subtitle, null, fileName);
                        _oldSubtitleFormat = captionsInc;
                        SetCurrentFormat(Configuration.Settings.General.DefaultSubtitleFormat);
                        SetEncoding(Configuration.Settings.General.DefaultEncoding);
                        encoding = GetCurrentEncoding();
                        justConverted = true;
                        format = GetCurrentSubtitleFormat();
                    }
                }

                if (format == null)
                {
                    var ultech130 = new Ultech130();
                    if (ultech130.IsMine(null, fileName))
                    {
                        ultech130.LoadSubtitle(_subtitle, null, fileName);
                        _oldSubtitleFormat = ultech130;
                        SetCurrentFormat(Configuration.Settings.General.DefaultSubtitleFormat);
                        SetEncoding(Configuration.Settings.General.DefaultEncoding);
                        encoding = GetCurrentEncoding();
                        justConverted = true;
                        format = GetCurrentSubtitleFormat();
                    }
                }

                if (format == null)
                {
                    var nciCaption = new NciCaption();
                    if (nciCaption.IsMine(null, fileName))
                    {
                        nciCaption.LoadSubtitle(_subtitle, null, fileName);
                        _oldSubtitleFormat = nciCaption;
                        SetCurrentFormat(Configuration.Settings.General.DefaultSubtitleFormat);
                        SetEncoding(Configuration.Settings.General.DefaultEncoding);
                        encoding = GetCurrentEncoding();
                        justConverted = true;
                        format = GetCurrentSubtitleFormat();
                    }
                }

                if (format == null)
                {
                    var tsb4 = new TSB4();
                    if (tsb4.IsMine(null, fileName))
                    {
                        tsb4.LoadSubtitle(_subtitle, null, fileName);
                        _oldSubtitleFormat = tsb4;
                        SetCurrentFormat(Configuration.Settings.General.DefaultSubtitleFormat);
                        SetEncoding(Configuration.Settings.General.DefaultEncoding);
                        encoding = GetCurrentEncoding();
                        justConverted = true;
                        format = GetCurrentSubtitleFormat();
                    }
                }

                if (format == null)
                {
                    var avidStl = new AvidStl();
                    if (avidStl.IsMine(null, fileName))
                    {
                        avidStl.LoadSubtitle(_subtitle, null, fileName);
                        _oldSubtitleFormat = avidStl;
                        SetCurrentFormat(Configuration.Settings.General.DefaultSubtitleFormat);
                        SetEncoding(Configuration.Settings.General.DefaultEncoding);
                        encoding = GetCurrentEncoding();
                        justConverted = true;
                        format = GetCurrentSubtitleFormat();
                    }
                }

                if (format == null)
                {
                    var chk = new Chk();
                    if (chk.IsMine(null, fileName))
                    {
                        chk.LoadSubtitle(_subtitle, null, fileName);
                        _oldSubtitleFormat = chk;
                        SetCurrentFormat(Configuration.Settings.General.DefaultSubtitleFormat);
                        SetEncoding(Configuration.Settings.General.DefaultEncoding);
                        encoding = GetCurrentEncoding();
                        justConverted = true;
                        format = GetCurrentSubtitleFormat();
                    }
                }

                if (format == null)
                {
                    var ayato = new Ayato();
                    if (ayato.IsMine(null, fileName))
                    {
                        ayato.LoadSubtitle(_subtitle, null, fileName);
                        _oldSubtitleFormat = ayato;
                        SetCurrentFormat(Configuration.Settings.General.DefaultSubtitleFormat);
                        SetEncoding(Configuration.Settings.General.DefaultEncoding);
                        encoding = GetCurrentEncoding();
                        justConverted = true;
                        format = GetCurrentSubtitleFormat();
                    }
                }

                if (format == null)
                {
                    var pacUnicode = new PacUnicode();
                    if (pacUnicode.IsMine(null, fileName))
                    {
                        pacUnicode.LoadSubtitle(_subtitle, null, fileName);
                        _oldSubtitleFormat = pacUnicode;
                        SetCurrentFormat(Configuration.Settings.General.DefaultSubtitleFormat);
                        SetEncoding(Configuration.Settings.General.DefaultEncoding);
                        encoding = GetCurrentEncoding();
                        justConverted = true;
                        format = GetCurrentSubtitleFormat();
                    }
                }

                if (format == null)
                {
                    var iai = new IaiSub();
                    if (iai.IsMine(null, fileName))
                    {
                        iai.LoadSubtitle(_subtitle, null, fileName);
                        _oldSubtitleFormat = iai;
                        SetCurrentFormat(Configuration.Settings.General.DefaultSubtitleFormat);
                        SetEncoding(Configuration.Settings.General.DefaultEncoding);
                        encoding = GetCurrentEncoding();
                        justConverted = true;
                        format = GetCurrentSubtitleFormat();
                    }
                }

                if (format == null)
                {
                    try
                    {
                        var bdnXml = new BdnXml();
                        var list = new List<string>(File.ReadAllLines(fileName, LanguageAutoDetect.GetEncodingFromFile(fileName)));
                        if (bdnXml.IsMine(list, fileName))
                        {
                            if (ContinueNewOrExit())
                            {
                                ImportAndOcrBdnXml(fileName, bdnXml, list);
                            }
                            return;
                        }
                    }
                    catch
                    {
                        format = null;
                    }
                }

                if (format == null)
                {
                    try
                    {
                        var fcpImage = new FinalCutProImage();
                        var list = new List<string>(File.ReadAllLines(fileName, LanguageAutoDetect.GetEncodingFromFile(fileName)));
                        if (fcpImage.IsMine(list, fileName))
                        {
                            if (ContinueNewOrExit())
                            {
                                ImportAndOcrDost(fileName, fcpImage, list);
                            }
                            return;
                        }
                    }
                    catch
                    {
                        format = null;
                    }
                }

                if (format == null)
                {
                    var elr = new ELRStudioClosedCaption();
                    if (elr.IsMine(null, fileName))
                    {
                        elr.LoadSubtitle(_subtitle, null, fileName);
                        _oldSubtitleFormat = elr;
                        SetCurrentFormat(Configuration.Settings.General.DefaultSubtitleFormat);
                        SetEncoding(Configuration.Settings.General.DefaultEncoding);
                        encoding = GetCurrentEncoding();
                        justConverted = true;
                        format = GetCurrentSubtitleFormat();
                    }
                }

                if (format == null)
                {
                    var asc = new TimeLineAscii();
                    if (asc.IsMine(null, fileName))
                    {
                        asc.LoadSubtitle(_subtitle, null, fileName);
                        _oldSubtitleFormat = asc;
                        SetCurrentFormat(Configuration.Settings.General.DefaultSubtitleFormat);
                        SetEncoding(Configuration.Settings.General.DefaultEncoding);
                        encoding = GetCurrentEncoding();
                        justConverted = true;
                        format = GetCurrentSubtitleFormat();
                    }
                }

                if (format == null)
                {
                    var asc = new TimeLineFootageAscii();
                    if (asc.IsMine(null, fileName))
                    {
                        asc.LoadSubtitle(_subtitle, null, fileName);
                        _oldSubtitleFormat = asc;
                        SetCurrentFormat(Configuration.Settings.General.DefaultSubtitleFormat);
                        SetEncoding(Configuration.Settings.General.DefaultEncoding);
                        encoding = GetCurrentEncoding();
                        justConverted = true;
                        format = GetCurrentSubtitleFormat();
                    }
                }

                if (format == null)
                {
                    var mtv = new TimeLineMvt();
                    if (mtv.IsMine(null, fileName))
                    {
                        mtv.LoadSubtitle(_subtitle, null, fileName);
                        _oldSubtitleFormat = mtv;
                        SetCurrentFormat(Configuration.Settings.General.DefaultSubtitleFormat);
                        SetEncoding(Configuration.Settings.General.DefaultEncoding);
                        encoding = GetCurrentEncoding();
                        justConverted = true;
                        format = GetCurrentSubtitleFormat();
                    }
                }

                if (ext == ".dost")
                {
                    try
                    {
                        var dost = new Dost();
                        var list = new List<string>(File.ReadAllLines(fileName, LanguageAutoDetect.GetEncodingFromFile(fileName)));
                        if (dost.IsMine(list, fileName))
                        {
                            if (ContinueNewOrExit())
                                ImportAndOcrDost(fileName, dost, list);
                            return;
                        }
                    }
                    catch
                    {
                        format = null;
                    }
                }

                if (format == null)
                {
                    try
                    {
                        var timedtextImage = new TimedTextImage();
                        var list = new List<string>(File.ReadAllLines(fileName, LanguageAutoDetect.GetEncodingFromFile(fileName)));
                        if (timedtextImage.IsMine(list, fileName))
                        {
                            if (ContinueNewOrExit())
                                ImportAndOcrDost(fileName, timedtextImage, list);
                            return;
                        }
                    }
                    catch
                    {
                        format = null;
                    }
                }

                if (format == null)
                {
                    try
                    {
                        var seImageHtmlIndex = new SeImageHtmlIndex();
                        var list = new List<string>(File.ReadAllLines(fileName, LanguageAutoDetect.GetEncodingFromFile(fileName)));
                        if (seImageHtmlIndex.IsMine(list, fileName))
                        {
                            if (ContinueNewOrExit())
                                ImportAndOcrDost(fileName, seImageHtmlIndex, list);
                            return;
                        }
                    }
                    catch
                    {
                        format = null;
                    }
                }

                if (format == null || format.Name == Scenarist.NameOfFormat)
                {
                    try
                    {
                        var son = new Son();
                        var list = new List<string>(File.ReadAllLines(fileName, LanguageAutoDetect.GetEncodingFromFile(fileName)));
                        if (son.IsMine(list, fileName))
                        {
                            if (ContinueNewOrExit())
                                ImportAndOcrSon(fileName, son, list);
                            return;
                        }
                    }
                    catch
                    {
                        format = null;
                    }
                }

                if (format == null || format.Name == SubRip.NameOfFormat)
                {
                    if (_subtitle.Paragraphs.Count > 1)
                    {
                        int imageCount = 0;
                        foreach (var p in _subtitle.Paragraphs)
                        {
                            string s = p.Text.ToLowerInvariant();
                            if (s.EndsWith(".bmp", StringComparison.Ordinal) || s.EndsWith(".png", StringComparison.Ordinal) || s.EndsWith(".jpg", StringComparison.Ordinal) || s.EndsWith(".tif", StringComparison.Ordinal))
                            {
                                imageCount++;
                            }
                        }
                        if (imageCount > 2 && imageCount >= _subtitle.Paragraphs.Count - 2)
                        {
                            if (ContinueNewOrExit())
                                ImportAndOcrSrt(_subtitle);
                            return;
                        }
                    }
                }

                if (format == null)
                {
                    try
                    {
                        var satBoxPng = new SatBoxPng();
                        var list = new List<string>(File.ReadAllLines(fileName, LanguageAutoDetect.GetEncodingFromFile(fileName)));
                        if (satBoxPng.IsMine(list, fileName))
                        {
                            var subtitle = new Subtitle();
                            satBoxPng.LoadSubtitle(subtitle, list, fileName);
                            if (ContinueNewOrExit())
                                ImportAndOcrSrt(subtitle);
                            return;
                        }
                    }
                    catch
                    {
                        format = null;
                    }
                }

                if (format == null || format.Name == Scenarist.NameOfFormat)
                {
                    try
                    {
                        var sst = new SonicScenaristBitmaps();
                        var list = new List<string>(File.ReadAllLines(fileName, LanguageAutoDetect.GetEncodingFromFile(fileName)));
                        if (sst.IsMine(list, fileName))
                        {
                            if (ContinueNewOrExit())
                                ImportAndOcrSst(fileName, sst, list);
                            return;
                        }
                    }
                    catch
                    {
                        format = null;
                    }
                }

                if (format == null)
                {
                    try
                    {
                        var htmlSamiArray = new HtmlSamiArray();
                        var list = new List<string>(File.ReadAllLines(fileName, LanguageAutoDetect.GetEncodingFromFile(fileName)));
                        if (htmlSamiArray.IsMine(list, fileName))
                        {
                            htmlSamiArray.LoadSubtitle(_subtitle, list, fileName);
                            _oldSubtitleFormat = htmlSamiArray;
                            SetCurrentFormat(Configuration.Settings.General.DefaultSubtitleFormat);
                            SetEncoding(Configuration.Settings.General.DefaultEncoding);
                            encoding = GetCurrentEncoding();
                            justConverted = true;
                            format = GetCurrentSubtitleFormat();
                        }
                    }
                    catch
                    {
                        format = null;
                    }
                }

                // retry vobsub (file with wrong extension)
                if (format == null && file.Length > 500 && IsVobSubFile(fileName, false))
                {
                    if (MessageBox.Show(this, _language.ImportThisVobSubSubtitle, _title, MessageBoxButtons.YesNo) == DialogResult.Yes)
                    {
                        ImportAndOcrVobSubSubtitleNew(fileName, _loading);
                    }
                    return;
                }

                // retry Blu-ray (file with wrong extension)
                if (format == null && file.Length > 500 && FileUtil.IsBluRaySup(fileName))
                {
                    ImportAndOcrBluRaySup(fileName, _loading);
                    return;
                }

                // retry SP DVD (file with wrong extension)
                if (format == null && file.Length > 500 && FileUtil.IsSpDvdSup(fileName))
                {
                    ImportAndOcrSpDvdSup(fileName, _loading);
                    return;
                }

                // retry Matroska (file with wrong extension)
                if (format == null && !string.IsNullOrWhiteSpace(fileName))
                {
                    var matroska = new MatroskaFile(fileName);
                    if (matroska.IsValid)
                    {
                        var subtitleList = matroska.GetTracks(true);
                        if (subtitleList.Count > 0)
                        {
                            ImportSubtitleFromMatroskaFile(fileName);
                            return;
                        }
                    }
                }

                // check for idx file
                if (format == null && file.Length > 100 && ext == ".idx")
                {
                    MessageBox.Show(_language.ErrorLoadIdx);
                    return;
                }

                // check for .rar file
                if (format == null && file.Length > 100 && FileUtil.IsRar(fileName))
                {
                    MessageBox.Show(_language.ErrorLoadRar);
                    return;
                }

                // check for .zip file
                if (format == null && file.Length > 100 && FileUtil.IsZip(fileName))
                {
                    MessageBox.Show(_language.ErrorLoadZip);
                    return;
                }

                // check for .png file
                if (format == null && file.Length > 100 && FileUtil.IsPng(fileName))
                {
                    MessageBox.Show(_language.ErrorLoadPng);
                    return;
                }

                // check for .jpg file
                if (format == null && file.Length > 100 && FileUtil.IsJpg(fileName))
                {
                    MessageBox.Show(_language.ErrorLoadJpg);
                    return;
                }

                // check for .srr file
                if (format == null && file.Length > 100 && ext == ".srr" && FileUtil.IsSrr(fileName))
                {
                    MessageBox.Show(_language.ErrorLoadSrr);
                    return;
                }

                // check for Torrent file
                if (format == null && file.Length > 50 && FileUtil.IsTorrentFile(fileName))
                {
                    MessageBox.Show(_language.ErrorLoadTorrent);
                    return;
                }

                // check for all binary zeroes (I've heard about this a few times... perhaps related to crashes?)
                if (format == null && file.Length > 50 && FileUtil.IsSubtitleFileAllBinaryZeroes(fileName))
                {
                    MessageBox.Show(_language.ErrorLoadBinaryZeroes);
                    return;
                }

                if (format == null && file.Length < 100 * 1000000 && TransportStreamParser.IsDvbSup(fileName))
                {
                    ImportSubtitleFromDvbSupFile(fileName);
                    return;
                }

                if (format == null && file.Length < 500000)
                {
                    // check for valid timed text
                    if (ext == ".xml" || ext == ".dfxp")
                    {
                        var sb = new StringBuilder();
                        foreach (var line in File.ReadAllLines(fileName, LanguageAutoDetect.GetEncodingFromFile(fileName)))
                            sb.AppendLine(line);
                        var xmlAsString = sb.ToString().Trim();

                        if (xmlAsString.Contains("http://www.w3.org/ns/ttml") && xmlAsString.Contains("<?xml version=") ||
                            xmlAsString.Contains("http://www.w3.org/") && xmlAsString.Contains("/ttaf1"))
                        {
                            var xml = new System.Xml.XmlDocument();
                            try
                            {
                                xml.LoadXml(xmlAsString);
                            }
                            catch (Exception ex)
                            {
                                MessageBox.Show("Timed text is not valid (xml is not well-formed): " + ex.Message);
                                return;
                            }
                        }
                    }


                    // Try to use a generic subtitle format parser (guessing subtitle format)
                    try
                    {
                        var enc = LanguageAutoDetect.GetEncodingFromFile(fileName);
                        var s = File.ReadAllText(fileName, enc);

                        // check for RTF file
                        if (ext == ".rtf" && s.TrimStart().StartsWith("{\\rtf", StringComparison.Ordinal))
                        {
                            using (var rtb = new RichTextBox { Rtf = s })
                            {
                                s = rtb.Text;
                            }
                        }
                        var uknownFormatImporter = new UknownFormatImporter { UseFrames = true };
                        var genericParseSubtitle = uknownFormatImporter.AutoGuessImport(s.SplitToLines());
                        if (genericParseSubtitle.Paragraphs.Count > 1)
                        {
                            _subtitle = genericParseSubtitle;
                            SetCurrentFormat(Configuration.Settings.General.DefaultSubtitleFormat);
                            SetEncoding(Configuration.Settings.General.DefaultEncoding);
                            encoding = GetCurrentEncoding();
                            justConverted = true;
                            format = GetCurrentSubtitleFormat();
                            ShowStatus("Guessed subtitle format via generic subtitle parser!");
                        }
                    }
                    catch
                    {
                    }
                }

                _fileDateTime = File.GetLastWriteTime(fileName);

                if (format != null && format.IsFrameBased)
                    _subtitle.CalculateTimeCodesFromFrameNumbers(CurrentFrameRate);
                else
                    _subtitle.CalculateFrameNumbersFromTimeCodes(CurrentFrameRate);

                if (format != null)
                {
                    if (Configuration.Settings.General.RemoveBlankLinesWhenOpening)
                    {
                        _subtitle.RemoveEmptyLines();
                    }

                    foreach (var p in _subtitle.Paragraphs)
                    {
                        // Replace U+0456 (CYRILLIC SMALL LETTER BYELORUSSIAN-UKRAINIAN I) by U+0069 (LATIN SMALL LETTER I)
                        p.Text = p.Text.Replace("<і>", "<i>").Replace("</і>", "</i>");
                    }

                    _subtitleListViewIndex = -1;
                    SetCurrentFormat(format);
                    _subtitleAlternateFileName = null;
                    if (LoadAlternateSubtitleFile(originalFileName))
                        _subtitleAlternateFileName = originalFileName;

                    // Seungki begin
                    _splitDualSami = false;
                    if (Configuration.Settings.SubtitleSettings.SamiDisplayTwoClassesAsTwoSubtitles && format.GetType() == typeof(Sami) && Sami.GetStylesFromHeader(_subtitle.Header).Count == 2)
                    {
                        var classes = Sami.GetStylesFromHeader(_subtitle.Header);
                        var s1 = new Subtitle(_subtitle);
                        var s2 = new Subtitle(_subtitle);
                        s1.Paragraphs.Clear();
                        s2.Paragraphs.Clear();
                        foreach (var p in _subtitle.Paragraphs)
                        {
                            if (p.Extra != null && p.Extra.Equals(classes[0], StringComparison.OrdinalIgnoreCase))
                                s1.Paragraphs.Add(p);
                            else
                                s2.Paragraphs.Add(p);
                        }
                        if (s1.Paragraphs.Count == 0 || s2.Paragraphs.Count == 0)
                            return;

                        _subtitle = s1;
                        _subtitleAlternate = s2;
                        _subtitleAlternateFileName = _fileName;
                        SubtitleListview1.HideExtraColumn();
                        SubtitleListview1.ShowAlternateTextColumn(classes[1]);
                        _splitDualSami = true;
                    }
                    // Seungki end

                    textBoxSource.Text = _subtitle.ToText(format);
                    SubtitleListview1.Fill(_subtitle, _subtitleAlternate);
                    if (SubtitleListview1.Items.Count > 0)
                        SubtitleListview1.Items[0].Selected = true;
                    _findHelper = null;
                    _spellCheckForm = null;

                    if (_resetVideo)
                    {
                        _videoFileName = null;
                        _videoInfo = null;
                        _videoAudioTrackNumber = -1;
                        labelVideoInfo.Text = _languageGeneral.NoVideoLoaded;
                        audioVisualizer.WavePeaks = null;
                        audioVisualizer.Spectrogram = null;
                    }

                    if (Configuration.Settings.General.ShowVideoPlayer || Configuration.Settings.General.ShowAudioVisualizer)
                    {
                        if (!Configuration.Settings.General.DisableVideoAutoLoading)
                        {
                            if (!string.IsNullOrEmpty(videoFileName) && File.Exists(videoFileName))
                            {
                                OpenVideo(videoFileName);
                            }
                            else if (!string.IsNullOrEmpty(fileName) && (toolStripButtonToggleVideo.Checked || toolStripButtonToggleWaveform.Checked))
                            {
                                TryToFindAndOpenVideoFile(Path.Combine(Path.GetDirectoryName(fileName), Path.GetFileNameWithoutExtension(fileName)));
                            }
                        }
                    }
                    videoFileLoaded = _videoFileName != null;

                    if (Configuration.Settings.RecentFiles.Files.Count > 0 &&
                        Configuration.Settings.RecentFiles.Files[0].FileName == fileName)
                    {
                    }
                    else
                    {
                        Configuration.Settings.RecentFiles.Add(fileName, _videoFileName, _subtitleAlternateFileName);
                        Configuration.Settings.Save();
                        UpdateRecentFilesUI();
                    }
                    _fileName = fileName;
                    SetTitle();
                    ShowStatus(string.Format(_language.LoadedSubtitleX, _fileName));
                    _sourceViewChange = false;
                    _changeSubtitleToString = _subtitle.GetFastHashCode();
                    _converted = false;
                    ResetHistory();

                    SetUndockedWindowsTitle();

                    if (justConverted)
                    {
                        _converted = true;
                        ShowStatus(string.Format(_language.LoadedSubtitleX, _fileName) + " - " + string.Format(_language.ConvertedToX, format.FriendlyName));
                    }
                    if (Configuration.Settings.General.AutoConvertToUtf8)
                        encoding = Encoding.UTF8;
                    SetEncoding(encoding);

                    var formatType = format.GetType();
                    if (formatType == typeof(SubStationAlpha))
                    {
                        string errors = AdvancedSubStationAlpha.CheckForErrors(_subtitle.Header);
                        if (!string.IsNullOrEmpty(errors))
                            MessageBox.Show(this, errors, Title, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        errors = (format as SubStationAlpha).Errors;
                        if (!string.IsNullOrEmpty(errors))
                            MessageBox.Show(this, errors, Title, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                    else if (formatType == typeof(AdvancedSubStationAlpha))
                    {
                        string errors = AdvancedSubStationAlpha.CheckForErrors(_subtitle.Header);
                        if (!string.IsNullOrEmpty(errors))
                            MessageBox.Show(this, errors, Title, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        errors = (format as AdvancedSubStationAlpha).Errors;
                        if (!string.IsNullOrEmpty(errors))
                            MessageBox.Show(this, errors, Title, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                    else if (formatType == typeof(SubRip))
                    {
                        string errors = (format as SubRip).Errors;
                        if (!string.IsNullOrEmpty(errors))
                            MessageBox.Show(this, errors, Title, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                    else if (formatType == typeof(MicroDvd))
                    {
                        string errors = (format as MicroDvd).Errors;
                        if (!string.IsNullOrEmpty(errors))
                            MessageBox.Show(this, errors, Title, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                    else if (formatType == typeof(DCinemaSmpte2007))
                    {
                        format.ToText(_subtitle, string.Empty);
                        string errors = (format as DCinemaSmpte2007).Errors;
                        if (!string.IsNullOrEmpty(errors))
                            MessageBox.Show(errors, Title, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                    else if (formatType == typeof(DCinemaSmpte2010))
                    {
                        format.ToText(_subtitle, string.Empty);
                        string errors = (format as DCinemaSmpte2010).Errors;
                        if (!string.IsNullOrEmpty(errors))
                            MessageBox.Show(errors, Title, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                }
                else
                {
                    if (file.Length < 50)
                    {
                        _findHelper = null;
                        _spellCheckForm = null;
                        _videoFileName = null;
                        _videoInfo = null;
                        _videoAudioTrackNumber = -1;
                        labelVideoInfo.Text = _languageGeneral.NoVideoLoaded;
                        audioVisualizer.WavePeaks = null;
                        audioVisualizer.Spectrogram = null;

                        Configuration.Settings.RecentFiles.Add(fileName, FirstVisibleIndex, FirstSelectedIndex, _videoFileName, _subtitleAlternateFileName);
                        Configuration.Settings.Save();
                        UpdateRecentFilesUI();
                        _fileName = fileName;
                        SetTitle();
                        ShowStatus(string.Format(_language.LoadedEmptyOrShort, _fileName));
                        _sourceViewChange = false;
                        _converted = false;

                        MessageBox.Show(_language.FileIsEmptyOrShort);
                    }
                    else
                    {
                        ShowUnknownSubtitle();
                        return;
                    }
                }

                if (!videoFileLoaded && mediaPlayer.VideoPlayer != null)
                {
                    mediaPlayer.VideoPlayer.DisposeVideoPlayer();
                    mediaPlayer.VideoPlayer = null;
                    timer1.Stop();
                }
                ResetShowEarlierOrLater();
            }
            else
            {
                MessageBox.Show(string.Format(_language.FileNotFound, fileName));
            }
        }
Example #4
0
        private void AddInputFile(string fileName)
        {
            try
            {
                foreach (ListViewItem lvi in listViewInputFiles.Items)
                {
                    if (lvi.Text.Equals(fileName, StringComparison.OrdinalIgnoreCase))
                        return;
                }

                var fi = new FileInfo(fileName);
                var ext = fi.Extension;
                var item = new ListViewItem(fileName);
                item.SubItems.Add(Utilities.FormatBytesToDisplayFileSize(fi.Length));

                SubtitleFormat format = null;
                var sub = new Subtitle();
                if (fi.Length < 1024 * 1024 * 10) // max 10 mb
                {
                    Encoding encoding;
                    format = sub.LoadSubtitle(fileName, out encoding, null);

                    if (format == null)
                    {
                        var ebu = new Ebu();
                        if (ebu.IsMine(null, fileName))
                        {
                            format = ebu;
                        }
                    }
                    if (format == null)
                    {
                        var pac = new Pac();
                        if (pac.IsMine(null, fileName))
                        {
                            format = pac;
                        }
                    }
                    if (format == null)
                    {
                        var cavena890 = new Cavena890();
                        if (cavena890.IsMine(null, fileName))
                        {
                            format = cavena890;
                        }
                    }
                    if (format == null)
                    {
                        var spt = new Spt();
                        if (spt.IsMine(null, fileName))
                        {
                            format = spt;
                        }
                    }
                    if (format == null)
                    {
                        var cheetahCaption = new CheetahCaption();
                        if (cheetahCaption.IsMine(null, fileName))
                        {
                            format = cheetahCaption;
                        }
                    }
                    if (format == null)
                    {
                        var chk = new Chk();
                        if (chk.IsMine(null, fileName))
                        {
                            format = chk;
                        }
                    }
                    if (format == null)
                    {
                        var ayato = new Ayato();
                        if (ayato.IsMine(null, fileName))
                        {
                            format = ayato;
                        }
                    }
                    if (format == null)
                    {
                        var capMakerPlus = new CapMakerPlus();
                        if (capMakerPlus.IsMine(null, fileName))
                        {
                            format = capMakerPlus;
                        }
                    }
                    if (format == null)
                    {
                        var captionate = new Captionate();
                        if (captionate.IsMine(null, fileName))
                        {
                            format = captionate;
                        }
                    }
                    if (format == null)
                    {
                        var ultech130 = new Ultech130();
                        if (ultech130.IsMine(null, fileName))
                        {
                            format = ultech130;
                        }
                    }
                    if (format == null)
                    {
                        var nciCaption = new NciCaption();
                        if (nciCaption.IsMine(null, fileName))
                        {
                            format = nciCaption;
                        }
                    }
                    if (format == null)
                    {
                        var avidStl = new AvidStl();
                        if (avidStl.IsMine(null, fileName))
                        {
                            format = avidStl;
                        }
                    }
                    if (format == null)
                    {
                        var asc = new TimeLineAscii();
                        if (asc.IsMine(null, fileName))
                        {
                            format = asc;
                        }
                    }
                    if (format == null)
                    {
                        var asc = new TimeLineFootageAscii();
                        if (asc.IsMine(null, fileName))
                        {
                            format = asc;
                        }
                    }
                }

                if (format == null)
                {
                    if (FileUtil.IsBluRaySup(fileName))
                    {
                        item.SubItems.Add("Blu-ray");
                    }
                    else if (FileUtil.IsVobSub(fileName))
                    {
                        item.SubItems.Add("VobSub");
                    }
                    else if (ext.Equals(".mkv", StringComparison.OrdinalIgnoreCase) || ext.Equals(".mks", StringComparison.OrdinalIgnoreCase))
                    {
                        int mkvCount = 0;
                        using (var matroska = new MatroskaFile(fileName))
                        {
                            if (matroska.IsValid)
                            {
                                foreach (var track in matroska.GetTracks(true))
                                {
                                    if (track.CodecId.Equals("S_VOBSUB", StringComparison.OrdinalIgnoreCase))
                                    {
                                        // TODO: Convert from VobSub image based format!
                                    }
                                    else if (track.CodecId.Equals("S_HDMV/PGS", StringComparison.OrdinalIgnoreCase))
                                    {
                                        // TODO: Convert from Blu-ray image based format!
                                    }
                                    else if (track.CodecId.Equals("S_TEXT/UTF8", StringComparison.OrdinalIgnoreCase) || track.CodecId.Equals("S_TEXT/SSA", StringComparison.OrdinalIgnoreCase) || track.CodecId.Equals("S_TEXT/ASS", StringComparison.OrdinalIgnoreCase))
                                    {
                                        mkvCount++;
                                    }
                                }
                            }
                        }
                        if (mkvCount > 0)
                        {
                            item.SubItems.Add("Matroska - " + mkvCount);
                        }
                        else
                        {
                            item.SubItems.Add(Configuration.Settings.Language.UnknownSubtitle.Title);
                        }
                    }
                    else
                    {
                        item.SubItems.Add(Configuration.Settings.Language.UnknownSubtitle.Title);
                    }
                }
                else
                {
                    item.SubItems.Add(format.Name);
                }
                item.SubItems.Add("-");

                listViewInputFiles.Items.Add(item);
            }
            catch
            {
            }
        }