Skip to content

Commit 49f5209

Browse files
authored
Adding support for modifying maximum sentence length when outputting to .srt via CLI argument (#14)
1 parent f0b1fd6 commit 49f5209

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

Sources/yap/OutputFormat.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ enum OutputFormat: String, EnumerableFlag {
1515
}
1616
}
1717

18-
func text(for transcript: AttributedString) -> String {
18+
func text(for transcript: AttributedString, maxLength: Int) -> String {
1919
switch self {
2020
case .txt:
2121
return String(transcript.characters)
@@ -28,7 +28,7 @@ enum OutputFormat: String, EnumerableFlag {
2828
return String(format: "%0.2d:%0.2d:%0.2d,%0.3d", h, m, s, ms)
2929
}
3030

31-
return transcript.sentences(maxLength: 40).compactMap { (sentence: AttributedString) -> (CMTimeRange, String)? in
31+
return transcript.sentences(maxLength: maxLength).compactMap { (sentence: AttributedString) -> (CMTimeRange, String)? in
3232
guard let timeRange = sentence.audioTimeRange else { return nil }
3333
return (timeRange, String(sentence.characters))
3434
}.enumerated().map { index, run in

Sources/yap/Transcribe.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ import Speech
3131
transform: URL.init(fileURLWithPath:)
3232
) var outputFile: URL?
3333

34+
@Option(
35+
name: .shortAndLong,
36+
help: "Maximum sentence length in characters. If not provided, it will be set to 40.",
37+
) var maxLength: Int = 40
38+
3439
mutating func run() async throws {
3540
let piped = isatty(STDOUT_FILENO) == 0
3641
struct DevNull: StandardPipelining { func write(content _: String) {} }
@@ -112,7 +117,7 @@ import Speech
112117
}
113118

114119
if let outputFile {
115-
try outputFormat.text(for: transcript).write(
120+
try outputFormat.text(for: transcript, maxLength: maxLength).write(
116121
to: outputFile,
117122
atomically: false,
118123
encoding: .utf8
@@ -121,7 +126,7 @@ import Speech
121126
}
122127

123128
if piped || outputFile == nil {
124-
print(outputFormat.text(for: transcript))
129+
print(outputFormat.text(for: transcript, maxLength: maxLength))
125130
}
126131
}
127132
}

0 commit comments

Comments
 (0)