private void btnReadFile_Click(object sender, RoutedEventArgs e) { string ccFilePath = textBoxInputFile.Text.Trim(); if (!File.Exists(ccFilePath)) { string message = "File doesn't exist"; DisplayStatusInfo(message); return; } string fileExtension = System.IO.Path.GetExtension(ccFilePath); List <ClosedCaptionConverter.Library.ClosedCaption> listOfClosedCaptions = null; if (fileExtension.ToLower() == ".vtt") { VTTHandler vttHandler = new VTTHandler(); listOfClosedCaptions = vttHandler.ReadFile(ccFilePath); } else if (fileExtension.ToLower() == ".srt") { SRTHandler srtHandler = new SRTHandler(); listOfClosedCaptions = srtHandler.ReadFile(ccFilePath); } else if (fileExtension.ToLower() == ".ttml" || fileExtension.ToLower() == ".xml") { TTMLHandler ttmlHandler = new TTMLHandler(); listOfClosedCaptions = ttmlHandler.ReadFile(ccFilePath); } else { string message = "Your selected file type doesn't support."; DisplayStatusInfo(message); } if (listOfClosedCaptions != null) { listViewClosedCaptions.ItemsSource = listOfClosedCaptions; string message = $"Transcript loads successfully, total {listOfClosedCaptions.Count} lines."; DisplayStatusInfo(message); } }
public void ReadFileTestMethod2() { string inputFile = @"TestData\SRT_fromVendor.srt"; int expectedClosedCaptionCount = 34; string expectedFirstStartPoint = "00:00:01.107"; string expectedFirstTranscript = "Before we jump into our demonstrations and we start walking through our specific scenarios,"; SRTHandler srtHandler = new SRTHandler(); var CCs = srtHandler.ReadFile(inputFile); Assert.AreEqual(expectedClosedCaptionCount, CCs.Count); if (CCs.Count == 34) { string actualFirstStartPoint = CCs[0].StartPoint; Assert.AreEqual(expectedFirstStartPoint, actualFirstStartPoint); string actualFirstTranscript = CCs[0].Transcript; Assert.AreEqual(expectedFirstTranscript, actualFirstTranscript); } }
public void ReadFileTestMethod1() { string inputFile = @"TestData\SRT_downloadFromedX.srt"; int expectedClosedCaptionCount = 106; string expectedLastStartPoint = "00:05:12.380"; string expectedLastTranscript = "I hope it taught you something that you could use later on"; SRTHandler srtHandler = new SRTHandler(); var CCs = srtHandler.ReadFile(inputFile); Assert.AreEqual(expectedClosedCaptionCount, CCs.Count); if (CCs.Count == 106) { string actualLastStartPoint = CCs[105].StartPoint; Assert.AreEqual(expectedLastStartPoint, actualLastStartPoint); string actualLastTranscript = CCs[105].Transcript; Assert.AreEqual(expectedLastTranscript, actualLastTranscript); } }
private void ConvertFiles(string targetFormat) { string folderPath = textBoxInputFolder.Text.Trim(); if (!Directory.Exists(folderPath)) { string message = "Folder doesn't exist."; DisplayStatusInfo(message); return; } string targetFolder = Directory.CreateDirectory(System.IO.Path.Combine(folderPath, targetFormat)).FullName; List <string> ccFiles = new List <string>(); // get closed caption files in this folder DirectoryInfo dirInfo = new DirectoryInfo(folderPath); foreach (FileInfo file in dirInfo.GetFiles()) { if (file.Extension.ToLower() == ".vtt" || file.Extension.ToLower() == ".srt" || file.Extension.ToLower() == ".ttml" || file.Extension.ToLower() == ".xml") { ccFiles.Add(file.FullName); } } if (ccFiles.Count > 0) { VTTHandler vttHandler = new VTTHandler(); SRTHandler srtHandler = new SRTHandler(); TTMLHandler ttmlHandler = new TTMLHandler(); StringBuilder sbLog = new StringBuilder(); foreach (string file in ccFiles) { string extension = System.IO.Path.GetExtension(file).ToLower(); List <ClosedCaptionConverter.Library.ClosedCaption> Transcripts = null; if (extension == ".vtt" || extension == ".srt") { Transcripts = srtHandler.ReadFile(file); } else if (extension == ".ttml" || extension == ".xml") { Transcripts = ttmlHandler.ReadFile(file); } if (Transcripts != null && Transcripts.Count > 0) { string fileName = System.IO.Path.GetFileNameWithoutExtension(file); if (targetFormat == "vtt") { vttHandler.WriteFile(System.IO.Path.Combine(targetFolder, fileName + ".vtt"), Transcripts); } else if (targetFormat == "srt") { srtHandler.WriteFile(System.IO.Path.Combine(targetFolder, fileName + ".srt"), Transcripts); } else if (targetFormat == "ttml") { ttmlHandler.WriteFile(System.IO.Path.Combine(targetFolder, fileName + ".ttml"), Transcripts); } } else { sbLog.AppendLine($"File failed convert - {file}."); } } if (sbLog.ToString() != string.Empty) { using (StreamWriter writer = new StreamWriter(System.IO.Path.Combine(targetFolder, "log.txt"))) { writer.Write(sbLog.ToString()); writer.Flush(); writer.Close(); } } } DisplayStatusInfo($"Conversion Complete, total converted {ccFiles.Count} files."); }