mirror of
https://github.com/SubtitleEdit/subtitleedit.git
synced 2024-10-27 14:32:35 +01:00
Finish transport stream support in cmd line/batch-convert
This commit is contained in:
parent
9d45720a31
commit
07330c878f
@ -18,6 +18,7 @@ using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Windows.Forms;
|
||||
using Nikse.SubtitleEdit.Core.ContainerFormats.TransportStream;
|
||||
using Idx = Nikse.SubtitleEdit.Core.VobSub.Idx;
|
||||
|
||||
namespace Nikse.SubtitleEdit.Forms
|
||||
@ -1152,10 +1153,84 @@ namespace Nikse.SubtitleEdit.Forms
|
||||
item.SubItems[3].Text = Configuration.Settings.Language.BatchConvert.Ocr + " " + progress;
|
||||
listViewInputFiles.Refresh();
|
||||
};
|
||||
vobSubOcr.InitializeBatch(fileName, Configuration.Settings.VobSubOcr, false, "Tesseract");
|
||||
vobSubOcr.InitializeBatch(fileName, Configuration.Settings.VobSubOcr, false, null);
|
||||
sub = vobSubOcr.SubtitleFromOcr;
|
||||
}
|
||||
}
|
||||
else if (isTs)
|
||||
{
|
||||
var programMapTableParser = new ProgramMapTableParser();
|
||||
programMapTableParser.Parse(fileName); // get languages
|
||||
var tsParser = new TransportStreamParser();
|
||||
tsParser.Parse(fileName, (position, total) =>
|
||||
{
|
||||
var percent = (int)Math.Round(position * 100.0 / total);
|
||||
item.SubItems[3].Text = $"Read: {percent}%";
|
||||
listViewInputFiles.Refresh();
|
||||
});
|
||||
|
||||
var outputFolder = textBoxOutputFolder.Text;
|
||||
var overwrite = checkBoxOverwrite.Checked;
|
||||
if (radioButtonSaveInSourceFolder.Checked)
|
||||
{
|
||||
outputFolder = Path.GetDirectoryName(fileName);
|
||||
}
|
||||
|
||||
var targetEncoding = GetCurrentEncoding(fileName);
|
||||
|
||||
var targetFrameRate = 0.0;
|
||||
if (double.TryParse(comboBoxFrameRateTo.Text.Replace(',', '.').Replace(CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator, "."), NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out var toFrameRate))
|
||||
{
|
||||
targetFrameRate = toFrameRate;
|
||||
}
|
||||
|
||||
// images
|
||||
foreach (int id in tsParser.SubtitlePacketIds)
|
||||
{
|
||||
void ProgressCallback(string progress)
|
||||
{
|
||||
item.SubItems[3].Text = progress;
|
||||
listViewInputFiles.Refresh();
|
||||
}
|
||||
|
||||
if (BluRaySubtitle.RemoveChar(' ').Equals(toFormat.RemoveChar(' '), StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
TsToBluRaySup.WriteTrack(fileName, outputFolder, overwrite, 0, null, ProgressCallback, null, programMapTableParser, id, tsParser);
|
||||
}
|
||||
else if (BdnXmlSubtitle.RemoveChar(' ').Equals(toFormat.RemoveChar(' '), StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
TsToBdnXml.WriteTrack(fileName, outputFolder, overwrite, null, ProgressCallback, null, programMapTableParser, id, tsParser);
|
||||
}
|
||||
else
|
||||
{
|
||||
var tsBinaryParagraphs = new List<IBinaryParagraph>();
|
||||
var subtitle = new Subtitle();
|
||||
foreach (var transportStreamSubtitle in tsParser.GetDvbSubtitles(id))
|
||||
{
|
||||
binaryParagraphs.Add(transportStreamSubtitle);
|
||||
subtitle.Paragraphs.Add(new Paragraph(string.Empty, transportStreamSubtitle.StartMilliseconds, transportStreamSubtitle.EndMilliseconds));
|
||||
}
|
||||
|
||||
var preExt = TsToBluRaySup.GetFileNameEnding(programMapTableParser, id);
|
||||
int dummy = 0;
|
||||
CommandLineConverter.BatchConvertSave(toFormat, TimeSpan.Zero, targetEncoding, outputFolder, 0, ref dummy, ref dummy, SubtitleFormat.AllSubtitleFormats.ToList(), fileName, subtitle, new SubRip(), tsBinaryParagraphs, overwrite, 0, targetFrameRate, null, new List<CommandLineConverter.BatchAction>(), null, true, null, null, null, preExt);
|
||||
}
|
||||
}
|
||||
|
||||
// teletext
|
||||
foreach (var program in tsParser.TeletextSubtitlesLookup)
|
||||
{
|
||||
foreach (var kvp in program.Value)
|
||||
{
|
||||
var subtitle = new Subtitle(kvp.Value);
|
||||
subtitle.Renumber();
|
||||
var preExt = TsToBluRaySup.GetFileNameEnding(programMapTableParser, kvp.Key);
|
||||
int dummy = 0;
|
||||
CommandLineConverter.BatchConvertSave(toFormat, TimeSpan.Zero, targetEncoding, outputFolder, 0, ref dummy, ref dummy, SubtitleFormat.AllSubtitleFormats.ToList(), fileName, subtitle, new SubRip(), null, overwrite, 0, targetFrameRate, null, new List<CommandLineConverter.BatchAction>(), null, true, null, null, null, preExt);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (comboBoxSubtitleFormats.Text == AdvancedSubStationAlpha.NameOfFormat && _assStyle != null)
|
||||
{
|
||||
if (!string.IsNullOrWhiteSpace(_assStyle) && !checkBoxUseStyleFromSource.Checked)
|
||||
@ -1748,21 +1823,10 @@ namespace Nikse.SubtitleEdit.Forms
|
||||
if (ext != null && (ext.Equals(".ts", StringComparison.OrdinalIgnoreCase) || ext.Equals(".m2ts", StringComparison.OrdinalIgnoreCase) || ext.Equals(".mts", StringComparison.OrdinalIgnoreCase)) &&
|
||||
(FileUtil.IsTransportStream(p.FileName) || FileUtil.IsM2TransportStream(p.FileName)))
|
||||
{
|
||||
if (p.ToFormat == BluRaySubtitle || p.ToFormat == BdnXmlSubtitle)
|
||||
{
|
||||
progressCallback = progress =>
|
||||
{
|
||||
p.Item.SubItems[3].Text = progress;
|
||||
listViewInputFiles.Refresh();
|
||||
};
|
||||
}
|
||||
else
|
||||
{
|
||||
p.Item.SubItems[3].Text = $"Only {BluRaySubtitle} or {BdnXmlSubtitle}";
|
||||
IncrementAndShowProgress();
|
||||
return;
|
||||
}
|
||||
IncrementAndShowProgress();
|
||||
return;
|
||||
}
|
||||
|
||||
p.SourceFormat = new SubRip();
|
||||
}
|
||||
|
||||
|
@ -131,6 +131,7 @@ namespace Nikse.SubtitleEdit.Logic.CommandLineConvert
|
||||
_stdOutWriter.WriteLine(" /outputfolder:<folder name>");
|
||||
_stdOutWriter.WriteLine(" /overwrite");
|
||||
_stdOutWriter.WriteLine(" /forcedonly");
|
||||
_stdOutWriter.WriteLine(" /teletextonly");
|
||||
_stdOutWriter.WriteLine(" /multiplereplace:<comma separated file name list> ('.' represents the default replace rules)");
|
||||
_stdOutWriter.WriteLine(" /multiplereplace (equivalent to /multiplereplace:.)");
|
||||
_stdOutWriter.WriteLine(" /ebuheaderfile:<file name>");
|
||||
@ -392,6 +393,7 @@ namespace Nikse.SubtitleEdit.Logic.CommandLineConvert
|
||||
|
||||
bool overwrite = GetArgument(unconsumedArguments, "overwrite").Length > 0;
|
||||
bool forcedOnly = GetArgument(unconsumedArguments, "forcedonly").Length > 0;
|
||||
bool teletextOnly = GetArgument(unconsumedArguments, "teletextonly").Length > 0;
|
||||
|
||||
var patterns = new List<string>();
|
||||
|
||||
@ -664,7 +666,12 @@ namespace Nikse.SubtitleEdit.Logic.CommandLineConvert
|
||||
Path.GetExtension(fileName).Equals(".mts", StringComparison.OrdinalIgnoreCase) ||
|
||||
Path.GetExtension(fileName).Equals(".m2ts", StringComparison.OrdinalIgnoreCase)) && (FileUtil.IsTransportStream(fileName) || FileUtil.IsM2TransportStream(fileName)))
|
||||
{
|
||||
TsConvert.ConvertFromTs(targetFormat, fileName, outputFolder, overwrite, ref count, ref converted, ref errors, formats, _stdOutWriter, null, resolution, targetEncoding, actions, offset, pacCodePage, targetFrameRate, multipleReplaceImportFiles, ocrEngine);
|
||||
var ok = TsConvert.ConvertFromTs(targetFormat, fileName, outputFolder, overwrite, ref count, ref converted, ref errors, formats, _stdOutWriter, null, resolution, targetEncoding, actions, offset, pacCodePage, targetFrameRate, multipleReplaceImportFiles, ocrEngine, teletextOnly);
|
||||
if (ok)
|
||||
{
|
||||
converted++;
|
||||
}
|
||||
|
||||
done = true;
|
||||
}
|
||||
|
||||
@ -1101,8 +1108,13 @@ namespace Nikse.SubtitleEdit.Logic.CommandLineConvert
|
||||
internal static bool BatchConvertSave(string targetFormat, TimeSpan offset, TextEncoding targetEncoding, string outputFolder, int count, ref int converted, ref int errors,
|
||||
List<SubtitleFormat> formats, string fileName, Subtitle sub, SubtitleFormat format, IList<IBinaryParagraph> binaryParagraphs, bool overwrite, int pacCodePage,
|
||||
double? targetFrameRate, ICollection<string> multipleReplaceImportFiles, List<BatchAction> actions = null,
|
||||
Point? resolution = null, bool autoDetectLanguage = false, BatchConvertProgress progressCallback = null, string ebuHeaderFile = null, string ocrEngine = null)
|
||||
Point? resolution = null, bool autoDetectLanguage = false, BatchConvertProgress progressCallback = null, string ebuHeaderFile = null, string ocrEngine = null, string preExt = null)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(preExt))
|
||||
{
|
||||
preExt = string.Empty;
|
||||
}
|
||||
|
||||
double oldFrameRate = Configuration.Settings.General.CurrentFrameRate;
|
||||
try
|
||||
{
|
||||
@ -1159,7 +1171,7 @@ namespace Nikse.SubtitleEdit.Logic.CommandLineConvert
|
||||
{
|
||||
targetFormatFound = true;
|
||||
sf.BatchMode = true;
|
||||
outputFileName = FormatOutputFileNameForBatchConvert(fileName, sf.Extension, outputFolder, overwrite);
|
||||
outputFileName = FormatOutputFileNameForBatchConvert(fileName, preExt + sf.Extension, outputFolder, overwrite);
|
||||
_stdOutWriter?.Write($"{count}: {Path.GetFileName(fileName)} -> {outputFileName}...");
|
||||
|
||||
if (sf.GetType() == typeof(WebVTT) || sf.GetType() == typeof(WebVTTFileWithLineNumber))
|
||||
@ -1365,80 +1377,69 @@ namespace Nikse.SubtitleEdit.Logic.CommandLineConvert
|
||||
if (BatchConvert.BluRaySubtitle.RemoveChar(' ').Equals(targetFormat.RemoveChar(' '), StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
targetFormatFound = true;
|
||||
var ext = Path.GetExtension(fileName);
|
||||
if ((ext.Equals(".ts", StringComparison.OrdinalIgnoreCase) ||
|
||||
ext.Equals(".mts", StringComparison.OrdinalIgnoreCase) ||
|
||||
ext.Equals(".m2ts", StringComparison.OrdinalIgnoreCase)) &&
|
||||
(FileUtil.IsTransportStream(fileName) || FileUtil.IsM2TransportStream(fileName)))
|
||||
outputFileName = FormatOutputFileNameForBatchConvert(fileName, preExt + ".sup", outputFolder, overwrite);
|
||||
_stdOutWriter?.Write($"{count}: {Path.GetFileName(fileName)} -> {outputFileName}...");
|
||||
using (var form = new ExportPngXml())
|
||||
{
|
||||
success = TsToBluRaySup.ConvertFromTsToBluRaySup(fileName, outputFolder, overwrite, count, _stdOutWriter, progressCallback, resolution);
|
||||
}
|
||||
else
|
||||
{
|
||||
outputFileName = FormatOutputFileNameForBatchConvert(fileName, ".sup", outputFolder, overwrite);
|
||||
_stdOutWriter?.Write($"{count}: {Path.GetFileName(fileName)} -> {outputFileName}...");
|
||||
using (var form = new ExportPngXml())
|
||||
form.Initialize(sub, format, ExportPngXml.ExportFormats.BluraySup, fileName, null, null);
|
||||
int width = 1920;
|
||||
int height = 1080;
|
||||
if (!string.IsNullOrEmpty(Configuration.Settings.Tools.ExportBluRayVideoResolution))
|
||||
{
|
||||
form.Initialize(sub, format, ExportPngXml.ExportFormats.BluraySup, fileName, null, null);
|
||||
int width = 1920;
|
||||
int height = 1080;
|
||||
if (!string.IsNullOrEmpty(Configuration.Settings.Tools.ExportBluRayVideoResolution))
|
||||
var parts = Configuration.Settings.Tools.ExportBluRayVideoResolution.Split('x');
|
||||
if (parts.Length == 2 && Utilities.IsInteger(parts[0]) && Utilities.IsInteger(parts[1]))
|
||||
{
|
||||
var parts = Configuration.Settings.Tools.ExportBluRayVideoResolution.Split('x');
|
||||
if (parts.Length == 2 && Utilities.IsInteger(parts[0]) && Utilities.IsInteger(parts[1]))
|
||||
{
|
||||
width = int.Parse(parts[0]);
|
||||
height = int.Parse(parts[1]);
|
||||
}
|
||||
if (resolution != null)
|
||||
{
|
||||
width = resolution.Value.X;
|
||||
height = resolution.Value.Y;
|
||||
}
|
||||
width = int.Parse(parts[0]);
|
||||
height = int.Parse(parts[1]);
|
||||
}
|
||||
|
||||
using (var binarySubtitleFile = new FileStream(outputFileName, FileMode.Create))
|
||||
if (resolution != null)
|
||||
{
|
||||
var isImageBased = IsImageBased(format);
|
||||
for (int index = 0; index < sub.Paragraphs.Count; index++)
|
||||
width = resolution.Value.X;
|
||||
height = resolution.Value.Y;
|
||||
}
|
||||
}
|
||||
|
||||
using (var binarySubtitleFile = new FileStream(outputFileName, FileMode.Create))
|
||||
{
|
||||
var isImageBased = IsImageBased(format);
|
||||
for (int index = 0; index < sub.Paragraphs.Count; index++)
|
||||
{
|
||||
var mp = form.MakeMakeBitmapParameter(index, width, height);
|
||||
mp.LineJoin = Configuration.Settings.Tools.ExportPenLineJoin;
|
||||
if (binaryParagraphs != null && binaryParagraphs.Count > 0)
|
||||
{
|
||||
var mp = form.MakeMakeBitmapParameter(index, width, height);
|
||||
mp.LineJoin = Configuration.Settings.Tools.ExportPenLineJoin;
|
||||
if (binaryParagraphs != null && binaryParagraphs.Count > 0)
|
||||
if (index < binaryParagraphs.Count)
|
||||
{
|
||||
if (index < binaryParagraphs.Count)
|
||||
{
|
||||
mp.Bitmap = binaryParagraphs[index].GetBitmap();
|
||||
mp.Forced = binaryParagraphs[index].IsForced;
|
||||
}
|
||||
mp.Bitmap = binaryParagraphs[index].GetBitmap();
|
||||
mp.Forced = binaryParagraphs[index].IsForced;
|
||||
}
|
||||
else if (isImageBased)
|
||||
}
|
||||
else if (isImageBased)
|
||||
{
|
||||
using (var ms = new MemoryStream(File.ReadAllBytes(Path.Combine(Path.GetDirectoryName(fileName), sub.Paragraphs[index].Text))))
|
||||
{
|
||||
using (var ms = new MemoryStream(File.ReadAllBytes(Path.Combine(Path.GetDirectoryName(fileName), sub.Paragraphs[index].Text))))
|
||||
{
|
||||
mp.Bitmap = (Bitmap)Image.FromStream(ms);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
mp.Bitmap = ExportPngXml.GenerateImageFromTextWithStyle(mp);
|
||||
}
|
||||
ExportPngXml.MakeBluRaySupImage(mp);
|
||||
binarySubtitleFile.Write(mp.Buffer, 0, mp.Buffer.Length);
|
||||
if (mp.Bitmap != null)
|
||||
{
|
||||
mp.Bitmap.Dispose();
|
||||
mp.Bitmap = null;
|
||||
}
|
||||
if (index % 50 == 0)
|
||||
{
|
||||
System.Windows.Forms.Application.DoEvents();
|
||||
mp.Bitmap = (Bitmap)Image.FromStream(ms);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
mp.Bitmap = ExportPngXml.GenerateImageFromTextWithStyle(mp);
|
||||
}
|
||||
ExportPngXml.MakeBluRaySupImage(mp);
|
||||
binarySubtitleFile.Write(mp.Buffer, 0, mp.Buffer.Length);
|
||||
if (mp.Bitmap != null)
|
||||
{
|
||||
mp.Bitmap.Dispose();
|
||||
mp.Bitmap = null;
|
||||
}
|
||||
if (index % 50 == 0)
|
||||
{
|
||||
System.Windows.Forms.Application.DoEvents();
|
||||
}
|
||||
}
|
||||
}
|
||||
_stdOutWriter?.WriteLine(" done.");
|
||||
}
|
||||
_stdOutWriter?.WriteLine(" done.");
|
||||
}
|
||||
|
||||
else if (BatchConvert.VobSubSubtitle.RemoveChar(' ').Equals(targetFormat.RemoveChar(' '), StringComparison.OrdinalIgnoreCase))
|
||||
@ -1597,73 +1598,62 @@ namespace Nikse.SubtitleEdit.Logic.CommandLineConvert
|
||||
else if (BatchConvert.BdnXmlSubtitle.RemoveChar(' ').Equals(targetFormat.RemoveChar(' '), StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
targetFormatFound = true;
|
||||
var ext = Path.GetExtension(fileName);
|
||||
if ((ext.Equals(".ts", StringComparison.OrdinalIgnoreCase) ||
|
||||
ext.Equals(".mts", StringComparison.OrdinalIgnoreCase) ||
|
||||
ext.Equals(".m2ts", StringComparison.OrdinalIgnoreCase)) &&
|
||||
(FileUtil.IsTransportStream(fileName) || FileUtil.IsM2TransportStream(fileName)))
|
||||
outputFileName = FormatOutputFileNameForBatchConvert(fileName, ".xml", outputFolder, overwrite);
|
||||
_stdOutWriter?.Write($"{count}: {Path.GetFileName(fileName)} -> {outputFileName}...");
|
||||
using (var form = new ExportPngXml())
|
||||
{
|
||||
success = TsToBdnXml.ConvertFromTsToBdnXml(fileName, outputFolder, overwrite, _stdOutWriter, progressCallback, resolution);
|
||||
}
|
||||
else
|
||||
{
|
||||
outputFileName = FormatOutputFileNameForBatchConvert(fileName, ".xml", outputFolder, overwrite);
|
||||
_stdOutWriter?.Write($"{count}: {Path.GetFileName(fileName)} -> {outputFileName}...");
|
||||
using (var form = new ExportPngXml())
|
||||
form.Initialize(sub, format, ExportPngXml.ExportFormats.BdnXml, fileName, null, null);
|
||||
int width = 1920;
|
||||
int height = 1080;
|
||||
if (!string.IsNullOrEmpty(Configuration.Settings.Tools.ExportBluRayVideoResolution))
|
||||
{
|
||||
form.Initialize(sub, format, ExportPngXml.ExportFormats.BdnXml, fileName, null, null);
|
||||
int width = 1920;
|
||||
int height = 1080;
|
||||
if (!string.IsNullOrEmpty(Configuration.Settings.Tools.ExportBluRayVideoResolution))
|
||||
var parts = Configuration.Settings.Tools.ExportBluRayVideoResolution.Split('x');
|
||||
if (parts.Length == 2 && Utilities.IsInteger(parts[0]) && Utilities.IsInteger(parts[1]))
|
||||
{
|
||||
var parts = Configuration.Settings.Tools.ExportBluRayVideoResolution.Split('x');
|
||||
if (parts.Length == 2 && Utilities.IsInteger(parts[0]) && Utilities.IsInteger(parts[1]))
|
||||
{
|
||||
width = int.Parse(parts[0]);
|
||||
height = int.Parse(parts[1]);
|
||||
}
|
||||
width = int.Parse(parts[0]);
|
||||
height = int.Parse(parts[1]);
|
||||
}
|
||||
if (resolution != null)
|
||||
{
|
||||
width = resolution.Value.X;
|
||||
height = resolution.Value.Y;
|
||||
}
|
||||
|
||||
var sb = new StringBuilder();
|
||||
var imagesSavedCount = 0;
|
||||
var isImageBased = IsImageBased(format);
|
||||
for (int index = 0; index < sub.Paragraphs.Count; index++)
|
||||
{
|
||||
var mp = form.MakeMakeBitmapParameter(index, width, height);
|
||||
mp.LineJoin = Configuration.Settings.Tools.ExportPenLineJoin;
|
||||
if (binaryParagraphs != null && binaryParagraphs.Count > 0)
|
||||
{
|
||||
if (index < binaryParagraphs.Count)
|
||||
{
|
||||
mp.Bitmap = binaryParagraphs[index].GetBitmap();
|
||||
mp.Forced = binaryParagraphs[index].IsForced;
|
||||
}
|
||||
}
|
||||
else if (isImageBased)
|
||||
{
|
||||
using (var ms = new MemoryStream(File.ReadAllBytes(Path.Combine(Path.GetDirectoryName(fileName), sub.Paragraphs[index].Text))))
|
||||
{
|
||||
mp.Bitmap = (Bitmap)Image.FromStream(ms);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
mp.Bitmap = ExportPngXml.GenerateImageFromTextWithStyle(mp);
|
||||
}
|
||||
imagesSavedCount = form.WriteBdnXmlParagraph(width, sb, form.GetBottomMarginInPixels(sub.Paragraphs[index]), height, imagesSavedCount, mp, index, Path.GetDirectoryName(outputFileName));
|
||||
|
||||
if (index % 50 == 0)
|
||||
{
|
||||
System.Windows.Forms.Application.DoEvents();
|
||||
}
|
||||
}
|
||||
form.WriteBdnXmlFile(imagesSavedCount, sb, outputFileName);
|
||||
}
|
||||
if (resolution != null)
|
||||
{
|
||||
width = resolution.Value.X;
|
||||
height = resolution.Value.Y;
|
||||
}
|
||||
|
||||
var sb = new StringBuilder();
|
||||
var imagesSavedCount = 0;
|
||||
var isImageBased = IsImageBased(format);
|
||||
for (int index = 0; index < sub.Paragraphs.Count; index++)
|
||||
{
|
||||
var mp = form.MakeMakeBitmapParameter(index, width, height);
|
||||
mp.LineJoin = Configuration.Settings.Tools.ExportPenLineJoin;
|
||||
if (binaryParagraphs != null && binaryParagraphs.Count > 0)
|
||||
{
|
||||
if (index < binaryParagraphs.Count)
|
||||
{
|
||||
mp.Bitmap = binaryParagraphs[index].GetBitmap();
|
||||
mp.Forced = binaryParagraphs[index].IsForced;
|
||||
}
|
||||
}
|
||||
else if (isImageBased)
|
||||
{
|
||||
using (var ms = new MemoryStream(File.ReadAllBytes(Path.Combine(Path.GetDirectoryName(fileName), sub.Paragraphs[index].Text))))
|
||||
{
|
||||
mp.Bitmap = (Bitmap)Image.FromStream(ms);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
mp.Bitmap = ExportPngXml.GenerateImageFromTextWithStyle(mp);
|
||||
}
|
||||
imagesSavedCount = form.WriteBdnXmlParagraph(width, sb, form.GetBottomMarginInPixels(sub.Paragraphs[index]), height, imagesSavedCount, mp, index, Path.GetDirectoryName(outputFileName));
|
||||
|
||||
if (index % 50 == 0)
|
||||
{
|
||||
System.Windows.Forms.Application.DoEvents();
|
||||
}
|
||||
}
|
||||
form.WriteBdnXmlFile(imagesSavedCount, sb, outputFileName);
|
||||
}
|
||||
_stdOutWriter?.WriteLine(" done.");
|
||||
}
|
||||
|
@ -12,25 +12,9 @@ namespace Nikse.SubtitleEdit.Logic.CommandLineConvert
|
||||
{
|
||||
public static class TsConvert
|
||||
{
|
||||
public static bool ConvertFromTs(string targetFormat, string fileName, string outputFolder, bool overwrite, ref int count, ref int converted, ref int errors, List<SubtitleFormat> formats, StreamWriter stdOutWriter, CommandLineConverter.BatchConvertProgress progressCallback, Point? resolution, TextEncoding targetEncoding, List<CommandLineConverter.BatchAction> actions, TimeSpan offset, int pacCodePage, double? targetFrameRate, HashSet<string> multipleReplaceImportFiles, string ocrEngine)
|
||||
public static bool ConvertFromTs(string targetFormat, string fileName, string outputFolder, bool overwrite, ref int count, ref int converted, ref int errors, List<SubtitleFormat> formats, StreamWriter stdOutWriter, CommandLineConverter.BatchConvertProgress progressCallback, Point? resolution, TextEncoding targetEncoding, List<CommandLineConverter.BatchAction> actions, TimeSpan offset, int pacCodePage, double? targetFrameRate, HashSet<string> multipleReplaceImportFiles, string ocrEngine, bool teletextOnly)
|
||||
{
|
||||
var success = false;
|
||||
bool teletextOnly = false;
|
||||
|
||||
if (BatchConvert.BluRaySubtitle.RemoveChar(' ').Equals(targetFormat.RemoveChar(' '), StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
success = TsToBluRaySup.ConvertFromTsToBluRaySup(fileName, outputFolder, overwrite, count, stdOutWriter, progressCallback, resolution);
|
||||
converted++;
|
||||
teletextOnly = true;
|
||||
}
|
||||
|
||||
if (BatchConvert.BdnXmlSubtitle.RemoveChar(' ').Equals(targetFormat.RemoveChar(' '), StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
success = TsToBdnXml.ConvertFromTsToBdnXml(fileName, outputFolder, overwrite, stdOutWriter, progressCallback, resolution);
|
||||
converted++;
|
||||
teletextOnly = true;
|
||||
}
|
||||
|
||||
var programMapTableParser = new ProgramMapTableParser();
|
||||
programMapTableParser.Parse(fileName); // get languages
|
||||
var tsParser = new TransportStreamParser();
|
||||
@ -47,16 +31,33 @@ namespace Nikse.SubtitleEdit.Logic.CommandLineConvert
|
||||
{
|
||||
foreach (int id in tsParser.SubtitlePacketIds)
|
||||
{
|
||||
var binaryParagraphs = new List<IBinaryParagraph>();
|
||||
var subtitle = new Subtitle();
|
||||
foreach (var transportStreamSubtitle in tsParser.GetDvbSubtitles(id))
|
||||
if (BatchConvert.BluRaySubtitle.RemoveChar(' ').Equals(targetFormat.RemoveChar(' '), StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
binaryParagraphs.Add(transportStreamSubtitle);
|
||||
subtitle.Paragraphs.Add(new Paragraph(string.Empty, transportStreamSubtitle.StartMilliseconds, transportStreamSubtitle.EndMilliseconds));
|
||||
TsToBluRaySup.WriteTrack(fileName, outputFolder, overwrite, count, stdOutWriter, progressCallback, resolution, programMapTableParser, id, tsParser);
|
||||
success = true;
|
||||
}
|
||||
else if (BatchConvert.BdnXmlSubtitle.RemoveChar(' ').Equals(targetFormat.RemoveChar(' '), StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
TsToBdnXml.WriteTrack(fileName, outputFolder, overwrite, stdOutWriter, progressCallback, resolution, programMapTableParser, id, tsParser);
|
||||
success = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
var preExt = TsToBluRaySup.GetFileNameEnding(programMapTableParser, id);
|
||||
var binaryParagraphs = new List<IBinaryParagraph>();
|
||||
var subtitle = new Subtitle();
|
||||
foreach (var transportStreamSubtitle in tsParser.GetDvbSubtitles(id))
|
||||
{
|
||||
binaryParagraphs.Add(transportStreamSubtitle);
|
||||
subtitle.Paragraphs.Add(new Paragraph(string.Empty, transportStreamSubtitle.StartMilliseconds, transportStreamSubtitle.EndMilliseconds));
|
||||
}
|
||||
|
||||
//TODO: use language or PID in file name - var language = programMapTableParser.GetSubtitleLanguage(id);
|
||||
success = CommandLineConverter.BatchConvertSave(targetFormat, offset, targetEncoding, outputFolder, count, ref converted, ref errors, formats, fileName, subtitle, new SubRip(), binaryParagraphs, overwrite, pacCodePage, targetFrameRate, multipleReplaceImportFiles, actions, resolution, true, null, null, ocrEngine);
|
||||
success = CommandLineConverter.BatchConvertSave(targetFormat, offset, targetEncoding, outputFolder, count, ref converted, ref errors, formats, fileName, subtitle, new SubRip(), binaryParagraphs, overwrite, pacCodePage, targetFrameRate, multipleReplaceImportFiles, actions, resolution, true, null, null, ocrEngine, preExt);
|
||||
if (success)
|
||||
{
|
||||
converted--;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -67,8 +68,12 @@ namespace Nikse.SubtitleEdit.Logic.CommandLineConvert
|
||||
{
|
||||
var subtitle = new Subtitle(kvp.Value);
|
||||
subtitle.Renumber();
|
||||
//TODO: use language or PID in file name
|
||||
success = CommandLineConverter.BatchConvertSave(targetFormat, offset, targetEncoding, outputFolder, count, ref converted, ref errors, formats, fileName, subtitle, new SubRip(), null, overwrite, pacCodePage, targetFrameRate, multipleReplaceImportFiles, actions, resolution, true);
|
||||
var preExt = TsToBluRaySup.GetFileNameEnding(programMapTableParser, kvp.Key);
|
||||
success = CommandLineConverter.BatchConvertSave(targetFormat, offset, targetEncoding, outputFolder, count, ref converted, ref errors, formats, fileName, subtitle, new SubRip(), null, overwrite, pacCodePage, targetFrameRate, multipleReplaceImportFiles, actions, resolution, true, null, null, null, preExt);
|
||||
if (success)
|
||||
{
|
||||
converted--;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -11,20 +11,8 @@ namespace Nikse.SubtitleEdit.Logic.CommandLineConvert
|
||||
{
|
||||
public static class TsToBdnXml
|
||||
{
|
||||
public static bool ConvertFromTsToBdnXml(string fileName, string outputFolder, bool overwrite, StreamWriter stdOutWriter, CommandLineConverter.BatchConvertProgress progressCallback, Point? resolution)
|
||||
internal static void WriteTrack(string fileName, string outputFolder, bool overwrite, StreamWriter stdOutWriter, CommandLineConverter.BatchConvertProgress progressCallback, Point? resolution, ProgramMapTableParser programMapTableParser, int pid, TransportStreamParser tsParser)
|
||||
{
|
||||
var programMapTableParser = new ProgramMapTableParser();
|
||||
programMapTableParser.Parse(fileName); // get languages from PMT if possible
|
||||
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");
|
||||
|
||||
var overrideScreenSize = Configuration.Settings.Tools.BatchConvertTsOverrideScreenSize &&
|
||||
Configuration.Settings.Tools.BatchConvertTsScreenHeight > 0 &&
|
||||
Configuration.Settings.Tools.BatchConvertTsScreenWidth > 0 ||
|
||||
@ -32,115 +20,110 @@ namespace Nikse.SubtitleEdit.Logic.CommandLineConvert
|
||||
|
||||
using (var form = new ExportPngXml())
|
||||
{
|
||||
if (tsParser.SubtitlePacketIds.Count == 0)
|
||||
var language = TsToBluRaySup.GetFileNameEnding(programMapTableParser, pid);
|
||||
var nameNoExt = Utilities.GetFileNameWithoutExtension(fileName) + "." + language;
|
||||
var folder = Path.Combine(outputFolder, nameNoExt);
|
||||
if (!Directory.Exists(folder))
|
||||
{
|
||||
stdOutWriter?.WriteLine("No subtitles found");
|
||||
progressCallback?.Invoke("No subtitles found");
|
||||
return false;
|
||||
Directory.CreateDirectory(folder);
|
||||
}
|
||||
foreach (int pid in tsParser.SubtitlePacketIds)
|
||||
|
||||
var outputFileName = CommandLineConverter.FormatOutputFileNameForBatchConvert(nameNoExt + Path.GetExtension(fileName), ".xml", folder, 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)
|
||||
{
|
||||
var language = TsToBluRaySup.GetFileNameEnding(programMapTableParser, pid);
|
||||
var nameNoExt = Utilities.GetFileNameWithoutExtension(fileName) + "." + language;
|
||||
var folder = Path.Combine(outputFolder, nameNoExt);
|
||||
if (!Directory.Exists(folder))
|
||||
{
|
||||
Directory.CreateDirectory(folder);
|
||||
}
|
||||
var outputFileName = CommandLineConverter.FormatOutputFileNameForBatchConvert(nameNoExt + Path.GetExtension(fileName), ".xml", folder, 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)
|
||||
{
|
||||
subtitle.Paragraphs.Add(new Paragraph(string.Empty, p.StartMilliseconds, p.EndMilliseconds));
|
||||
}
|
||||
subtitle.Paragraphs.Add(new Paragraph(string.Empty, p.StartMilliseconds, p.EndMilliseconds));
|
||||
}
|
||||
|
||||
var res = TsToBluRaySup.GetSubtitleScreenSize(sub, overrideScreenSize, resolution);
|
||||
var videoInfo = new VideoInfo { Success = true, Width = res.X, Height = res.Y };
|
||||
form.Initialize(subtitle, new SubRip(), BatchConvert.BdnXmlSubtitle, fileName, videoInfo, fileName);
|
||||
var sb = new StringBuilder();
|
||||
var imagesSavedCount = 0;
|
||||
for (int index = 0; index < sub.Count; index++)
|
||||
var res = TsToBluRaySup.GetSubtitleScreenSize(sub, overrideScreenSize, resolution);
|
||||
var videoInfo = new VideoInfo { Success = true, Width = res.X, Height = res.Y };
|
||||
form.Initialize(subtitle, new SubRip(), BatchConvert.BdnXmlSubtitle, fileName, videoInfo, fileName);
|
||||
var sb = new StringBuilder();
|
||||
var imagesSavedCount = 0;
|
||||
for (int index = 0; index < sub.Count; index++)
|
||||
{
|
||||
var p = sub[index];
|
||||
var pos = p.GetPosition();
|
||||
var bmp = sub[index].GetBitmap();
|
||||
var tsWidth = bmp.Width;
|
||||
var tsHeight = bmp.Height;
|
||||
var nBmp = new NikseBitmap(bmp);
|
||||
pos.Top += nBmp.CropTopTransparent(0);
|
||||
pos.Left += nBmp.CropSidesAndBottom(0, Color.FromArgb(0, 0, 0, 0), true);
|
||||
bmp.Dispose();
|
||||
bmp = nBmp.GetBitmap();
|
||||
var mp = form.MakeMakeBitmapParameter(index, videoInfo.Width, videoInfo.Height);
|
||||
|
||||
if (overrideScreenSize)
|
||||
{
|
||||
var p = sub[index];
|
||||
var pos = p.GetPosition();
|
||||
var bmp = sub[index].GetBitmap();
|
||||
var tsWidth = bmp.Width;
|
||||
var tsHeight = bmp.Height;
|
||||
var nBmp = new NikseBitmap(bmp);
|
||||
pos.Top += nBmp.CropTopTransparent(0);
|
||||
pos.Left += nBmp.CropSidesAndBottom(0, Color.FromArgb(0, 0, 0, 0), true);
|
||||
var widthFactor = (double)videoInfo.Width / tsWidth;
|
||||
var heightFactor = (double)videoInfo.Height / tsHeight;
|
||||
var resizeBmp = ResizeBitmap(bmp, (int)Math.Round(bmp.Width * widthFactor), (int)Math.Round(bmp.Height * heightFactor));
|
||||
bmp.Dispose();
|
||||
bmp = nBmp.GetBitmap();
|
||||
var mp = form.MakeMakeBitmapParameter(index, videoInfo.Width, videoInfo.Height);
|
||||
bmp = resizeBmp;
|
||||
pos.Left = (int)Math.Round(pos.Left * widthFactor);
|
||||
pos.Top = (int)Math.Round(pos.Top * heightFactor);
|
||||
progressCallback?.Invoke($"Save PID {pid}: {(index + 1) * 100 / sub.Count}%");
|
||||
}
|
||||
|
||||
if (overrideScreenSize)
|
||||
mp.Bitmap = bmp;
|
||||
mp.P = new Paragraph(string.Empty, p.StartMilliseconds, p.EndMilliseconds);
|
||||
mp.ScreenWidth = videoInfo.Width;
|
||||
mp.ScreenHeight = videoInfo.Height;
|
||||
int bottomMarginInPixels;
|
||||
if (Configuration.Settings.Tools.BatchConvertTsOverrideXPosition || Configuration.Settings.Tools.BatchConvertTsOverrideYPosition)
|
||||
{
|
||||
if (Configuration.Settings.Tools.BatchConvertTsOverrideXPosition && Configuration.Settings.Tools.BatchConvertTsOverrideYPosition)
|
||||
{
|
||||
var widthFactor = (double)videoInfo.Width / tsWidth;
|
||||
var heightFactor = (double)videoInfo.Height / tsHeight;
|
||||
var resizeBmp = ResizeBitmap(bmp, (int)Math.Round(bmp.Width * widthFactor), (int)Math.Round(bmp.Height * heightFactor));
|
||||
bmp.Dispose();
|
||||
bmp = resizeBmp;
|
||||
pos.Left = (int)Math.Round(pos.Left * widthFactor);
|
||||
pos.Top = (int)Math.Round(pos.Top * heightFactor);
|
||||
progressCallback?.Invoke($"Save PID {pid}: {(index + 1) * 100 / sub.Count}%");
|
||||
var x = (int)Math.Round(videoInfo.Width / 2.0 - mp.Bitmap.Width / 2.0);
|
||||
if (Configuration.Settings.Tools.BatchConvertTsOverrideHAlign.Equals("left", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
x = Configuration.Settings.Tools.BatchConvertTsOverrideHMargin;
|
||||
}
|
||||
else if (Configuration.Settings.Tools.BatchConvertTsOverrideHAlign.Equals("right", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
x = videoInfo.Width - Configuration.Settings.Tools.BatchConvertTsOverrideHMargin - mp.Bitmap.Width;
|
||||
}
|
||||
|
||||
var y = videoInfo.Height - Configuration.Settings.Tools.BatchConvertTsOverrideBottomMargin - mp.Bitmap.Height;
|
||||
mp.OverridePosition = new Point(x, y);
|
||||
}
|
||||
|
||||
mp.Bitmap = bmp;
|
||||
mp.P = new Paragraph(string.Empty, p.StartMilliseconds, p.EndMilliseconds);
|
||||
mp.ScreenWidth = videoInfo.Width;
|
||||
mp.ScreenHeight = videoInfo.Height;
|
||||
int bottomMarginInPixels;
|
||||
if (Configuration.Settings.Tools.BatchConvertTsOverrideXPosition || Configuration.Settings.Tools.BatchConvertTsOverrideYPosition)
|
||||
else if (Configuration.Settings.Tools.BatchConvertTsOverrideXPosition)
|
||||
{
|
||||
if (Configuration.Settings.Tools.BatchConvertTsOverrideXPosition && Configuration.Settings.Tools.BatchConvertTsOverrideYPosition)
|
||||
var x = (int)Math.Round(videoInfo.Width / 2.0 - mp.Bitmap.Width / 2.0);
|
||||
if (Configuration.Settings.Tools.BatchConvertTsOverrideHAlign.Equals("left", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
var x = (int)Math.Round(videoInfo.Width / 2.0 - mp.Bitmap.Width / 2.0);
|
||||
if (Configuration.Settings.Tools.BatchConvertTsOverrideHAlign.Equals("left", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
x = Configuration.Settings.Tools.BatchConvertTsOverrideHMargin;
|
||||
}
|
||||
else if (Configuration.Settings.Tools.BatchConvertTsOverrideHAlign.Equals("right", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
x = videoInfo.Width - Configuration.Settings.Tools.BatchConvertTsOverrideHMargin - mp.Bitmap.Width;
|
||||
}
|
||||
var y = videoInfo.Height - Configuration.Settings.Tools.BatchConvertTsOverrideBottomMargin - mp.Bitmap.Height;
|
||||
mp.OverridePosition = new Point(x, y);
|
||||
x = Configuration.Settings.Tools.BatchConvertTsOverrideHMargin;
|
||||
}
|
||||
else if (Configuration.Settings.Tools.BatchConvertTsOverrideXPosition)
|
||||
else if (Configuration.Settings.Tools.BatchConvertTsOverrideHAlign.Equals("right", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
var x = (int)Math.Round(videoInfo.Width / 2.0 - mp.Bitmap.Width / 2.0);
|
||||
if (Configuration.Settings.Tools.BatchConvertTsOverrideHAlign.Equals("left", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
x = Configuration.Settings.Tools.BatchConvertTsOverrideHMargin;
|
||||
}
|
||||
else if (Configuration.Settings.Tools.BatchConvertTsOverrideHAlign.Equals("right", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
x = videoInfo.Width - Configuration.Settings.Tools.BatchConvertTsOverrideHMargin - mp.Bitmap.Width;
|
||||
}
|
||||
mp.OverridePosition = new Point(x, pos.Top);
|
||||
x = videoInfo.Width - Configuration.Settings.Tools.BatchConvertTsOverrideHMargin - mp.Bitmap.Width;
|
||||
}
|
||||
else
|
||||
{
|
||||
var y = videoInfo.Height - Configuration.Settings.Tools.BatchConvertTsOverrideBottomMargin - mp.Bitmap.Height;
|
||||
mp.OverridePosition = new Point(pos.Left, y);
|
||||
}
|
||||
bottomMarginInPixels = Configuration.Settings.Tools.BatchConvertTsScreenHeight - pos.Top - mp.Bitmap.Height;
|
||||
|
||||
mp.OverridePosition = new Point(x, pos.Top);
|
||||
}
|
||||
else
|
||||
{
|
||||
mp.OverridePosition = new Point(pos.Left, pos.Top); // use original position
|
||||
bottomMarginInPixels = Configuration.Settings.Tools.BatchConvertTsScreenHeight - pos.Top - mp.Bitmap.Height;
|
||||
var y = videoInfo.Height - Configuration.Settings.Tools.BatchConvertTsOverrideBottomMargin - mp.Bitmap.Height;
|
||||
mp.OverridePosition = new Point(pos.Left, y);
|
||||
}
|
||||
|
||||
imagesSavedCount = form.WriteBdnXmlParagraph(videoInfo.Width, sb, bottomMarginInPixels, videoInfo.Height, imagesSavedCount, mp, index, Path.GetDirectoryName(outputFileName));
|
||||
bottomMarginInPixels = Configuration.Settings.Tools.BatchConvertTsScreenHeight - pos.Top - mp.Bitmap.Height;
|
||||
}
|
||||
form.WriteBdnXmlFile(imagesSavedCount, sb, outputFileName);
|
||||
else
|
||||
{
|
||||
mp.OverridePosition = new Point(pos.Left, pos.Top); // use original position
|
||||
bottomMarginInPixels = Configuration.Settings.Tools.BatchConvertTsScreenHeight - pos.Top - mp.Bitmap.Height;
|
||||
}
|
||||
|
||||
imagesSavedCount = form.WriteBdnXmlParagraph(videoInfo.Width, sb, bottomMarginInPixels, videoInfo.Height, imagesSavedCount, mp, index, Path.GetDirectoryName(outputFileName));
|
||||
}
|
||||
|
||||
form.WriteBdnXmlFile(imagesSavedCount, sb, outputFileName);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private static Bitmap ResizeBitmap(Bitmap b, int width, int height)
|
||||
|
@ -12,20 +12,8 @@ namespace Nikse.SubtitleEdit.Logic.CommandLineConvert
|
||||
{
|
||||
public static class TsToBluRaySup
|
||||
{
|
||||
public static bool ConvertFromTsToBluRaySup(string fileName, string outputFolder, bool overwrite, int count, StreamWriter stdOutWriter, CommandLineConverter.BatchConvertProgress progressCallback, Point? resolution)
|
||||
internal static void WriteTrack(string fileName, string outputFolder, bool overwrite, int count, StreamWriter stdOutWriter, CommandLineConverter.BatchConvertProgress progressCallback, Point? resolution, ProgramMapTableParser programMapTableParser, int pid, TransportStreamParser tsParser)
|
||||
{
|
||||
var programMapTableParser = new ProgramMapTableParser();
|
||||
programMapTableParser.Parse(fileName); // get languages from PMT if possible
|
||||
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");
|
||||
|
||||
var overrideScreenSize = Configuration.Settings.Tools.BatchConvertTsOverrideScreenSize &&
|
||||
Configuration.Settings.Tools.BatchConvertTsScreenHeight > 0 &&
|
||||
Configuration.Settings.Tools.BatchConvertTsScreenWidth > 0 ||
|
||||
@ -33,107 +21,99 @@ 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, null, fileName);
|
||||
foreach (int pid in tsParser.SubtitlePacketIds)
|
||||
|
||||
|
||||
var language = GetFileNameEnding(programMapTableParser, pid);
|
||||
var outputFileName = CommandLineConverter.FormatOutputFileNameForBatchConvert(Utilities.GetPathAndFileNameWithoutExtension(fileName) + language + Path.GetExtension(fileName), ".sup", outputFolder, overwrite);
|
||||
stdOutWriter?.Write($"{count}: {Path.GetFileName(fileName)} -> PID {pid} to {outputFileName}...");
|
||||
var sub = tsParser.GetDvbSubtitles(pid);
|
||||
progressCallback?.Invoke($"Save PID {pid}");
|
||||
var subtitleScreenSize = GetSubtitleScreenSize(sub, overrideScreenSize, resolution);
|
||||
using (var binarySubtitleFile = new FileStream(outputFileName, FileMode.Create))
|
||||
{
|
||||
var language = GetFileNameEnding(programMapTableParser, pid);
|
||||
var outputFileName = CommandLineConverter.FormatOutputFileNameForBatchConvert(Utilities.GetPathAndFileNameWithoutExtension(fileName) + language + Path.GetExtension(fileName), ".sup", outputFolder, overwrite);
|
||||
stdOutWriter?.Write($"{count}: {Path.GetFileName(fileName)} -> PID {pid} to {outputFileName}...");
|
||||
var sub = tsParser.GetDvbSubtitles(pid);
|
||||
progressCallback?.Invoke($"Save PID {pid}");
|
||||
var subtitleScreenSize = GetSubtitleScreenSize(sub, overrideScreenSize, resolution);
|
||||
using (var binarySubtitleFile = new FileStream(outputFileName, FileMode.Create))
|
||||
for (int index = 0; index < sub.Count; index++)
|
||||
{
|
||||
for (int index = 0; index < sub.Count; index++)
|
||||
var p = sub[index];
|
||||
var pos = p.GetPosition();
|
||||
var bmp = sub[index].GetBitmap();
|
||||
var tsWidth = bmp.Width;
|
||||
var tsHeight = bmp.Height;
|
||||
var nBmp = new NikseBitmap(bmp);
|
||||
pos.Top += nBmp.CropTopTransparent(0);
|
||||
pos.Left += nBmp.CropSidesAndBottom(0, Color.FromArgb(0, 0, 0, 0), true);
|
||||
bmp.Dispose();
|
||||
bmp = nBmp.GetBitmap();
|
||||
var mp = form.MakeMakeBitmapParameter(index, subtitleScreenSize.X, subtitleScreenSize.Y);
|
||||
|
||||
if (overrideScreenSize)
|
||||
{
|
||||
var p = sub[index];
|
||||
var pos = p.GetPosition();
|
||||
var bmp = sub[index].GetBitmap();
|
||||
var tsWidth = bmp.Width;
|
||||
var tsHeight = bmp.Height;
|
||||
var nBmp = new NikseBitmap(bmp);
|
||||
pos.Top += nBmp.CropTopTransparent(0);
|
||||
pos.Left += nBmp.CropSidesAndBottom(0, Color.FromArgb(0, 0, 0, 0), true);
|
||||
var widthFactor = (double)subtitleScreenSize.X / tsWidth;
|
||||
var heightFactor = (double)subtitleScreenSize.Y / tsHeight;
|
||||
var resizeBmp = ResizeBitmap(bmp, (int)Math.Round(bmp.Width * widthFactor), (int)Math.Round(bmp.Height * heightFactor));
|
||||
bmp.Dispose();
|
||||
bmp = nBmp.GetBitmap();
|
||||
var mp = form.MakeMakeBitmapParameter(index, subtitleScreenSize.X, subtitleScreenSize.Y);
|
||||
bmp = resizeBmp;
|
||||
pos.Left = (int)Math.Round(pos.Left * widthFactor);
|
||||
pos.Top = (int)Math.Round(pos.Top * heightFactor);
|
||||
progressCallback?.Invoke($"Save PID {pid}: {(index + 1) * 100 / sub.Count}%");
|
||||
}
|
||||
|
||||
if (overrideScreenSize)
|
||||
mp.Bitmap = bmp;
|
||||
mp.P = new Paragraph(string.Empty, p.StartMilliseconds, p.EndMilliseconds);
|
||||
mp.ScreenWidth = subtitleScreenSize.X;
|
||||
mp.ScreenHeight = subtitleScreenSize.Y;
|
||||
if (Configuration.Settings.Tools.BatchConvertTsOverrideXPosition || Configuration.Settings.Tools.BatchConvertTsOverrideYPosition)
|
||||
{
|
||||
var overrideMarginX = (int)Math.Round(Configuration.Settings.Tools.BatchConvertTsOverrideHMargin * subtitleScreenSize.X / 100.0);
|
||||
var overrideMarginY = (int)Math.Round(Configuration.Settings.Tools.BatchConvertTsOverrideBottomMargin * subtitleScreenSize.Y / 100.0);
|
||||
if (Configuration.Settings.Tools.BatchConvertTsOverrideXPosition && Configuration.Settings.Tools.BatchConvertTsOverrideYPosition)
|
||||
{
|
||||
var widthFactor = (double)subtitleScreenSize.X / tsWidth;
|
||||
var heightFactor = (double)subtitleScreenSize.Y / tsHeight;
|
||||
var resizeBmp = ResizeBitmap(bmp, (int)Math.Round(bmp.Width * widthFactor), (int)Math.Round(bmp.Height * heightFactor));
|
||||
bmp.Dispose();
|
||||
bmp = resizeBmp;
|
||||
pos.Left = (int)Math.Round(pos.Left * widthFactor);
|
||||
pos.Top = (int)Math.Round(pos.Top * heightFactor);
|
||||
progressCallback?.Invoke($"Save PID {pid}: {(index + 1) * 100 / sub.Count}%");
|
||||
var x = (int)Math.Round(subtitleScreenSize.X / 2.0 - mp.Bitmap.Width / 2.0);
|
||||
if (Configuration.Settings.Tools.BatchConvertTsOverrideHAlign.Equals("left", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
x = overrideMarginX;
|
||||
}
|
||||
else if (Configuration.Settings.Tools.BatchConvertTsOverrideHAlign.Equals("right", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
x = subtitleScreenSize.X - overrideMarginX - mp.Bitmap.Width;
|
||||
}
|
||||
|
||||
var y = subtitleScreenSize.Y - overrideMarginY - mp.Bitmap.Height;
|
||||
mp.OverridePosition = new Point(x, y);
|
||||
}
|
||||
|
||||
mp.Bitmap = bmp;
|
||||
mp.P = new Paragraph(string.Empty, p.StartMilliseconds, p.EndMilliseconds);
|
||||
mp.ScreenWidth = subtitleScreenSize.X;
|
||||
mp.ScreenHeight = subtitleScreenSize.Y;
|
||||
if (Configuration.Settings.Tools.BatchConvertTsOverrideXPosition || Configuration.Settings.Tools.BatchConvertTsOverrideYPosition)
|
||||
else if (Configuration.Settings.Tools.BatchConvertTsOverrideXPosition)
|
||||
{
|
||||
var overrideMarginX = (int)Math.Round(Configuration.Settings.Tools.BatchConvertTsOverrideHMargin * subtitleScreenSize.X / 100.0);
|
||||
var overrideMarginY = (int)Math.Round(Configuration.Settings.Tools.BatchConvertTsOverrideBottomMargin * subtitleScreenSize.Y / 100.0);
|
||||
if (Configuration.Settings.Tools.BatchConvertTsOverrideXPosition && Configuration.Settings.Tools.BatchConvertTsOverrideYPosition)
|
||||
var x = (int)Math.Round(subtitleScreenSize.X / 2.0 - mp.Bitmap.Width / 2.0);
|
||||
if (Configuration.Settings.Tools.BatchConvertTsOverrideHAlign.Equals("left", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
var x = (int)Math.Round(subtitleScreenSize.X / 2.0 - mp.Bitmap.Width / 2.0);
|
||||
if (Configuration.Settings.Tools.BatchConvertTsOverrideHAlign.Equals("left", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
x = overrideMarginX;
|
||||
}
|
||||
else if (Configuration.Settings.Tools.BatchConvertTsOverrideHAlign.Equals("right", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
x = subtitleScreenSize.X - overrideMarginX - mp.Bitmap.Width;
|
||||
}
|
||||
var y = subtitleScreenSize.Y - overrideMarginY - mp.Bitmap.Height;
|
||||
mp.OverridePosition = new Point(x, y);
|
||||
x = overrideMarginX;
|
||||
}
|
||||
else if (Configuration.Settings.Tools.BatchConvertTsOverrideXPosition)
|
||||
else if (Configuration.Settings.Tools.BatchConvertTsOverrideHAlign.Equals("right", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
var x = (int)Math.Round(subtitleScreenSize.X / 2.0 - mp.Bitmap.Width / 2.0);
|
||||
if (Configuration.Settings.Tools.BatchConvertTsOverrideHAlign.Equals("left", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
x = overrideMarginX;
|
||||
}
|
||||
else if (Configuration.Settings.Tools.BatchConvertTsOverrideHAlign.Equals("right", StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
x = subtitleScreenSize.X - overrideMarginX - mp.Bitmap.Width;
|
||||
}
|
||||
mp.OverridePosition = new Point(x, pos.Top);
|
||||
}
|
||||
else
|
||||
{
|
||||
var y = subtitleScreenSize.Y - overrideMarginY - mp.Bitmap.Height;
|
||||
mp.OverridePosition = new Point(pos.Left, y);
|
||||
x = subtitleScreenSize.X - overrideMarginX - mp.Bitmap.Width;
|
||||
}
|
||||
|
||||
mp.OverridePosition = new Point(x, pos.Top);
|
||||
}
|
||||
else
|
||||
{
|
||||
mp.OverridePosition = new Point(pos.Left, pos.Top); // use original position (can be scaled)
|
||||
}
|
||||
ExportPngXml.MakeBluRaySupImage(mp);
|
||||
binarySubtitleFile.Write(mp.Buffer, 0, mp.Buffer.Length);
|
||||
if (mp.Bitmap != null)
|
||||
{
|
||||
mp.Bitmap.Dispose();
|
||||
mp.Bitmap = null;
|
||||
var y = subtitleScreenSize.Y - overrideMarginY - mp.Bitmap.Height;
|
||||
mp.OverridePosition = new Point(pos.Left, y);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
mp.OverridePosition = new Point(pos.Left, pos.Top); // use original position (can be scaled)
|
||||
}
|
||||
|
||||
ExportPngXml.MakeBluRaySupImage(mp);
|
||||
binarySubtitleFile.Write(mp.Buffer, 0, mp.Buffer.Length);
|
||||
mp.Bitmap?.Dispose();
|
||||
mp.Bitmap = null;
|
||||
}
|
||||
stdOutWriter?.WriteLine(" done.");
|
||||
}
|
||||
}
|
||||
return true;
|
||||
stdOutWriter?.WriteLine(" done.");
|
||||
}
|
||||
|
||||
public static string GetFileNameEnding(ProgramMapTableParser pmt, int pid)
|
||||
|
Loading…
Reference in New Issue
Block a user