User Tools

Site Tools


subtitles_in_markdown

This is an old revision of the document!


Subtitle Technical Spec: Markdown-Like Parser Reference

This page defines a strict syntax specification for a custom Markdown-like subtitle source format. It is designed so you can write subtitles in plain text, then reliably parse and convert them to SRT, WebVTT, ASS, or other formats.

Technical Overview

Format name: MDSub Custom (inspired by mdsub and similar tools)
Purpose: Human-readable subtitle authoring with deterministic parsing
Target outputs: SRT, WebVTT, ASS/SSA
Parser requirements: Line-by-line regex matching + timestamp validation

Strict Syntax Rules

Every subtitle file MUST follow these exact rules:

  • Lines are processed sequentially — no lookahead beyond current line
  • Timestamps use ISO-like format: HH:MM:SS.mmm –> HH:MM:SS.mmm
  • Each cue = 1 timestamp line + 1-3 text lines
  • Blank lines separate cues
  • Comments use // prefix
  • File must start with optional metadata header

Complete Line Types

Type Pattern Example Parser Action
Metadata // Title: ... // Title: My Video Store as metadata
Metadata // Language: ... // Language: en-US Store as metadata
Comment // ... // Scene 1 starts Ignore for output
Timestamp [HH:MM:SS.mmm –> HH:MM:SS.mmm] [00:01:23.456 –> 00:01:25.789] Start new cue
Speaker [SPEAKER] [NARRATOR] Prefix to next dialogue
Dialogue Plain text This is spoken text. Add to current cue
Emphasis text or _text_ WARNING Convert per output format
Style «position:center» «position:bottom» ASS positioning tag
Empty (blank line) — End current cue

File Structure

// Optional metadata block

[00:00:01.000 --> 00:00:03.500]
[NARRATOR]
Welcome to the show.

[00:00:03.500 --> 00:00:06.000]
This is the first subtitle.

[00:00:06.000 --> 00:00:08.000]
[JANE]
Whispering: Did you hear that?

Timestamp Specification

Format: [HH:MM:SS.mmm –> HH:MM:SS.mmm]

Validation rules:

  • Hours: 00-23
  • Minutes/Seconds: 00-59
  • Milliseconds: 000-999
  • End time must be greater than start time
  • Leading zeros required

Parser regex (PCRE compatible):

^\s*(\d{2}):(\d{2}):(\d{2})\.(\d{3})\s*-->\s*(\d{2}):(\d{2}):(\d{2})\.(\d{3})\s*$

Parser State Machine

State: IDLE

  • Metadata line (// prefix) → Store metadata, stay IDLE
  • Timestamp line → Create new Cue, go to COLLECTING
  • Other → Error

State: COLLECTING

  • Blank line → Finalize cue, go to IDLE
  • Speaker line → Set cue.speaker, stay COLLECTING
  • Dialogue line → Append to cue.text, stay COLLECTING
  • Timestamp → Finalize current cue, create new cue, stay COLLECTING

Output Format Mappings

SRT

1
00:00:01,000 --> 00:00:03,500
[NARRATOR]
Welcome to the show.

WebVTT

WEBVTT

00:00:01.000 --> 00:00:03.500
[NARRATOR]
Welcome to the show.

ASS

[Events]
Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
Dialogue: 0,0:00:01.00,0:00:03.50,Default,,0,0,0,,{\pos(640,360)}[NARRATOR]Welcome to the show.

Markdown Processing Rules

Source SRT WebVTT ASS
**bold** **bold** <b>bold</b> {\b1}bold{\b0}
_italic_ _italic_ <i>italic</i> {\i1}italic{\i0}
[SPEAKER] [SPEAKER] [SPEAKER] {\c&Hffffff&}[SPEAKER]
<<position:center>> ignored ignored {\pos(640,360)}

Complete Parser Pseudocode

function parseMDSub(source) {
  let cues = [];
  let currentCue = null;
  let metadata = {};
 
  for (let line of source.split('\n')) {
    line = line.trim();
 
    if (line.startsWith('//')) {
      parseMetadata(line, metadata);
      continue;
    }
 
    if (isTimestampLine(line)) {
      if (currentCue) finalizeCue(currentCue, cues);
      currentCue = newCue(parseTimestamp(line));
      continue;
    }
 
    if (line === '') {
      if (currentCue) finalizeCue(currentCue, cues);
      currentCue = null;
      continue;
    }
 
    if (currentCue) {
      if (isSpeakerLine(line)) {
        currentCue.speaker = extractSpeaker(line);
      } else {
        currentCue.text.push(processMarkdown(line));
      }
    }
  }
 
  if (currentCue) finalizeCue(currentCue, cues);
  return { metadata, cues };
}

Validation Checklist

Before conversion, validate:

  • All timestamps parse correctly
  • End time > start time for each cue
  • No overlapping timestamps
  • Maximum 2 lines per cue (SRT limit)
  • Maximum 42 characters per line (SRT recommendation)
  • Consistent language metadata

Error Conditions

Error Message Recovery
Invalid timestamp Line X: malformed timestamp Skip line
Overlapping cues Cue X overlaps with Y Use earliest end time
Empty cue text Timestamp without text Skip cue
Unclosed cue File ends mid-cue Finalize last cue

Implementation Notes

Language: JavaScript/Node.js, Python 3, or Bash
Dependencies: None (pure regex + string processing)
Runtime: O(n) where n = lines
File size limit: 10MB source → ~1M cues
Output validation: Test generated SRT/VTT in VLC, browser

(Add your own scripts here)

  • Python converter: scripts/mdsub2srt.py
  • Node.js version: scripts/mdsub-parser.js
  • Test files: tests/sample.mdsub

Quick Implementation Template

#!/usr/bin/env python3
import re
import sys
 
TIMESTAMP_RE = re.compile(r'^\[\s*(\d{2}):(\d{2}):(\d{2})\.(\d{3})\s*-->\s*(\d{2}):(\d{2}):(\d{2})\.(\d{3})\s*\]$')
 
def parse_mdsub(filename):
    # Implementation here following spec above
    pass
 
if __name__ == "__main__":
    cues = parse_mdsub(sys.argv[1])
    print_srt(cues)

Final Notes

This spec is complete and deterministic. Any implementation following these rules will produce identical output from the same source file. Test thoroughly with edge cases before production use.

subtitles_in_markdown.1777143963.txt.gz · Last modified: by smickster

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki