-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
563 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<configuration> | ||
<startup> | ||
|
||
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup> | ||
</configuration> |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using System.Windows.Forms; | ||
using System.IO; | ||
|
||
namespace vtt_to_srt | ||
{ | ||
public partial class MainForm : Form | ||
{ | ||
|
||
public MainForm() | ||
{ | ||
InitializeComponent(); | ||
} | ||
|
||
private void AddButton_Click(object sender, EventArgs e) | ||
{ | ||
OpenFileDialog openFileDialog = new OpenFileDialog | ||
{ | ||
Filter = "WebVTT files (*.vtt)|*.vtt", | ||
RestoreDirectory = true, | ||
Multiselect = true | ||
}; | ||
|
||
if (openFileDialog.ShowDialog() == DialogResult.OK) | ||
{ | ||
foreach(string str in openFileDialog.FileNames) | ||
if(!filesListBox.Items.Contains(str)) | ||
filesListBox.Items.Add(str); | ||
} | ||
} | ||
|
||
private void DeleteSelectedButton_Click(object sender, EventArgs e) | ||
{ | ||
List<int> list = new List<int>(); | ||
foreach (int index in filesListBox.SelectedIndices) | ||
list.Add(index); | ||
list.Reverse(); | ||
foreach (int index in list) | ||
filesListBox.Items.RemoveAt(index); | ||
} | ||
|
||
private void DeleteAllButton_Click(object sender, EventArgs e) | ||
{ | ||
filesListBox.Items.Clear(); | ||
} | ||
|
||
private void BrowseButton_Click(object sender, EventArgs e) | ||
{ | ||
using (FolderBrowserDialog fbd = new FolderBrowserDialog()) | ||
{ | ||
if (fbd.ShowDialog() == DialogResult.OK && !String.IsNullOrWhiteSpace(fbd.SelectedPath)) | ||
{ | ||
outputFolderTextBox.Text = fbd.SelectedPath; | ||
} | ||
} | ||
} | ||
|
||
private void ConvertButton_Click(object sender, EventArgs e) | ||
{ | ||
if (Directory.Exists(outputFolderTextBox.Text)) | ||
{ | ||
SwitchUIControlsEnabled(); | ||
foreach (string str in filesListBox.Items) | ||
ConvertToSrt(str); | ||
MessageBox.Show("All files converted successfully.", "Done", MessageBoxButtons.OK, MessageBoxIcon.Information); | ||
SwitchUIControlsEnabled(); | ||
} | ||
else | ||
MessageBox.Show("Output folder doesn't exist.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); | ||
|
||
} | ||
|
||
void SwitchUIControlsEnabled() | ||
{ | ||
outputFolderTextBox.Enabled = !outputFolderTextBox.Enabled; | ||
filesListBox.Enabled = !filesListBox.Enabled; | ||
addButton.Enabled = !addButton.Enabled; | ||
deleteSelectedButton.Enabled = !deleteSelectedButton.Enabled; | ||
deleteAllButton.Enabled = !deleteAllButton.Enabled; | ||
browseButton.Enabled = !browseButton.Enabled; | ||
convertButton.Enabled = !convertButton.Enabled; | ||
} | ||
|
||
void ConvertToSrt(string filePath) | ||
{ | ||
try | ||
{ | ||
using (StreamReader stream = new StreamReader(filePath)) | ||
{ | ||
StringBuilder output = new StringBuilder(); | ||
int lineNumber = 1; | ||
while (!stream.EndOfStream) | ||
{ | ||
string line = stream.ReadLine(); | ||
if (IsTimecode(line)) | ||
{ | ||
output.AppendLine(lineNumber.ToString()); | ||
lineNumber++; | ||
line = line.Replace('.', ','); | ||
line = DeleteCueSettings(line); | ||
output.AppendLine(line); | ||
bool foundCaption = false; | ||
while(true) | ||
{ | ||
if (stream.EndOfStream) | ||
{ | ||
if (foundCaption) | ||
break; | ||
else | ||
throw new Exception("Corrupted file: Found timecode without caption"); | ||
} | ||
line = stream.ReadLine(); | ||
if (String.IsNullOrEmpty(line) || String.IsNullOrWhiteSpace(line)) | ||
{ | ||
output.AppendLine(); | ||
break; | ||
} | ||
foundCaption = true; | ||
output.AppendLine(line); | ||
} | ||
} | ||
} | ||
string fileName = Path.GetFileNameWithoutExtension(filePath) + ".srt"; | ||
using (StreamWriter outputFile = new StreamWriter(outputFolderTextBox.Text + '\\' + fileName)) | ||
outputFile.Write(output); | ||
} | ||
} | ||
catch (Exception e) | ||
{ | ||
MessageBox.Show(this, e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); | ||
} | ||
} | ||
|
||
bool IsTimecode(string line) | ||
{ | ||
return line.Contains("-->"); | ||
} | ||
|
||
string DeleteCueSettings(string line) | ||
{ | ||
StringBuilder output = new StringBuilder(); | ||
foreach (char ch in line) | ||
{ | ||
char chLower = Char.ToLower(ch); | ||
if (chLower >= 'a' && chLower <= 'z') | ||
{ | ||
break; | ||
} | ||
output.Append(ch); | ||
} | ||
return output.ToString(); | ||
} | ||
|
||
} | ||
|
||
} |
Oops, something went wrong.