Batch converter now multi-threaded with progress bar

git-svn-id: https://subtitleedit.googlecode.com/svn/trunk@1645 99eadd0c-20b8-1223-b5c4-2a2b2df33de2
This commit is contained in:
niksedk 2013-02-20 08:34:52 +00:00
parent 96a13e749f
commit c507ef1e8d
3 changed files with 216 additions and 60 deletions

View File

@ -33,12 +33,14 @@
this.buttonCancel = new System.Windows.Forms.Button();
this.groupBoxConvertOptions = new System.Windows.Forms.GroupBox();
this.checkBoxFixCommonErrors = new System.Windows.Forms.CheckBox();
this.buttonFixCommonErrorSettings = new System.Windows.Forms.Button();
this.groupBoxChangeFrameRate = new System.Windows.Forms.GroupBox();
this.comboBoxFrameRateTo = new System.Windows.Forms.ComboBox();
this.labelToFrameRate = new System.Windows.Forms.Label();
this.comboBoxFrameRateFrom = new System.Windows.Forms.ComboBox();
this.labelFromFrameRate = new System.Windows.Forms.Label();
this.groupBoxOffsetTimeCodes = new System.Windows.Forms.GroupBox();
this.timeUpDownAdjust = new Nikse.SubtitleEdit.Controls.TimeUpDown();
this.labelHoursMinSecsMilliSecs = new System.Windows.Forms.Label();
this.checkBoxFixCasing = new System.Windows.Forms.CheckBox();
this.checkBoxRemoveTextForHI = new System.Windows.Forms.CheckBox();
@ -67,8 +69,8 @@
this.removeAllToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.folderBrowserDialog1 = new System.Windows.Forms.FolderBrowserDialog();
this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
this.buttonFixCommonErrorSettings = new System.Windows.Forms.Button();
this.timeUpDownAdjust = new Nikse.SubtitleEdit.Controls.TimeUpDown();
this.progressBar1 = new System.Windows.Forms.ProgressBar();
this.labelStatus = new System.Windows.Forms.Label();
this.groupBoxConvertOptions.SuspendLayout();
this.groupBoxChangeFrameRate.SuspendLayout();
this.groupBoxOffsetTimeCodes.SuspendLayout();
@ -130,6 +132,17 @@
this.checkBoxFixCommonErrors.UseVisualStyleBackColor = true;
this.checkBoxFixCommonErrors.CheckedChanged += new System.EventHandler(this.checkBoxFixCommonErrors_CheckedChanged);
//
// buttonFixCommonErrorSettings
//
this.buttonFixCommonErrorSettings.ImeMode = System.Windows.Forms.ImeMode.NoControl;
this.buttonFixCommonErrorSettings.Location = new System.Drawing.Point(149, 88);
this.buttonFixCommonErrorSettings.Name = "buttonFixCommonErrorSettings";
this.buttonFixCommonErrorSettings.Size = new System.Drawing.Size(87, 21);
this.buttonFixCommonErrorSettings.TabIndex = 4;
this.buttonFixCommonErrorSettings.Text = "Settings...";
this.buttonFixCommonErrorSettings.UseVisualStyleBackColor = true;
this.buttonFixCommonErrorSettings.Click += new System.EventHandler(this.buttonFixCommonErrorSettings_Click);
//
// groupBoxChangeFrameRate
//
this.groupBoxChangeFrameRate.Controls.Add(this.comboBoxFrameRateTo);
@ -188,6 +201,16 @@
this.groupBoxOffsetTimeCodes.TabStop = false;
this.groupBoxOffsetTimeCodes.Text = "Offset time codes";
//
// timeUpDownAdjust
//
this.timeUpDownAdjust.AutoSize = true;
this.timeUpDownAdjust.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
this.timeUpDownAdjust.Location = new System.Drawing.Point(7, 33);
this.timeUpDownAdjust.Margin = new System.Windows.Forms.Padding(4);
this.timeUpDownAdjust.Name = "timeUpDownAdjust";
this.timeUpDownAdjust.Size = new System.Drawing.Size(92, 24);
this.timeUpDownAdjust.TabIndex = 1;
//
// labelHoursMinSecsMilliSecs
//
this.labelHoursMinSecsMilliSecs.AutoSize = true;
@ -429,20 +452,20 @@
this.removeToolStripMenuItem,
this.removeAllToolStripMenuItem});
this.contextMenuStripFiles.Name = "contextMenuStripStyles";
this.contextMenuStripFiles.Size = new System.Drawing.Size(133, 48);
this.contextMenuStripFiles.Size = new System.Drawing.Size(153, 70);
this.contextMenuStripFiles.Opening += new System.ComponentModel.CancelEventHandler(this.ContextMenuStripFilesOpening);
//
// removeToolStripMenuItem
//
this.removeToolStripMenuItem.Name = "removeToolStripMenuItem";
this.removeToolStripMenuItem.Size = new System.Drawing.Size(132, 22);
this.removeToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
this.removeToolStripMenuItem.Text = "Remove";
this.removeToolStripMenuItem.Click += new System.EventHandler(this.RemoveToolStripMenuItemClick);
//
// removeAllToolStripMenuItem
//
this.removeAllToolStripMenuItem.Name = "removeAllToolStripMenuItem";
this.removeAllToolStripMenuItem.Size = new System.Drawing.Size(132, 22);
this.removeAllToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
this.removeAllToolStripMenuItem.Text = "Remove all";
this.removeAllToolStripMenuItem.Click += new System.EventHandler(this.RemoveAllToolStripMenuItemClick);
//
@ -450,26 +473,24 @@
//
this.openFileDialog1.FileName = "openFileDialog1";
//
// buttonFixCommonErrorSettings
// progressBar1
//
this.buttonFixCommonErrorSettings.ImeMode = System.Windows.Forms.ImeMode.NoControl;
this.buttonFixCommonErrorSettings.Location = new System.Drawing.Point(149, 88);
this.buttonFixCommonErrorSettings.Name = "buttonFixCommonErrorSettings";
this.buttonFixCommonErrorSettings.Size = new System.Drawing.Size(87, 21);
this.buttonFixCommonErrorSettings.TabIndex = 4;
this.buttonFixCommonErrorSettings.Text = "Settings...";
this.buttonFixCommonErrorSettings.UseVisualStyleBackColor = true;
this.buttonFixCommonErrorSettings.Click += new System.EventHandler(this.buttonFixCommonErrorSettings_Click);
this.progressBar1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.progressBar1.Location = new System.Drawing.Point(15, 556);
this.progressBar1.Name = "progressBar1";
this.progressBar1.Size = new System.Drawing.Size(833, 10);
this.progressBar1.TabIndex = 8;
//
// timeUpDownAdjust
// labelStatus
//
this.timeUpDownAdjust.AutoSize = true;
this.timeUpDownAdjust.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
this.timeUpDownAdjust.Location = new System.Drawing.Point(7, 33);
this.timeUpDownAdjust.Margin = new System.Windows.Forms.Padding(4);
this.timeUpDownAdjust.Name = "timeUpDownAdjust";
this.timeUpDownAdjust.Size = new System.Drawing.Size(92, 24);
this.timeUpDownAdjust.TabIndex = 1;
this.labelStatus.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.labelStatus.AutoSize = true;
this.labelStatus.Location = new System.Drawing.Point(12, 540);
this.labelStatus.Name = "labelStatus";
this.labelStatus.Size = new System.Drawing.Size(59, 13);
this.labelStatus.TabIndex = 9;
this.labelStatus.Text = "labelStatus";
//
// BatchConvert
//
@ -477,6 +498,8 @@
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(1048, 580);
this.Controls.Add(this.labelStatus);
this.Controls.Add(this.progressBar1);
this.Controls.Add(this.groupBoxOutput);
this.Controls.Add(this.groupBoxInput);
this.Controls.Add(this.buttonConvert);
@ -485,7 +508,6 @@
this.MinimumSize = new System.Drawing.Size(1024, 578);
this.Name = "BatchConvert";
this.ShowIcon = false;
this.ShowInTaskbar = false;
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
this.Text = "Batch convert";
this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.BatchConvert_FormClosing);
@ -500,6 +522,7 @@
this.groupBoxInput.PerformLayout();
this.contextMenuStripFiles.ResumeLayout(false);
this.ResumeLayout(false);
this.PerformLayout();
}
@ -545,6 +568,8 @@
private System.Windows.Forms.ToolStripMenuItem removeAllToolStripMenuItem;
private System.Windows.Forms.CheckBox checkBoxFixCommonErrors;
private System.Windows.Forms.Button buttonFixCommonErrorSettings;
private System.Windows.Forms.ProgressBar progressBar1;
private System.Windows.Forms.Label labelStatus;
}
}

