Work on ocr progress in cmd-line + batch-convert-ui

This commit is contained in:
Nikolaj Olsson 2019-10-06 08:43:20 +02:00
parent e8404cdd55
commit 93d666244f
8 changed files with 127 additions and 16 deletions

View File

@ -7,10 +7,11 @@
* Add format AWS transcribe json - thx jaccoud
* Add new subtitle format - thx Zhen
* Add new subtitle format
* Run only "Batch convert" in UI via "/batchconvert" - thx 07416
* Run only "Batch convert" in UI via "/batchconvertui" - thx 07416
* Fix mp4 text time codes parsing with 64-bit mdhd/mvhd box - thx DJC
* Add option to capture top align in OCR - thx teodargent
* Add support for WebVTT X-TIMESTAMP-MAP - thx laurensscheers/Nickola
* Add cmd-line/batch-convert-ui of TS to Bluray sup or BDN/XML
* IMPROVED:
* Update Finish translation - thx Teijo
* Update Farsi translation - thx ghost1372
@ -18,6 +19,8 @@
* Update Brazilian translation - thx Igor
* Update Catalan translation - thx juansa
* Update Korean translation - thx domddol
* Update Portuguese translation - thx moob
* Update Polish translation - thx admas
* Improve FCP/image export frame rate compat - thx Antoine
* Auto-switch to just downloaded dictionary
* Allow letters stuck to numbers for Arabic spellcheck - thx OmrSi
@ -29,6 +32,7 @@
* Improve compatibility for ASS/SSA - thx sneaker_ger
* Improve compatibility with Linux/Mono - thx shag00/JGoutin/stefano
* Add some missing translations - thx Jamakmake
* Add progress when using ocr in "Batch convert" or cmd-line
* FIXED:
* Fix crash + bug in "Fix music notations" - thx paolovador/btsix
* Fix crash when using "Batch convert" txt to img - thx Alan

View File

