Beispiel #1
0
        public static void JoinTwoFiles(string file1, string file2, TimeSpan file2StartTsSelectedByUser)
        {
            LogWindow.AddMessage(LogType.Info, "JoinTwoFiles Start");
            var file1Items = new ParseSrt().ToObservableCollectionFromFile(file1);
            var file2Items = new ParseSrt().ToObservableCollectionFromFile(file2);

            var file1LastItem  = file1Items.OrderByDescending(o => o.Number).FirstOrDefault();
            var file2FirstItem = file2Items.OrderBy(o => o.Number).FirstOrDefault();
            var delta          = file2StartTsSelectedByUser - file2FirstItem.StartTs;

            for (int i = 0; i < file2Items.Count; i++)
            {
                //Correct file2's numbers
                file2Items[i].Number = i + 1 + file1LastItem.Number;
                //shift file2's items
                file2Items[i].StartTs += delta;
                file2Items[i].EndTs   += delta;
                //write it back
                file2Items[i].Time =
                    string.Format("{0} --> {1}", ParseSrt.TimeSpanToString(file2Items[i].StartTs), ParseSrt.TimeSpanToString(file2Items[i].EndTs));
            }

            var newShiftedFile2 = ParseSrt.SubitemsToString(file2Items);

            //Create a single file
            var fileName     = string.Format("{0}\\JoinedFile{1}", Path.GetDirectoryName(file1), Path.GetExtension(file1));
            var file1Content = File.ReadAllText(file1);

            File.WriteAllText(fileName, file1Content.ApplyCorrectYeKe(), Encoding.UTF8);
            File.AppendAllText(fileName, newShiftedFile2.ApplyCorrectYeKe(), Encoding.UTF8);
            LogWindow.AddMessage(LogType.Announcement, string.Format("Saved to:  {0}", fileName));
        }
Beispiel #2
0
        // Public Methods (1) 

        public static void DeleteWholeRow(SubtitleItems data, SubtitleItem toDelete, string fileNameToSave)
        {
            if (data == null || toDelete == null || string.IsNullOrWhiteSpace(fileNameToSave))
            {
                LogWindow.AddMessage(LogType.Alert, "Please select a line to delete it.");
                return;
            }

            var number = toDelete.Number;

            if (data.Remove(toDelete))
            {
                //fix numbers
                for (var i = 0; i < data.Count; i++)
                {
                    data[i].Number = i + 1;
                }

                //backup original file
                var backupFile = string.Format("{0}.original.bak", fileNameToSave);
                File.Copy(fileNameToSave, backupFile, overwrite: true);

                //write data
                File.WriteAllText(fileNameToSave, ParseSrt.SubitemsToString(data).ApplyCorrectYeKe(), Encoding.UTF8);

                LogWindow.AddMessage(LogType.Info, string.Format("Line {0} has been deleted.", number));
                LogWindow.AddMessage(LogType.Info, string.Format("Backup file: {0}", backupFile));
            }
            else
            {
                LogWindow.AddMessage(LogType.Alert, string.Format("Couldn't delete line {0}", number));
            }
        }
Beispiel #3
0
        public static string ShiftThisTime(string line, TimeSpan delta, bool add)
        {
            var lineTs = ParseSrt.ConvertStringToTimeSpan(line);

            if (add)
            {
                lineTs += delta;
            }
            else
            {
                lineTs -= delta;
            }

            return(ParseSrt.TimeSpanToString(lineTs));
        }
