1
1
import { DeepgramConverter , IConverter , isConverter } from "./converters" ;
2
- import { secondsToTimestamp } from "./lib/helpers" ;
2
+ import { chunkArray , secondsToTimestamp } from "./lib/helpers" ;
3
3
import { DeepgramResponse } from "./lib/types" ;
4
4
5
5
const parseInput = ( transcription : any ) : IConverter => {
@@ -26,12 +26,19 @@ const webvtt = (transcription: any, lineLength: number = 8): string => {
26
26
// get the lines
27
27
const lines = data . getLines ( lineLength ) ;
28
28
29
+ // is speaker output required?
30
+ const speakerLabels = "speaker" in lines [ 0 ] [ 0 ] ;
31
+
29
32
lines . forEach ( ( words ) => {
30
33
const firstWord = words [ 0 ] ;
31
34
const lastWord = words [ words . length - 1 ] ;
32
35
33
36
output . push ( `${ secondsToTimestamp ( firstWord . start ) } --> ${ secondsToTimestamp ( lastWord . end ) } ` ) ;
34
- output . push ( words . map ( ( word ) => word . punctuated_word ?? word . word ) . join ( " " ) ) ;
37
+
38
+ const line = words . map ( ( word ) => word . punctuated_word ?? word . word ) . join ( " " ) ;
39
+ const speakerLabel = speakerLabels ? `<v Speaker ${ firstWord . speaker } >` : "" ;
40
+
41
+ output . push ( `${ speakerLabel } ${ line } ` ) ;
35
42
output . push ( "" ) ;
36
43
} ) ;
37
44
@@ -41,9 +48,16 @@ const webvtt = (transcription: any, lineLength: number = 8): string => {
41
48
const srt = ( transcription : any , lineLength : number = 8 ) : string => {
42
49
const output : string [ ] = [ ] ;
43
50
51
+ const data = parseInput ( transcription ) ;
52
+
44
53
// get the lines
45
- const lines = parseInput ( transcription ) . getLines ( lineLength ) ;
54
+ let lines = data . getLines ( lineLength ) ;
55
+
56
+ // is speaker output required?
57
+ const speakerLabels = "speaker" in lines [ 0 ] [ 0 ] ;
58
+
46
59
let entry = 1 ;
60
+ let currentSpeaker : any ;
47
61
48
62
lines . forEach ( ( words ) => {
49
63
output . push ( ( entry ++ ) . toString ( ) ) ;
@@ -57,8 +71,17 @@ const srt = (transcription: any, lineLength: number = 8): string => {
57
71
"HH:mm:ss,SSS"
58
72
) } `
59
73
) ;
60
- output . push ( words . map ( ( word ) => word . punctuated_word ?? word . word ) . join ( " " ) ) ;
74
+
75
+ const line = words . map ( ( word ) => word . punctuated_word ?? word . word ) . join ( " " ) ;
76
+ const speakerLabel =
77
+ speakerLabels && currentSpeaker !== firstWord . speaker
78
+ ? `[Speaker ${ firstWord . speaker } ]\n`
79
+ : "" ;
80
+
81
+ output . push ( `${ speakerLabel } ${ line } ` ) ;
61
82
output . push ( "" ) ;
83
+
84
+ currentSpeaker = firstWord . speaker ;
62
85
} ) ;
63
86
64
87
return output . join ( "\n" ) ;
0 commit comments