Calc sane font size for video with burned in sub

This commit is contained in:
niksedk 2021-06-30 16:59:57 +02:00
parent 6bac1bae5e
commit 2197aa19df
4 changed files with 101 additions and 8 deletions

View File

@ -14,6 +14,7 @@
* Add a few profiles for Amazon Prime
* Add new FCE rule: "Remove first dash in non-dialogs"
* Add "Video - Generate blank video" - thx OmrSi
* Add "Video - Generate video w burned-in sub"
* IMPROVED:
* Update Bulgarian translation - thx Калин
* Update Chinese translation - thx nkh0472

View File

@ -34,15 +34,18 @@ namespace Nikse.SubtitleEdit.Forms
this.buttonCancel = new System.Windows.Forms.Button();
this.labelPleaseWait = new System.Windows.Forms.Label();
this.labelInfo = new System.Windows.Forms.Label();
this.numericUpDownFontSize = new System.Windows.Forms.NumericUpDown();
this.labelFontSize = new System.Windows.Forms.Label();
((System.ComponentModel.ISupportInitialize)(this.numericUpDownFontSize)).BeginInit();
this.SuspendLayout();
//
// progressBar1
//
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(12, 108);
this.progressBar1.Location = new System.Drawing.Point(12, 135);
this.progressBar1.Name = "progressBar1";
this.progressBar1.Size = new System.Drawing.Size(278, 23);
this.progressBar1.Size = new System.Drawing.Size(405, 23);
this.progressBar1.TabIndex = 22;
this.progressBar1.Visible = false;
//
@ -50,7 +53,7 @@ namespace Nikse.SubtitleEdit.Forms
//
this.buttonOK.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonOK.ImeMode = System.Windows.Forms.ImeMode.NoControl;
this.buttonOK.Location = new System.Drawing.Point(296, 108);
this.buttonOK.Location = new System.Drawing.Point(423, 135);
this.buttonOK.Name = "buttonOK";
this.buttonOK.Size = new System.Drawing.Size(121, 23);
this.buttonOK.TabIndex = 23;
@ -63,7 +66,7 @@ namespace Nikse.SubtitleEdit.Forms
this.buttonCancel.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
this.buttonCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
this.buttonCancel.ImeMode = System.Windows.Forms.ImeMode.NoControl;
this.buttonCancel.Location = new System.Drawing.Point(423, 108);
this.buttonCancel.Location = new System.Drawing.Point(550, 135);
this.buttonCancel.Name = "buttonCancel";
this.buttonCancel.Size = new System.Drawing.Size(75, 23);
this.buttonCancel.TabIndex = 24;
@ -75,7 +78,7 @@ namespace Nikse.SubtitleEdit.Forms
//
this.labelPleaseWait.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
this.labelPleaseWait.AutoSize = true;
this.labelPleaseWait.Location = new System.Drawing.Point(12, 92);
this.labelPleaseWait.Location = new System.Drawing.Point(12, 119);
this.labelPleaseWait.Name = "labelPleaseWait";
this.labelPleaseWait.Size = new System.Drawing.Size(70, 13);
this.labelPleaseWait.TabIndex = 25;
@ -90,11 +93,44 @@ namespace Nikse.SubtitleEdit.Forms
this.labelInfo.TabIndex = 26;
this.labelInfo.Text = "This will save video x with burned-in subtitle.";
//
// numericUpDownFontSize
//
this.numericUpDownFontSize.Location = new System.Drawing.Point(472, 40);
this.numericUpDownFontSize.Maximum = new decimal(new int[] {
1000,
0,
0,
0});
this.numericUpDownFontSize.Minimum = new decimal(new int[] {
3,
0,
0,
0});
this.numericUpDownFontSize.Name = "numericUpDownFontSize";
this.numericUpDownFontSize.Size = new System.Drawing.Size(72, 20);
this.numericUpDownFontSize.TabIndex = 27;
this.numericUpDownFontSize.Value = new decimal(new int[] {
50,
0,
0,
0});
//
// labelFontSize
//
this.labelFontSize.AutoSize = true;
this.labelFontSize.Location = new System.Drawing.Point(472, 21);
this.labelFontSize.Name = "labelFontSize";
this.labelFontSize.Size = new System.Drawing.Size(49, 13);
this.labelFontSize.TabIndex = 28;
this.labelFontSize.Text = "Font size";
//
// GenerateVideoWithHardSubs
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(510, 143);
this.ClientSize = new System.Drawing.Size(637, 170);
this.Controls.Add(this.labelFontSize);
this.Controls.Add(this.numericUpDownFontSize);
this.Controls.Add(this.labelInfo);
this.Controls.Add(this.labelPleaseWait);
this.Controls.Add(this.progressBar1);
@ -108,6 +144,7 @@ namespace Nikse.SubtitleEdit.Forms
this.ShowIcon = false;
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
this.Text = "GenerateVideoWithHardSubs";
((System.ComponentModel.ISupportInitialize)(this.numericUpDownFontSize)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();
@ -120,5 +157,7 @@ namespace Nikse.SubtitleEdit.Forms
private System.Windows.Forms.Button buttonCancel;
private System.Windows.Forms.Label labelPleaseWait;
private System.Windows.Forms.Label labelInfo;
private System.Windows.Forms.NumericUpDown numericUpDownFontSize;
private System.Windows.Forms.Label labelFontSize;
}
}

View File

@ -14,7 +14,7 @@ namespace Nikse.SubtitleEdit.Forms
private string _inputVideoFileName;
public string VideoFileName { get; private set; }
public GenerateVideoWithHardSubs(Subtitle assaSubtitle, string inputVideoFileName)
public GenerateVideoWithHardSubs(Subtitle assaSubtitle, string inputVideoFileName, int? fontSize)
{
UiUtil.PreInitialize(this);
InitializeComponent();
@ -26,9 +26,20 @@ namespace Nikse.SubtitleEdit.Forms
_inputVideoFileName = inputVideoFileName;
buttonOK.Text = LanguageSettings.Current.Watermark.Generate;
labelPleaseWait.Text = LanguageSettings.Current.General.PleaseWait;
labelFontSize.Text = LanguageSettings.Current.ExportPngXml.FontSize;
buttonCancel.Text = LanguageSettings.Current.General.Cancel;
progressBar1.Visible = false;
labelPleaseWait.Visible = false;
if (fontSize.HasValue)
{
numericUpDownFontSize.Value = fontSize.Value;
}
else
{
labelFontSize.Visible = false;
numericUpDownFontSize.Visible = false;
}
}
private void buttonCancel_Click(object sender, EventArgs e)
@ -58,6 +69,15 @@ namespace Nikse.SubtitleEdit.Forms
File.Delete(VideoFileName);
}
if (numericUpDownFontSize.Visible)
{
var fontSize = (int)numericUpDownFontSize.Value;
var style = AdvancedSubStationAlpha.GetSsaStyle("Default", _assaSubtitle.Header);
style.FontSize = fontSize;
var styleLine = style.ToRawAss();
_assaSubtitle.Header = AdvancedSubStationAlpha.AddTagToHeader("Style", styleLine, "[V4+ Styles]", _assaSubtitle.Header);
}
SubtitleFormat format = new AdvancedSubStationAlpha();
var assaTempFileName = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString() + ".ass");
File.WriteAllText(assaTempFileName, format.ToText(_assaSubtitle, null));

View File

@ -31425,15 +31425,21 @@ namespace Nikse.SubtitleEdit.Forms
}
var sub = new Subtitle(_subtitle, false);
int? fontSize = null;
if (string.IsNullOrEmpty(sub.Header))
{
sub.Header = AdvancedSubStationAlpha.DefaultHeader;
var style = AdvancedSubStationAlpha.GetSsaStyle("Default", sub.Header);
style.FontSize = GetOptimalSubtitleFontSize(_videoInfo.Height);
fontSize = (int)style.FontSize;
var styleLine = style.ToRawAss();
sub.Header = AdvancedSubStationAlpha.AddTagToHeader("Style", styleLine, "[V4+ Styles]", sub.Header);
}
sub.Header = AdvancedSubStationAlpha.AddTagToHeader("PlayResX", "PlayResX: " + _videoInfo.Width.ToString(CultureInfo.InvariantCulture), "[Script Info]", sub.Header);
sub.Header = AdvancedSubStationAlpha.AddTagToHeader("PlayResY", "PlayResY: " + _videoInfo.Height.ToString(CultureInfo.InvariantCulture), "[Script Info]", sub.Header);
using (var form = new GenerateVideoWithHardSubs(sub, VideoFileName))
using (var form = new GenerateVideoWithHardSubs(sub, VideoFileName, fontSize))
{
var result = form.ShowDialog(this);
if (result != DialogResult.OK)
@ -31447,5 +31453,32 @@ namespace Nikse.SubtitleEdit.Forms
}
}
}
private int GetOptimalSubtitleFontSize(int height)
{
var wantedHeight = height * 0.08; // let the optimal height be 8% of video height
var currentSize = 50;
using (var graphics = CreateGraphics())
{
for (int i = 40; i > -10; i--)
{
using (var font = new Font(Font.FontFamily, (float)currentSize, FontStyle.Regular))
{
var currentHeight = graphics.MeasureString("HJKLj", font).Height;
if (currentHeight > wantedHeight)
{
currentSize -= Math.Max(1, i);
}
else if (currentHeight < wantedHeight)
{
currentSize += Math.Max(1, i);
}
}
}
}
return currentSize;
}
}
}