@ -58,6 +58,7 @@ namespace Nikse.SubtitleEdit.Core.TransportStream
var packetBuffer = new byte[packetLength];
var m2TsTimeCodeBuffer = new byte[4];
long position = 0;
long callBackTicks = 0;
// check for Topfield .rec file
ms.Seek(position, SeekOrigin.Begin);
@ -170,13 +171,15 @@ namespace Nikse.SubtitleEdit.Core.TransportStream
//}
}
if (callback != null)
{
callback.Invoke(ms.Position, transportStreamLength);
}
}
TotalNumberOfPackets++;
position += packetLength;
if (callback != null && DateTime.UtcNow.Ticks - 10000 * 500 > callBackTicks) // call back every half second
{
callback.Invoke(ms.Position, transportStreamLength);
callBackTicks = DateTime.UtcNow.Ticks;
}
}
else
{

View File

@ -747,7 +747,13 @@ namespace Nikse.SubtitleEdit.Forms
item.SubItems[3].Text = Configuration.Settings.Language.BatchConvert.Ocr;
using (var vobSubOcr = new VobSubOcr())
{
vobSubOcr.ProgressCallback = progress =>
{
item.SubItems[3].Text = Configuration.Settings.Language.BatchConvert.Ocr + " " + progress;
listViewInputFiles.Refresh();
};
vobSubOcr.FileName = Path.GetFileName(fileName);
vobSubOcr.InitializeBatch(vobSubs, idx.Palette, Configuration.Settings.VobSubOcr, fileName, false, track.Language);
sub = vobSubOcr.SubtitleFromOcr;
}
@ -766,6 +772,11 @@ namespace Nikse.SubtitleEdit.Forms
item.SubItems[3].Text = Configuration.Settings.Language.BatchConvert.Ocr;
using (var vobSubOcr = new VobSubOcr())
{
vobSubOcr.ProgressCallback = progress =>
{
item.SubItems[3].Text = Configuration.Settings.Language.BatchConvert.Ocr + " " + progress;
listViewInputFiles.Refresh();
};
vobSubOcr.FileName = Path.GetFileName(fileName);
vobSubOcr.InitializeBatch(bluRaySubtitles, Configuration.Settings.VobSubOcr, fileName, false, track.Language);
sub = vobSubOcr.SubtitleFromOcr;
@ -810,6 +821,11 @@ namespace Nikse.SubtitleEdit.Forms
item.SubItems[3].Text = Configuration.Settings.Language.BatchConvert.Ocr;
using (var vobSubOcr = new VobSubOcr())
{
vobSubOcr.ProgressCallback = progress =>
{
item.SubItems[3].Text = Configuration.Settings.Language.BatchConvert.Ocr + " " + progress;
listViewInputFiles.Refresh();
};
vobSubOcr.FileName = Path.GetFileName(fileName);
vobSubOcr.InitializeBatch(bluRaySubtitles, Configuration.Settings.VobSubOcr, fileName, false);
sub = vobSubOcr.SubtitleFromOcr;
@ -821,6 +837,11 @@ namespace Nikse.SubtitleEdit.Forms
item.SubItems[3].Text = Configuration.Settings.Language.BatchConvert.Ocr;
using (var vobSubOcr = new VobSubOcr())
{
vobSubOcr.ProgressCallback = progress =>
{
item.SubItems[3].Text = Configuration.Settings.Language.BatchConvert.Ocr + " " + progress;
listViewInputFiles.Refresh();
};
vobSubOcr.InitializeBatch(fileName, Configuration.Settings.VobSubOcr, false);
sub = vobSubOcr.SubtitleFromOcr;
}
@ -1260,6 +1281,7 @@ namespace Nikse.SubtitleEdit.Forms
private void ThreadWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
CommandLineConverter.BatchConvertProgress progressCallback = null;
var p = (ThreadDoWorkParameter)e.Result;
if (p.Item.Index + 2 < listViewInputFiles.Items.Count)
{
@ -1279,12 +1301,16 @@ namespace Nikse.SubtitleEdit.Forms
if (p.SourceFormat == null)
{
var ext = Path.GetExtension(p.FileName);
if ((ext.Equals(".ts", StringComparison.OrdinalIgnoreCase) || ext.Equals(".m2ts", StringComparison.OrdinalIgnoreCase)) &&
if (ext != null && (ext.Equals(".ts", StringComparison.OrdinalIgnoreCase) || ext.Equals(".m2ts", StringComparison.OrdinalIgnoreCase)) &&
(FileUtil.IsTransportStream(p.FileName) || FileUtil.IsM2TransportStream(p.FileName)))
{
if (p.ToFormat == BluRaySubtitle || p.ToFormat == BdnXmlSubtitle)
{
p.Item.SubItems[3].Text = Configuration.Settings.Language.General.PleaseWait;
progressCallback = progress =>
{
p.Item.SubItems[3].Text = progress;
listViewInputFiles.Refresh();
};
}
else
{
@ -1321,8 +1347,15 @@ namespace Nikse.SubtitleEdit.Forms
dir = Path.GetDirectoryName(p.FileName);
overwrite = true;
}
var success = CommandLineConverter.BatchConvertSave(targetFormat, TimeSpan.Zero, GetCurrentEncoding(), dir, _count, ref _converted, ref _errors, _allFormats, p.FileName, p.Subtitle, p.SourceFormat, binaryParagraphs, overwrite, -1, null, null);
p.Item.SubItems[3].Text = success ? Configuration.Settings.Language.BatchConvert.Converted : Configuration.Settings.Language.BatchConvert.NotConverted;
var success = CommandLineConverter.BatchConvertSave(targetFormat, TimeSpan.Zero, GetCurrentEncoding(), dir, _count, ref _converted, ref _errors, _allFormats, p.FileName, p.Subtitle, p.SourceFormat, binaryParagraphs, overwrite, -1, null, null, CommandLineConverter.BatchAction.None, null, false, progressCallback);
if (success)
{
p.Item.SubItems[3].Text = Configuration.Settings.Language.BatchConvert.Converted;
}
else
{
p.Item.SubItems[3].Text = Configuration.Settings.Language.BatchConvert.NotConverted + " " + p.Item.SubItems[3].Text.Trim('-').Trim();
}
}
catch (Exception exception)
{

View File

@ -359,7 +359,7 @@ namespace Nikse.SubtitleEdit.Forms
fileName = commandLineArgs[1];
if (fileName.Equals("/batchconvert", StringComparison.OrdinalIgnoreCase) || fileName.Equals("-batchconvert", StringComparison.OrdinalIgnoreCase))
if (fileName.Equals("/batchconvertui", StringComparison.OrdinalIgnoreCase) || fileName.Equals("-batchconvertui", StringComparison.OrdinalIgnoreCase))
{
new BatchConvert(this.Icon).ShowDialog();
Environment.Exit(0);

View File

@ -226,6 +226,9 @@ namespace Nikse.SubtitleEdit.Forms.Ocr
public int Language { get; set; }
}
public delegate void ProgressCallbackDelegate(string progress);
public ProgressCallbackDelegate ProgressCallback { get; set; }
private Main _main;
public string FileName { get; set; }
private Subtitle _subtitle = new Subtitle();
@ -843,6 +846,11 @@ namespace Nikse.SubtitleEdit.Forms.Ocr
SetButtonsEnabledAfterOcrDone();
return;
}
if (ProgressCallback != null)
{
var percent = (int)Math.Round((i + 1) * 100.0 / max);
ProgressCallback?.Invoke($"{percent}%");
}
subtitleListView1.SelectIndexAndEnsureVisible(i);
string text = OcrViaTesseract(GetSubtitleBitmap(i), i);
@ -5288,6 +5296,11 @@ namespace Nikse.SubtitleEdit.Forms.Ocr
GetSubtitleTime(index, out var startTime, out var endTime);
labelStatus.Text = $"{index + 1} / {max}: {startTime} - {endTime}";
progressBar1.Value = index + 1;
if (ProgressCallback != null)
{
var percent = (int)Math.Round((index + 1) * 100.0 / max);
ProgressCallback?.Invoke($"{percent}%");
}
labelStatus.Refresh();
progressBar1.Refresh();
@ -5318,6 +5331,11 @@ namespace Nikse.SubtitleEdit.Forms.Ocr
GetSubtitleTime(i, out startTime, out endTime);
labelStatus.Text = $"{i + 1} / {max}: {startTime} - {endTime}";
progressBar1.Value = i + 1;
if (ProgressCallback != null)
{
var percent = (int)Math.Round((i + 1) * 100.0 / max);
ProgressCallback?.Invoke($"{percent}%");
}
labelStatus.Refresh();
progressBar1.Refresh();
if (_abort)

View File

@ -24,6 +24,8 @@ namespace Nikse.SubtitleEdit.Logic.CommandLineConvert
private static string _currentFolder;
private static bool _consoleAttached;
public delegate void BatchConvertProgress(string progress);
[Flags]
internal enum BatchAction
{
@ -423,8 +425,13 @@ namespace Nikse.SubtitleEdit.Logic.CommandLineConvert
_stdOutWriter.WriteLine("Using OCR to extract subtitles");
using (var vobSubOcr = new VobSubOcr())
{
vobSubOcr.ProgressCallback = progress =>
{
_stdOutWriter?.Write($"\r{Configuration.Settings.Language.BatchConvert.Ocr} : {progress}");
};
vobSubOcr.FileName = Path.GetFileName(fileName);
vobSubOcr.InitializeBatch(vobSubs, idx.Palette, Configuration.Settings.VobSubOcr, fileName, false, lang);
_stdOutWriter?.WriteLine();
sub = vobSubOcr.SubtitleFromOcr;
}
}
@ -445,8 +452,13 @@ namespace Nikse.SubtitleEdit.Logic.CommandLineConvert
_stdOutWriter.WriteLine("Using OCR to extract subtitles");
using (var vobSubOcr = new VobSubOcr())
{
vobSubOcr.ProgressCallback = progress =>
{
_stdOutWriter?.Write($"\r{Configuration.Settings.Language.BatchConvert.Ocr} : {progress}");
};
vobSubOcr.FileName = Path.GetFileName(fileName);
vobSubOcr.InitializeBatch(bluRaySubtitles, Configuration.Settings.VobSubOcr, fileName, false, lang);
_stdOutWriter?.WriteLine();
sub = vobSubOcr.SubtitleFromOcr;
}
}
@ -544,8 +556,13 @@ namespace Nikse.SubtitleEdit.Logic.CommandLineConvert
_stdOutWriter.WriteLine("Using OCR to extract subtitles");
using (var vobSubOcr = new VobSubOcr())
{
vobSubOcr.ProgressCallback = progress =>
{
_stdOutWriter?.Write($"\r{Configuration.Settings.Language.BatchConvert.Ocr} : {progress}");
};
vobSubOcr.FileName = Path.GetFileName(fileName);
vobSubOcr.InitializeBatch(subPicturesWithTimeCodes, fileName);
_stdOutWriter?.WriteLine();
sub = vobSubOcr.SubtitleFromOcr;
}
@ -703,8 +720,13 @@ namespace Nikse.SubtitleEdit.Logic.CommandLineConvert
using (var vobSubOcr = new VobSubOcr())
{
_stdOutWriter?.WriteLine("Using OCR to extract subtitles");
vobSubOcr.ProgressCallback = progress =>
{
_stdOutWriter?.Write($"\r{Configuration.Settings.Language.BatchConvert.Ocr} : {progress}");
};
vobSubOcr.FileName = Path.GetFileName(fileName);
vobSubOcr.InitializeBatch(bluRaySubtitles, Configuration.Settings.VobSubOcr, fileName, forcedOnly);
_stdOutWriter?.WriteLine();
sub = vobSubOcr.SubtitleFromOcr;
_stdOutWriter?.WriteLine($"Extracted subtitles from file \"{fileName}\"");
}
@ -725,7 +747,12 @@ namespace Nikse.SubtitleEdit.Logic.CommandLineConvert
using (var vobSubOcr = new VobSubOcr())
{
_stdOutWriter?.WriteLine("Using OCR to extract subtitles");
vobSubOcr.ProgressCallback = progress =>
{
_stdOutWriter?.Write($"\r{Configuration.Settings.Language.BatchConvert.Ocr} : {progress}");
};
vobSubOcr.InitializeBatch(fileName, Configuration.Settings.VobSubOcr, forcedOnly);
_stdOutWriter?.WriteLine();
sub = vobSubOcr.SubtitleFromOcr;
_stdOutWriter?.WriteLine($"Extracted subtitles from file \"{fileName}\"");
}
@ -746,7 +773,12 @@ namespace Nikse.SubtitleEdit.Logic.CommandLineConvert
using (var vobSubOcr = new VobSubOcr())
{
_stdOutWriter?.WriteLine("Using OCR to extract subtitles");
vobSubOcr.ProgressCallback = progress =>
{
_stdOutWriter?.Write($"\r{Configuration.Settings.Language.BatchConvert.Ocr} : {progress}");
};
vobSubOcr.InitializeBatch(subtitle, Configuration.Settings.VobSubOcr, GetTargetformat(targetFormat, formats).Name == new Son().Name);
_stdOutWriter?.WriteLine();
sub = vobSubOcr.SubtitleFromOcr;
_stdOutWriter?.WriteLine($"Extracted subtitles from file \"{fileName}\"");
}
@ -977,11 +1009,13 @@ namespace Nikse.SubtitleEdit.Logic.CommandLineConvert
internal static bool BatchConvertSave(string targetFormat, TimeSpan offset, Encoding targetEncoding, string outputFolder, int count, ref int converted, ref int errors,
IEnumerable<SubtitleFormat> formats, string fileName, Subtitle sub, SubtitleFormat format, List<IBinaryParagraph> binaryParagraphs, bool overwrite, int pacCodePage,
double? targetFrameRate, ICollection<string> multipleReplaceImportFiles, BatchAction actions = BatchAction.None,
Point? resolution = null, bool autoDetectLanguage = false)
Point? resolution = null, bool autoDetectLanguage = false, BatchConvertProgress progressCallback = null)
{
double oldFrameRate = Configuration.Settings.General.CurrentFrameRate;
try
{
var success = true;
// adjust offset
if (offset.Ticks != 0)
{
@ -1273,7 +1307,7 @@ namespace Nikse.SubtitleEdit.Logic.CommandLineConvert
if ((ext.Equals(".ts", StringComparison.OrdinalIgnoreCase) || ext.Equals(".m2ts", StringComparison.OrdinalIgnoreCase)) &&
(FileUtil.IsTransportStream(fileName) || FileUtil.IsM2TransportStream(fileName)))
{
TsToBluRaySup.ConvertFromTsToBluRaySup(fileName, outputFolder, overwrite, _stdOutWriter);
success = TsToBluRaySup.ConvertFromTsToBluRaySup(fileName, outputFolder, overwrite, _stdOutWriter, progressCallback);
}
else
{
@ -1487,7 +1521,7 @@ namespace Nikse.SubtitleEdit.Logic.CommandLineConvert
if ((ext.Equals(".ts", StringComparison.OrdinalIgnoreCase) || ext.Equals(".m2ts", StringComparison.OrdinalIgnoreCase)) &&
(FileUtil.IsTransportStream(fileName) || FileUtil.IsM2TransportStream(fileName)))
{
TsToBdnXml.ConvertFromTsToBdnXml(fileName, outputFolder, overwrite, _stdOutWriter);
success = TsToBdnXml.ConvertFromTsToBdnXml(fileName, outputFolder, overwrite, _stdOutWriter, progressCallback);
}
else
{
@ -1648,7 +1682,7 @@ namespace Nikse.SubtitleEdit.Logic.CommandLineConvert
return false;
}
converted++;
return true;
return success;
}
finally
{

View File

@ -11,13 +11,14 @@ namespace Nikse.SubtitleEdit.Logic.CommandLineConvert
{
public static class TsToBdnXml
{
public static void ConvertFromTsToBdnXml(string fileName, string outputFolder, bool overwrite, StreamWriter stdOutWriter)
public static bool ConvertFromTsToBdnXml(string fileName, string outputFolder, bool overwrite, StreamWriter stdOutWriter, CommandLineConverter.BatchConvertProgress progressCallback)
{
var tsParser = new TransportStreamParser();
tsParser.Parse(fileName, (position, total) =>
{
var percent = (int)Math.Round(position * 100.0 / total);
stdOutWriter?.Write("\rParsing transport stream: {0}%", percent);
progressCallback?.Invoke($"{percent}%");
});
stdOutWriter?.Write("\r".PadRight(32, ' '));
stdOutWriter?.Write("\r");
@ -39,10 +40,17 @@ namespace Nikse.SubtitleEdit.Logic.CommandLineConvert
}
using (var form = new ExportPngXml())
{
if (tsParser.SubtitlePacketIds.Count == 0)
{
stdOutWriter?.WriteLine($"No subtitles found");
progressCallback?.Invoke($"No subtitles found");
return false;
}
foreach (int pid in tsParser.SubtitlePacketIds)
{
var outputFileName = CommandLineConverter.FormatOutputFileNameForBatchConvert(Utilities.GetPathAndFileNameWithoutExtension(fileName) + "-" + pid + Path.GetExtension(fileName), ".xml", outputFolder, overwrite);
stdOutWriter?.WriteLine($"Saving PID {pid} to {outputFileName}...");
progressCallback?.Invoke($"Save PID {pid}");
var sub = tsParser.GetDvbSubtitles(pid);
var subtitle = new Subtitle();
foreach (var p in sub)
@ -92,6 +100,7 @@ namespace Nikse.SubtitleEdit.Logic.CommandLineConvert
form.WriteBdnXmlFile(imagesSavedCount, sb, outputFileName);
}
}
return true;
}
}
}

View File

@ -10,13 +10,14 @@ namespace Nikse.SubtitleEdit.Logic.CommandLineConvert
{
public static class TsToBluRaySup
{
public static void ConvertFromTsToBluRaySup(string fileName, string outputFolder, bool overwrite, StreamWriter stdOutWriter)
public static bool ConvertFromTsToBluRaySup(string fileName, string outputFolder, bool overwrite, StreamWriter stdOutWriter, CommandLineConverter.BatchConvertProgress progressCallback)
{
var tsParser = new TransportStreamParser();
tsParser.Parse(fileName, (position, total) =>
{
var percent = (int)Math.Round(position * 100.0 / total);
stdOutWriter?.Write("\rParsing transport stream: {0}%", percent);
progressCallback?.Invoke($"{percent}%");
});
stdOutWriter?.Write("\r".PadRight(32, ' '));
stdOutWriter?.Write("\r");
@ -38,11 +39,19 @@ namespace Nikse.SubtitleEdit.Logic.CommandLineConvert
}
using (var form = new ExportPngXml())
{
if (tsParser.SubtitlePacketIds.Count == 0)
{
stdOutWriter?.WriteLine($"No subtitles found");
progressCallback?.Invoke($"No subtitles found");
return false;
}
form.Initialize(new Subtitle(), new SubRip(), BatchConvert.BluRaySubtitle, fileName, videoInfo, fileName);
foreach (int pid in tsParser.SubtitlePacketIds)
{
var outputFileName = CommandLineConverter.FormatOutputFileNameForBatchConvert(Utilities.GetPathAndFileNameWithoutExtension(fileName) + "-" + pid + Path.GetExtension(fileName), ".sup", outputFolder, overwrite);
stdOutWriter?.WriteLine($"Saving PID {pid} to {outputFileName}...");
progressCallback?.Invoke($"Save PID {pid}");
var sub = tsParser.GetDvbSubtitles(pid);
using (var binarySubtitleFile = new FileStream(outputFileName, FileMode.Create))
{
@ -84,6 +93,7 @@ namespace Nikse.SubtitleEdit.Logic.CommandLineConvert
}
}
}
return true;
}
}
}