Skip to content

Commit 3216005

Browse files
authored
Merge pull request #11701 from SubtitleEdit/feat/seconv-plaintext-options
Add plain text output options to seconv
2 parents d9112a2 + 5718185 commit 3216005

4 files changed

Lines changed: 72 additions & 6 deletions

File tree

src/seconv/Commands/ConvertCommand.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,18 @@ public sealed class Settings : CommandSettings
7474
[Description("Path to a Subtitle Edit custom-format XML file (used with --format customtext)")]
7575
public string? CustomFormat { get; init; }
7676

77+
[CommandOption("--plaintext-merge|--plaintextmerge")]
78+
[Description("Plain text output: merge all subtitles into one space-separated block (no blank lines)")]
79+
public bool PlainTextMerge { get; init; }
80+
81+
[CommandOption("--plaintext-unbreak|--plaintextunbreak")]
82+
[Description("Plain text output: unbreak each subtitle, joining its lines into one")]
83+
public bool PlainTextUnbreak { get; init; }
84+
85+
[CommandOption("--plaintext-no-blank-line|--plaintextnoblankline")]
86+
[Description("Plain text output: do not put a blank line between subtitles (default keeps it)")]
87+
public bool PlainTextNoBlankLine { get; init; }
88+
7789
[CommandOption("--ocr-engine|--ocrengine")]
7890
[Description("OCR engine: tesseract | nocr | binaryocr | ollama | paddle (default: tesseract)")]
7991
public string? OcrEngine { get; init; }
@@ -448,6 +460,9 @@ protected override async Task<int> ExecuteAsync(CommandContext context, Settings
448460
EbuHeaderFile = settings.EbuHeaderFile,
449461
MultipleReplaceFile = settings.MultipleReplace,
450462
CustomFormatFile = settings.CustomFormat,
463+
PlainTextMerge = settings.PlainTextMerge,
464+
PlainTextUnbreak = settings.PlainTextUnbreak,
465+
PlainTextLineBetweenSubtitles = !settings.PlainTextNoBlankLine,
451466
TrackNumbers = ParseTrackNumbers(settings.TrackNumber),
452467
ForcedOnly = settings.ForcedOnly,
453468
OcrEngine = string.IsNullOrWhiteSpace(settings.OcrEngine) ? "tesseract" : settings.OcrEngine,

src/seconv/Core/LibSEIntegration.cs

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -310,26 +310,51 @@ public static void SaveSubtitle(
310310
return;
311311
}
312312

313-
// Plain text output — strip HTML, one paragraph per group separated by blank lines
313+
// Plain text output — strip HTML; optional merge/unbreak and blank-line control
314314
if (formatName.Replace(" ", "").Equals("PlainText", StringComparison.OrdinalIgnoreCase))
315315
{
316316
var outputDirPlain = Path.GetDirectoryName(filePath);
317317
if (!string.IsNullOrEmpty(outputDirPlain) && !Directory.Exists(outputDirPlain))
318318
{
319319
Directory.CreateDirectory(outputDirPlain);
320320
}
321+
322+
var plainEncoding = string.IsNullOrWhiteSpace(encodingName)
323+
? new UTF8Encoding(true)
324+
: GetEncoding(encodingName);
325+
326+
// Merge: collapse every paragraph into a single space-separated block.
327+
if (options?.PlainTextMerge == true)
328+
{
329+
var parts = new List<string>();
330+
foreach (var p in subtitle.Paragraphs)
331+
{
332+
var text = HtmlUtil.RemoveHtmlTags(p.Text, true).Replace(Environment.NewLine, " ").Trim();
333+
if (!string.IsNullOrEmpty(text))
334+
{
335+
parts.Add(text);
336+
}
337+
}
338+
File.WriteAllText(filePath, string.Join(" ", parts), plainEncoding);
339+
return;
340+
}
341+
342+
var unbreak = options?.PlainTextUnbreak == true;
343+
var blankLineBetween = options?.PlainTextLineBetweenSubtitles ?? true;
321344
var sb = new StringBuilder();
322345
foreach (var p in subtitle.Paragraphs)
323346
{
324-
if (sb.Length > 0)
347+
var text = HtmlUtil.RemoveHtmlTags(p.Text, true);
348+
if (unbreak)
349+
{
350+
text = Utilities.UnbreakLine(text.Replace(Environment.NewLine, " "));
351+
}
352+
if (sb.Length > 0 && blankLineBetween)
325353
{
326354
sb.AppendLine();
327355
}
328-
sb.AppendLine(HtmlUtil.RemoveHtmlTags(p.Text, true));
356+
sb.AppendLine(text);
329357
}
330-
var plainEncoding = string.IsNullOrWhiteSpace(encodingName)
331-
? new UTF8Encoding(true)
332-
: GetEncoding(encodingName);
333358
File.WriteAllText(filePath, sb.ToString(), plainEncoding);
334359
return;
335360
}

src/seconv/Core/SubtitleConverter.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -867,6 +867,15 @@ internal record class ConversionOptions
867867
public bool SkipTeletext { get; init; }
868868
public int? TeletextOnlyPage { get; init; }
869869

870+
/// <summary>Plain text output: merge every paragraph into one space-separated block (no blank lines).</summary>
871+
public bool PlainTextMerge { get; init; }
872+
873+
/// <summary>Plain text output: unbreak each paragraph, joining its lines into one.</summary>
874+
public bool PlainTextUnbreak { get; init; }
875+
876+
/// <summary>Plain text output: emit a blank line between consecutive paragraphs. Default true (legacy behaviour).</summary>
877+
public bool PlainTextLineBetweenSubtitles { get; init; } = true;
878+
870879
public bool Quiet { get; init; }
871880
public bool Verbose { get; init; }
872881
}

src/seconv/README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,23 @@ seconv movie.ts fcpimage # DVB-sub → FCP
222222
>
223223
> The full list of supported keys lives in `src/seconv/Core/SeConvSettings.cs`.
224224
225+
### Plain text output (`--format plaintext`)
226+
| Option | Description |
227+
|---|---|
228+
| `--plaintext-merge` | Merge all subtitles into one space-separated block (no blank lines) |
229+
| `--plaintext-unbreak` | Unbreak each subtitle, joining its lines into one |
230+
| `--plaintext-no-blank-line` | Do not put a blank line between subtitles (default keeps it) |
231+
232+
HTML/styling tags are always stripped. `--plaintext-merge` takes precedence over
233+
`--plaintext-unbreak` and `--plaintext-no-blank-line`.
234+
235+
```bash
236+
seconv movie.srt plaintext # one entry per blank-line-separated block
237+
seconv movie.srt plaintext --plaintext-no-blank-line # one entry per line, no blank lines
238+
seconv movie.srt plaintext --plaintext-unbreak # collapse multi-line entries
239+
seconv movie.srt plaintext --plaintext-merge # everything on a single line
240+
```
241+
225242
### Verbosity
226243
| Option | Description |
227244
|---|---|

0 commit comments

Comments
 (0)