Beispiel #4
0
        public void TranslateAll(string fileName, string sourceLanguage, string targetLanguage)
        {
            if (_isRunning)
            {
                LogWindow.AddMessage(LogType.Alert, "Translation is in progress...");
                return;
            }
            _isRunning      = true;
            StopTranslation = false;

            try
            {
                if (string.IsNullOrWhiteSpace(fileName))
                {
                    LogWindow.AddMessage(LogType.Alert, "Please open a file.");
                    return;
                }

                _data = new ParseSrt().ToObservableCollectionFromFile(fileName);
                if (_data == null || !_data.Any())
                {
                    LogWindow.AddMessage(LogType.Alert, "Please open a file.");
                    return;
                }

                _sourceLanguage = sourceLanguage;
                _targetLanguage = targetLanguage;

                for (var itemId = 0; itemId < _data.Count; itemId++)
                {
                    if (StopTranslation)
                    {
                        break;
                    }
                    translateItem(itemId);
                }

                var saveToFileName = getSaveToFileName(fileName);
                File.WriteAllText(saveToFileName, ParseSrt.SubitemsToString(_data).ApplyCorrectYeKe(), Encoding.UTF8);
                LogWindow.AddMessage(LogType.Info, "Finished Translating. Saved to " + saveToFileName);
            }
            finally
            {
                _isRunning = false;
            }
        }
Beispiel #5
0
        // Public Methods (4)

        public static void RecalculateRowNumbers(SubtitleItems data, string fileNameToSave)
        {
            if (data == null || string.IsNullOrWhiteSpace(fileNameToSave))
            {
                LogWindow.AddMessage(LogType.Alert, "Please open a file.");
                return;
            }

            //fix numbers
            for (var i = 0; i < data.Count; i++)
            {
                data[i].Number = i + 1;
            }

            //write data
            File.WriteAllText(fileNameToSave, ParseSrt.SubitemsToString(data).ApplyCorrectYeKe(), Encoding.UTF8);
            LogWindow.AddMessage(LogType.Info, "Finished RecalculateRowNumbers.");
        }
Beispiel #6
0
        public static SubtitleItem SubtitleFileLastItem(string filePath)
        {
            var content            = File.ReadAllText(filePath);
            var firstSubtitleItems = new ParseSrt().ToObservableCollectionFromContent(content);

            var lastItem = firstSubtitleItems.OrderByDescending(o => o.Number).FirstOrDefault();

            if (lastItem == null)
            {
                throw new InvalidDataException("Please select a valid subtitle file.");
            }

            return(new SubtitleItem
            {
                Dialog = lastItem.Dialog,
                Number = lastItem.Number,
                Time = lastItem.Time,
                EndTs = lastItem.EndTs,
                StartTs = lastItem.StartTs
            });
        }
Beispiel #7
0
        public static void WriteMergedList(string mainFilePath, string fromFilepath)
        {
            LogWindow.AddMessage(LogType.Info, "WriteMergedList Start.");
            //backup original file
            var backupFile = string.Format("{0}.original.bak", mainFilePath);

            File.Copy(mainFilePath, backupFile, overwrite: true);
            LogWindow.AddMessage(LogType.Info, string.Format("Backup file: {0}", backupFile));

            //read files
            var mainItems = new ParseSrt().ToObservableCollectionFromFile(mainFilePath);
            var fromItems = new ParseSrt().ToObservableCollectionFromFile(fromFilepath);

            //merge
            MergeLists(mainItems, fromItems);
            //toString
            var content = ParseSrt.SubitemsToString(mainItems);

            //write to file
            File.WriteAllText(mainFilePath, content.ApplyCorrectYeKe(), Encoding.UTF8);
            LogWindow.AddMessage(LogType.Info, "WriteMergedList End.");
        }
Beispiel #8
0
        void speechRecognized(object sender, SpeechRecognizedEventArgs e)
        {
            if (e.Result == null || string.IsNullOrWhiteSpace(e.Result.Text))
            {
                return;
            }
            if (SpeechRecognized == null)
            {
                return;
            }
            SpeechRecognized(new SubtitleItem
            {
                Dialog  = ParseSrt.SetMaxWordsPerLine(e.Result.Text),
                Number  = _number++,
                StartTs = e.Result.Audio.AudioPosition,
                EndTs   = e.Result.Audio.AudioPosition + e.Result.Audio.Duration
            });

            _confidenceSum += e.Result.Confidence;
            updateAverageConfidence();
            updatePosition();
        }