diff --git a/App.config b/App.config new file mode 100644 index 0000000..9cdea26 --- /dev/null +++ b/App.config @@ -0,0 +1,6 @@ + + + + + + diff --git a/MainForm.Designer.cs b/MainForm.Designer.cs new file mode 100644 index 0000000..0f2a7aa --- /dev/null +++ b/MainForm.Designer.cs @@ -0,0 +1,165 @@ +namespace vtt_to_srt +{ + partial class MainForm + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + this.filesListBox = new System.Windows.Forms.ListBox(); + this.addButton = new System.Windows.Forms.Button(); + this.deleteSelectedButton = new System.Windows.Forms.Button(); + this.deleteAllButton = new System.Windows.Forms.Button(); + this.browseButton = new System.Windows.Forms.Button(); + this.convertButton = new System.Windows.Forms.Button(); + this.outputFolderLabel = new System.Windows.Forms.Label(); + this.outputFolderTextBox = new System.Windows.Forms.TextBox(); + this.websiteLabel = new System.Windows.Forms.Label(); + this.SuspendLayout(); + // + // filesListBox + // + this.filesListBox.FormattingEnabled = true; + this.filesListBox.HorizontalScrollbar = true; + this.filesListBox.Location = new System.Drawing.Point(12, 12); + this.filesListBox.Name = "filesListBox"; + this.filesListBox.SelectionMode = System.Windows.Forms.SelectionMode.MultiExtended; + this.filesListBox.Size = new System.Drawing.Size(237, 212); + this.filesListBox.TabIndex = 0; + // + // addButton + // + this.addButton.Location = new System.Drawing.Point(255, 12); + this.addButton.Name = "addButton"; + this.addButton.Size = new System.Drawing.Size(75, 23); + this.addButton.TabIndex = 1; + this.addButton.Text = "Add..."; + this.addButton.UseVisualStyleBackColor = true; + this.addButton.Click += new System.EventHandler(this.AddButton_Click); + // + // deleteSelectedButton + // + this.deleteSelectedButton.Location = new System.Drawing.Point(255, 41); + this.deleteSelectedButton.Name = "deleteSelectedButton"; + this.deleteSelectedButton.Size = new System.Drawing.Size(75, 41); + this.deleteSelectedButton.TabIndex = 2; + this.deleteSelectedButton.Text = "Delete Selected"; + this.deleteSelectedButton.UseVisualStyleBackColor = true; + this.deleteSelectedButton.Click += new System.EventHandler(this.DeleteSelectedButton_Click); + // + // deleteAllButton + // + this.deleteAllButton.Location = new System.Drawing.Point(255, 88); + this.deleteAllButton.Name = "deleteAllButton"; + this.deleteAllButton.Size = new System.Drawing.Size(75, 23); + this.deleteAllButton.TabIndex = 3; + this.deleteAllButton.Text = "Clear"; + this.deleteAllButton.UseVisualStyleBackColor = true; + this.deleteAllButton.Click += new System.EventHandler(this.DeleteAllButton_Click); + // + // browseButton + // + this.browseButton.Location = new System.Drawing.Point(255, 229); + this.browseButton.Name = "browseButton"; + this.browseButton.Size = new System.Drawing.Size(75, 23); + this.browseButton.TabIndex = 4; + this.browseButton.Text = "Browse..."; + this.browseButton.UseVisualStyleBackColor = true; + this.browseButton.Click += new System.EventHandler(this.BrowseButton_Click); + // + // convertButton + // + this.convertButton.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + this.convertButton.Location = new System.Drawing.Point(255, 258); + this.convertButton.Name = "convertButton"; + this.convertButton.Size = new System.Drawing.Size(75, 23); + this.convertButton.TabIndex = 5; + this.convertButton.Text = "Convert"; + this.convertButton.UseVisualStyleBackColor = true; + this.convertButton.Click += new System.EventHandler(this.ConvertButton_Click); + // + // outputFolderLabel + // + this.outputFolderLabel.AutoSize = true; + this.outputFolderLabel.Location = new System.Drawing.Point(12, 235); + this.outputFolderLabel.Name = "outputFolderLabel"; + this.outputFolderLabel.Size = new System.Drawing.Size(76, 13); + this.outputFolderLabel.TabIndex = 6; + this.outputFolderLabel.Text = "Output folder:"; + // + // outputFolderTextBox + // + this.outputFolderTextBox.Location = new System.Drawing.Point(94, 230); + this.outputFolderTextBox.Name = "outputFolderTextBox"; + this.outputFolderTextBox.Size = new System.Drawing.Size(155, 20); + this.outputFolderTextBox.TabIndex = 7; + // + // websiteLabel + // + this.websiteLabel.AutoSize = true; + this.websiteLabel.Location = new System.Drawing.Point(2, 285); + this.websiteLabel.Name = "websiteLabel"; + this.websiteLabel.Size = new System.Drawing.Size(91, 13); + this.websiteLabel.TabIndex = 8; + this.websiteLabel.Text = "AhmedOsama.me"; + // + // MainForm + // + this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(339, 301); + this.Controls.Add(this.websiteLabel); + this.Controls.Add(this.outputFolderTextBox); + this.Controls.Add(this.outputFolderLabel); + this.Controls.Add(this.convertButton); + this.Controls.Add(this.browseButton); + this.Controls.Add(this.deleteAllButton); + this.Controls.Add(this.deleteSelectedButton); + this.Controls.Add(this.addButton); + this.Controls.Add(this.filesListBox); + this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle; + this.MaximizeBox = false; + this.Name = "MainForm"; + this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; + this.Text = "WebVTT to SubRip Converter"; + this.ResumeLayout(false); + this.PerformLayout(); + + } + + #endregion + + private System.Windows.Forms.ListBox filesListBox; + private System.Windows.Forms.Button addButton; + private System.Windows.Forms.Button deleteSelectedButton; + private System.Windows.Forms.Button deleteAllButton; + private System.Windows.Forms.Button browseButton; + private System.Windows.Forms.Button convertButton; + private System.Windows.Forms.Label outputFolderLabel; + private System.Windows.Forms.TextBox outputFolderTextBox; + private System.Windows.Forms.Label websiteLabel; + } +} + diff --git a/MainForm.cs b/MainForm.cs new file mode 100644 index 0000000..48e4dec --- /dev/null +++ b/MainForm.cs @@ -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 list = new List(); + 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(); + } + + } + +} diff --git a/MainForm.resx b/MainForm.resx new file mode 100644 index 0000000..29dcb1b --- /dev/null +++ b/MainForm.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/Program.cs b/Program.cs new file mode 100644 index 0000000..dd20bbd --- /dev/null +++ b/Program.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace vtt_to_srt +{ + static class Program + { + /// + /// The main entry point for the application. + /// + [STAThread] + static void Main() + { + Application.EnableVisualStyles(); + Application.SetCompatibleTextRenderingDefault(false); + Application.Run(new MainForm()); + } + } +} diff --git a/WebVTT to SubRip Converter.csproj b/WebVTT to SubRip Converter.csproj new file mode 100644 index 0000000..549fec7 --- /dev/null +++ b/WebVTT to SubRip Converter.csproj @@ -0,0 +1,92 @@ + + + + + Debug + AnyCPU + {9DC26F66-A010-412B-B426-C9785026452A} + WinExe + Properties + vtt_to_srt + WebVTT to SubRip Converter + v4.0 + 512 + true + + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + + Form + + + MainForm.cs + + + + + MainForm.cs + + + ResXFileCodeGenerator + Resources.Designer.cs + Designer + + + True + Resources.resx + True + + + SettingsSingleFileGenerator + Settings.Designer.cs + + + True + Settings.settings + True + + + + + + + + \ No newline at end of file