View File

@ -6,22 +6,58 @@ using System.Windows.Forms;
using Nikse.SubtitleEdit.Logic;
using Nikse.SubtitleEdit.Logic.SubtitleFormats;
using System.Globalization;
using System.ComponentModel;
using System.Collections.Generic;
namespace Nikse.SubtitleEdit.Forms
{
public partial class BatchConvert : Form
{
public class ThreadDoWorkParameter
{
public bool FixCommonErrors { get; set; }
public ListViewItem Item { get; set; }
public Subtitle Subtitle { get; set; }
public SubtitleFormat Format { get; set; }
public Encoding Encoding { get; set; }
public string Language { get; set; }
public string Error { get; set; }
public string FileName { get; set; }
public string ToFormat { get; set; }
public SubtitleFormat SourceFormat { get; set; }
public ThreadDoWorkParameter(bool fixCommonErrors, ListViewItem item, Subtitle subtitle, SubtitleFormat format, Encoding encoding, string language, string fileName, string toFormat, SubtitleFormat sourceFormat)
{
FixCommonErrors = fixCommonErrors;
Item = item;
Subtitle = subtitle;
Format = format;
Encoding = encoding;
Language = language;
FileName = fileName;
ToFormat = toFormat;
SourceFormat = sourceFormat;
}
}
string _assStyle;
string _ssaStyle;
FormRemoveTextForHearImpaired _removeForHI = new FormRemoveTextForHearImpaired();
ChangeCasing _changeCasing = new ChangeCasing();
ChangeCasingNames _changeCasingNames = new ChangeCasingNames();
bool _converting = false;
int _count = 0;
int _converted = 0;
int _errors = 0;
IList<SubtitleFormat> _allFormats = SubtitleFormat.AllSubtitleFormats;
bool _abort = false;
public BatchConvert()
{
InitializeComponent();
progressBar1.Visible = false;
labelStatus.Text = string.Empty;
var l = Configuration.Settings.Language.BatchConvert;
Text = l.Title;
groupBoxInput.Text = l.Input;
@ -287,12 +323,23 @@ namespace Nikse.SubtitleEdit.Forms
private void listViewInputFiles_DragEnter(object sender, DragEventArgs e)
{
if (_converting)
{
e.Effect = DragDropEffects.None;
return;
}
if (e.Data.GetDataPresent(DataFormats.FileDrop, false))
e.Effect = DragDropEffects.All;
}
private void listViewInputFiles_DragDrop(object sender, DragEventArgs e)
{
if (_converting)
{
return;
}
string[] fileNames = (string[])e.Data.GetData(DataFormats.FileDrop);
foreach (string fileName in fileNames)
{
@ -354,13 +401,37 @@ namespace Nikse.SubtitleEdit.Forms
}
_converting = true;
buttonConvert.Enabled = false;
buttonCancel.Enabled = false;
progressBar1.Maximum = listViewInputFiles.Items.Count;
progressBar1.Value = 0;
progressBar1.Visible = progressBar1.Maximum > 2;
string toFormat = comboBoxSubtitleFormats.Text;
int count = 0;
int converted = 0;
int errors = 0;
var allFormats = SubtitleFormat.AllSubtitleFormats;
groupBoxOutput.Enabled = false;
groupBoxConvertOptions.Enabled = false;
buttonInputBrowse.Enabled = false;
_count = 0;
_converted = 0;
_errors = 0;
_abort = false;
BackgroundWorker worker1 = new BackgroundWorker();
BackgroundWorker worker2 = new BackgroundWorker();
BackgroundWorker worker3 = new BackgroundWorker();
worker1.DoWork += new DoWorkEventHandler(DoThreadWork);
worker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(ThreadWorkerCompleted);
worker2.DoWork += new DoWorkEventHandler(DoThreadWork);
worker2.RunWorkerCompleted += new RunWorkerCompletedEventHandler(ThreadWorkerCompleted);
worker3.DoWork += new DoWorkEventHandler(DoThreadWork);
worker3.RunWorkerCompleted += new RunWorkerCompletedEventHandler(ThreadWorkerCompleted);
foreach (ListViewItem item in listViewInputFiles.Items)
item.SubItems[3].Text = "-";
int index = 0;
while (index < listViewInputFiles.Items.Count && _abort == false)
{
ListViewItem item = listViewInputFiles.Items[index];
string fileName = item.Text;
string friendlyName = item.SubItems[1].Text;
@ -470,7 +541,9 @@ namespace Nikse.SubtitleEdit.Forms
if (format == null)
{
if (progressBar1.Value < progressBar1.Maximum)
progressBar1.Value++;
labelStatus.Text = progressBar1.Value + " / " + progressBar1.Maximum;
}
else
{
@ -504,19 +577,6 @@ namespace Nikse.SubtitleEdit.Forms
_changeCasingNames.Initialize(sub);
_changeCasingNames.FixCasing();
}
if (checkBoxFixCommonErrors.Checked)
{
try
{
FixCommonErrors fixCommonErrors = new FixCommonErrors();
fixCommonErrors.RunBatch(sub, GetCurrentSubtitleFormat(), GetCurrentEncoding(), Configuration.Settings.Tools.BatchConvertLanguage);
sub = fixCommonErrors.FixedSubtitle;
}
catch (Exception exception)
{
item.SubItems[3].Text = "FCE ERROR: " + exception.Message;
}
}
double fromFrameRate;
double toFrameRate;
@ -531,27 +591,89 @@ namespace Nikse.SubtitleEdit.Forms
sub.AddTimeToAllParagraphs(TimeSpan.FromMilliseconds(timeUpDownAdjust.TimeCode.TotalMilliseconds));
}
int oldConverted = converted;
Main.BatchConvertSave(toFormat, null, GetCurrentEncoding(), textBoxOutputFolder.Text, count, ref converted, ref errors, allFormats, fileName, sub, format, checkBoxOverwrite.Checked);
if (converted == oldConverted + 1)
while (worker1.IsBusy && worker2.IsBusy && worker3.IsBusy)
{
item.SubItems[3].Text = Configuration.Settings.Language.BatchConvert.Converted;
}
else if (converted > oldConverted + 1)
{
item.SubItems[3].Text = string.Format(Configuration.Settings.Language.BatchConvert.ConvertedX, converted - oldConverted);
}
else
{
item.SubItems[3].Text = "ERROR";
Application.DoEvents();
System.Threading.Thread.Sleep(100);
}
ThreadDoWorkParameter parameter = new ThreadDoWorkParameter(checkBoxFixCommonErrors.Checked, item, sub, GetCurrentSubtitleFormat(), GetCurrentEncoding(), Configuration.Settings.Tools.BatchConvertLanguage, fileName, toFormat, format);
if (!worker1.IsBusy)
worker1.RunWorkerAsync(parameter);
else if (!worker2.IsBusy)
worker2.RunWorkerAsync(parameter);
else if (!worker3.IsBusy)
worker3.RunWorkerAsync(parameter);
}
}
catch
{
if (progressBar1.Value < progressBar1.Maximum)
progressBar1.Value++;
labelStatus.Text = progressBar1.Value + " / " + progressBar1.Maximum;
}
index++;
}
while (worker1.IsBusy || worker2.IsBusy || worker3.IsBusy)
{
Application.DoEvents();
System.Threading.Thread.Sleep(100);
}
_converting = false;
labelStatus.Text = string.Empty;
progressBar1.Visible = false;
buttonConvert.Enabled = true;
buttonCancel.Enabled = true;
groupBoxOutput.Enabled = true;
groupBoxConvertOptions.Enabled = true;
buttonInputBrowse.Enabled = true;
checkBoxFixCommonErrors_CheckedChanged(null, null);
}
void DoThreadWork(object sender, DoWorkEventArgs e)
{
ThreadDoWorkParameter p = (ThreadDoWorkParameter)e.Argument;
if (p.FixCommonErrors)
{
try
{
FixCommonErrors fixCommonErrors = new FixCommonErrors();
fixCommonErrors.RunBatch(p.Subtitle, p.Format, p.Encoding, Configuration.Settings.Tools.BatchConvertLanguage);
p.Subtitle = fixCommonErrors.FixedSubtitle;
}
catch (Exception exception)
{
p.Error = "FCE ERROR: " + exception.Message;
}
}
e.Result = p;
}
void ThreadWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
ThreadDoWorkParameter p = (ThreadDoWorkParameter)e.Result;
if (!string.IsNullOrEmpty(p.Error))
{
p.Item.SubItems[3].Text = p.Error;
}
else
{
bool success = Main.BatchConvertSave(p.ToFormat, null, GetCurrentEncoding(), textBoxOutputFolder.Text, _count, ref _converted, ref _errors, _allFormats, p.FileName, p.Subtitle, p.SourceFormat, checkBoxOverwrite.Checked);
if (success)
{
p.Item.SubItems[3].Text = Configuration.Settings.Language.BatchConvert.Converted;
}
else
{
p.Item.SubItems[3].Text = "ERROR";
}
}
if (progressBar1.Value < progressBar1.Maximum)
progressBar1.Value++;
labelStatus.Text = progressBar1.Value + " / " + progressBar1.Maximum;
if (progressBar1.Value == progressBar1.Maximum)
labelStatus.Text = string.Empty;
}
private void ComboBoxSubtitleFormatsSelectedIndexChanged(object sender, EventArgs e)
@ -601,7 +723,7 @@ namespace Nikse.SubtitleEdit.Forms
private void ContextMenuStripFilesOpening(object sender, System.ComponentModel.CancelEventArgs e)
{
if (listViewInputFiles.Items.Count == 0)
if (listViewInputFiles.Items.Count == 0 || _converting)
{
e.Cancel = true;
return;
@ -634,6 +756,8 @@ namespace Nikse.SubtitleEdit.Forms
{
if (e.KeyCode == Keys.Delete)
RemoveSelectedFiles();
if (e.KeyCode == Keys.Escape)
_abort = true;
}
private void buttonFixCommonErrorSettings_Click(object sender, EventArgs e)
@ -646,11 +770,16 @@ namespace Nikse.SubtitleEdit.Forms
private void BatchConvert_FormClosing(object sender, FormClosingEventArgs e)
{
if (_converting)
{
e.Cancel = true;
return;
}
Configuration.Settings.Tools.BatchConvertFixCasing = checkBoxFixCasing.Checked;
Configuration.Settings.Tools.BatchConvertFixCommonErrors = checkBoxFixCommonErrors.Checked;
Configuration.Settings.Tools.BatchConvertRemoveFormatting = checkBoxRemoveFormatting.Checked;
Configuration.Settings.Tools.BatchConvertRemoveTextForHI = checkBoxRemoveTextForHI.Checked;
}
private void checkBoxFixCommonErrors_CheckedChanged(object sender, EventArgs e)

View File

@ -849,7 +849,7 @@ namespace Nikse.SubtitleEdit.Forms
Environment.Exit(1);
}
internal static void BatchConvertSave(string toFormat, string offset, Encoding targetEncoding, string outputFolder, int count, ref int converted, ref int errors, IList<SubtitleFormat> formats, string fileName, Subtitle sub, SubtitleFormat format, bool overwrite)
internal static bool BatchConvertSave(string toFormat, string offset, Encoding targetEncoding, string outputFolder, int count, ref int converted, ref int errors, IList<SubtitleFormat> formats, string fileName, Subtitle sub, SubtitleFormat format, bool overwrite)
{
// adjust offset
if (!string.IsNullOrEmpty(offset) && (offset.StartsWith("/offset:") || offset.StartsWith("offset:")))
@ -976,10 +976,12 @@ namespace Nikse.SubtitleEdit.Forms
{
Console.WriteLine(string.Format("{0}: {1} - target format '{2}' not found!", count, fileName, toFormat));
errors++;
return false;
}
else
{
converted++;
return true;
}
}
@ -9457,11 +9459,9 @@ namespace Nikse.SubtitleEdit.Forms
}
else if (e.Modifiers == (Keys.Control | Keys.Shift | Keys.Alt) && e.KeyCode == Keys.C)
{
var form = new BatchConvert();
form.ShowDialog(this);
toolStripMenuItemBatchConvert_Click(null, null);
}
// TABS - MUST BE LAST
else if (tabControlButtons.SelectedTab == tabPageAdjust)
{
@ -16600,10 +16600,12 @@ namespace Nikse.SubtitleEdit.Forms
private void toolStripMenuItemBatchConvert_Click(object sender, EventArgs e)
{
this.Visible = false;
var form = new BatchConvert();
_formPositionsAndSizes.SetPositionAndSize(form);
form.ShowDialog(this);
_formPositionsAndSizes.SavePositionAndSize(form);
this.Visible = true;
}
private void copyOriginalTextToCurrentToolStripMenuItem_Click(object sender, EventArgs e)