toSrt method

String toSrt({
  1. Duration length = const Duration(seconds: 4),
})

Notes as an SRT subtitle file (handy to replay notes as captions).

Implementation

String toSrt({Duration length = const Duration(seconds: 4)}) {
  String stamp(Duration value) {
    String two(int n) => n.toString().padLeft(2, "0");
    return "${two(value.inHours)}:${two(value.inMinutes.remainder(60))}:${two(value.inSeconds.remainder(60))},${value.inMilliseconds.remainder(1000).toString().padLeft(3, "0")}";
  }

  final StringBuffer buffer = StringBuffer();
  for (int i = 0; i < _notes.length; i++) {
    final UMediaNote note = _notes[i];
    buffer
      ..writeln(i + 1)
      ..writeln("${stamp(note.position)} --> ${stamp(note.end ?? note.position + length)}")
      ..writeln(note.text)
      ..writeln();
  }
  return buffer.toString();
}