Allow import time codes w empty subtitle - thx Heung :)

This commit is contained in:
niksedk 2022-05-13 13:59:37 +02:00
parent d96e6db6de
commit f6d2949676
4 changed files with 94 additions and 10 deletions

View File

@ -45,7 +45,7 @@
* Do not reapeat colors in color picker - thx SDH Marven
* Fix cmd line convert issue with ebu stl headerfile - thx Rumczeis
* Fix image export missing error for last two lines - th Antoine
* Fix "Merge short lines" in "Batch Conv" to use UI settings - thx maharaj12
* Fix "Batch Convert" "Merge short lines" UI settings - thx maharaj12
* Fix casing issue in Remove text for HI - thx Petar

View File

@ -16,7 +16,7 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats
public override string Name => "MacCaption 1.0";
// ANC data bytes may be represented by one ASCII character according to the following schema:
private static Dictionary<char, string> _ancDictionary = new Dictionary<char, string>
private static readonly Dictionary<char, string> AncDictionary = new Dictionary<char, string>
{
{ 'G', "FA0000" },
{ 'H', "FA0000FA0000" },
@ -240,7 +240,7 @@ namespace Nikse.SubtitleEdit.Core.SubtitleFormats
var sb = new StringBuilder();
foreach (var ch in input)
{
if (_ancDictionary.TryGetValue(ch, out var hexValue))
if (AncDictionary.TryGetValue(ch, out var hexValue))
{
sb.Append(hexValue);
}

View File

@ -0,0 +1,73 @@
using Nikse.SubtitleEdit.Core.Common;
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
namespace Nikse.SubtitleEdit.Core.SubtitleFormats
{
public class TimeCodesOnly3 : SubtitleFormat
{
// 01:00:00:15
private static readonly Regex RegexTimeCodes = new Regex(@"^\d+:\d\d:\d\d:\d\d$", RegexOptions.Compiled);
public override string Extension => ".txt";
public const string NameOfFormat = "Time Codes Only 3";
public override string Name => NameOfFormat;
public override string ToText(Subtitle subtitle, string title)
{
var sb = new StringBuilder();
foreach (var p in subtitle.Paragraphs)
{
sb.AppendLine($"{EncodeTimeCode(p.StartTime)}");
}
return sb.ToString();
}
private static string EncodeTimeCode(TimeCode time)
{
return $"{time.Hours}:{time.Minutes:00}:{time.Seconds:00}:{MillisecondsToFramesMaxFrameRate(time.Milliseconds):00}";
}
public override void LoadSubtitle(Subtitle subtitle, List<string> lines, string fileName)
{
Paragraph p = null;
subtitle.Paragraphs.Clear();
_errorCount = 0;
foreach (var line in lines)
{
if (RegexTimeCodes.IsMatch(line))
{
var startParts = line.Split(SplitCharColon, StringSplitOptions.RemoveEmptyEntries);
if (startParts.Length == 4)
{
p = new Paragraph(DecodeTimeCodeFramesFourParts(startParts), DecodeTimeCodeFramesFourParts(startParts), string.Empty);
p.EndTime.TotalMilliseconds += Configuration.Settings.General.NewEmptyDefaultMs;
subtitle.Paragraphs.Add(p);
var prev = subtitle.GetParagraphOrDefault(subtitle.GetIndex(p) - 1);
if (prev != null)
{
if (prev.EndTime.TotalMilliseconds > p.StartTime.TotalMilliseconds)
{
prev.EndTime.TotalMilliseconds = p.StartTime.TotalMilliseconds - Configuration.Settings.General.MinimumMillisecondsBetweenLines;
}
}
}
}
else if (p != null)
{
_errorCount++;
}
}
subtitle.Renumber();
}
}
}

View File

@ -20946,12 +20946,6 @@ namespace Nikse.SubtitleEdit.Forms
private void toolStripMenuItemImportTimeCodes_Click(object sender, EventArgs e)
{
if (_subtitle.Paragraphs.Count < 1)
{
DisplaySubtitleNotLoadedMessage();
return;
}
openFileDialog1.Title = _languageGeneral.OpenSubtitle;
openFileDialog1.FileName = string.Empty;
openFileDialog1.Filter = UiUtil.SubtitleExtensionFilter.Value;
@ -20987,7 +20981,8 @@ namespace Nikse.SubtitleEdit.Forms
var formats = SubtitleFormat.GetBinaryFormats(true).Union(SubtitleFormat.GetTextOtherFormats()).Union(new SubtitleFormat[]
{
new TimeCodesOnly1(),
new TimeCodesOnly2()
new TimeCodesOnly2(),
new TimeCodesOnly3(),
}).ToArray();
format = SubtitleFormat.LoadSubtitleFromFile(formats, openFileDialog1.FileName, timeCodeSubtitle);
}
@ -20998,6 +20993,22 @@ namespace Nikse.SubtitleEdit.Forms
return;
}
if (_subtitle.Paragraphs.Count < 1)
{
foreach (var p in timeCodeSubtitle.Paragraphs)
{
p.Text = string.Empty;
}
_subtitle.Paragraphs.AddRange(timeCodeSubtitle.Paragraphs);
_converted = true;
_fileName = string.Empty;
_subtitleListViewIndex = -1;
ShowStatus(string.Format(_language.LoadedSubtitleX, openFileDialog1.FileName));
SubtitleListview1.Fill(_subtitle, _subtitleOriginal);
RestoreSubtitleListviewIndices();
}
if (timeCodeSubtitle.Paragraphs.Count != _subtitle.Paragraphs.Count)
{
var text = string.Format(_language.ImportTimeCodesDifferentNumberOfLinesWarning, timeCodeSubtitle.Paragraphs.Count, _subtitle.Paragraphs.Count);