1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
|
#include "textWriter.h"
#include "../../ComLightLib/comLightClient.h"
#include <array>
#define WIN32_LEAN_AND_MEAN
#include <pathcch.h>
#include <atlstr.h>
#include <atlfile.h>
#pragma comment(lib, "Pathcch.lib")
namespace
{
HRESULT replaceExtension( CString& path, LPCTSTR inputPath, LPCTSTR ext )
{
path = inputPath;
const size_t len = (size_t)path.GetLength() + 4;
wchar_t* buffer = path.GetBufferSetLength( (int)len );
const HRESULT hr = PathCchRenameExtension( buffer, len, ext );
path.ReleaseBuffer();
return hr;
}
// Abstract base class for text writers
class Writer
{
protected:
CAtlFile file;
virtual HRESULT impl( const Whisper::sSegment* const segments, const size_t length ) = 0;
public:
HRESULT write( Whisper::iContext* context, LPCTSTR audioPath, LPCTSTR ext )
{
CString path;
CHECK( replaceExtension( path, audioPath, ext ) );
CHECK( file.Create( path, GENERIC_WRITE, 0, CREATE_ALWAYS ) );
using namespace Whisper;
const eResultFlags resultFlags = eResultFlags::Timestamps | eResultFlags::Tokens;
ComLight::CComPtr<iTranscribeResult> result;
CHECK( context->getResults( resultFlags, &result ) );
sTranscribeLength len;
CHECK( result->getSize( len ) );
const sSegment* const segments = result->getSegments();
return impl( segments, len.countSegments );
}
};
HRESULT writeUtf8Bom( CAtlFile& file )
{
const std::array<uint8_t, 3> bom = { 0xEF, 0xBB, 0xBF };
return file.Write( bom.data(), 3 );
}
void printTime( CStringA& rdi, Whisper::sTimeSpan time, bool comma )
{
Whisper::sTimeSpanFields fields = time;
const char separator = comma ? ',' : '.';
rdi.AppendFormat( "%02d:%02d:%02d%c%03d",
(int)fields.hours,
(int)fields.minutes,
(int)fields.seconds,
separator,
fields.ticks / 10'000 );
}
void printTimeStamp( CStringA& rdi, Whisper::sTimeSpan ts )
{
Whisper::sTimeSpanFields fields = ts;
uint32_t msec = fields.ticks / 10'000;
uint32_t hr = fields.days * 24 + fields.hours;
uint32_t min = fields.minutes;
uint32_t sec = fields.seconds;
rdi.AppendFormat( "%02d:%02d:%02d.%03d", hr, min, sec, msec );
}
const char* skipBlank( const char* rsi )
{
while( true )
{
const char c = *rsi;
if( c == ' ' || c == '\t' )
{
rsi++;
continue;
}
return rsi;
}
}
inline const char* cstr( const CStringA& s ) { return s; }
HRESULT writeString( CAtlFile& file, const CStringA& line )
{
if( line.GetLength() > 0 )
CHECK( file.Write( cstr( line ), (DWORD)line.GetLength() ) );
return S_OK;
}
// Writer for UTF-8 text files
class TextWriter : public Writer
{
const bool timestamps;
HRESULT impl( const Whisper::sSegment* const segments, const size_t length ) override final
{
CHECK( writeUtf8Bom( file ) );
using namespace Whisper;
CStringA line;
for( size_t i = 0; i < length; i++ )
{
const sSegment& seg = segments[ i ];
if( timestamps )
{
line = "[";
printTimeStamp( line, seg.time.begin );
line += " --> ";
printTimeStamp( line, seg.time.end );
line += "] ";
}
else
line = "";
line += skipBlank( seg.text );
line += "\r\n";
CHECK( writeString( file, line ) );
}
return S_OK;
}
public:
TextWriter( bool tt ) : timestamps( tt ) { }
};
// Writer for SubRip format: https://en.wikipedia.org/wiki/SubRip#SubRip_file_format
class SubRipWriter : public Writer
{
HRESULT impl( const Whisper::sSegment* const segments, const size_t length ) override final
{
CHECK( writeUtf8Bom( file ) );
using namespace Whisper;
CStringA line;
for( size_t i = 0; i < length; i++ )
{
const sSegment& seg = segments[ i ];
line.Format( "%zu\r\n", i + 1 );
printTime( line, seg.time.begin, true );
line += " --> ";
printTime( line, seg.time.end, true );
line += "\r\n";
line += skipBlank( seg.text );
line += "\r\n\r\n";
CHECK( writeString( file, line ) );
}
return S_OK;
}
};
// Writer for WebVTT format: https://en.wikipedia.org/wiki/WebVTT
class VttWriter : public Writer
{
HRESULT impl( const Whisper::sSegment* const segments, const size_t length ) override final
{
CHECK( writeUtf8Bom( file ) );
using namespace Whisper;
CStringA line;
line = "WEBVTT\r\n\r\n";
CHECK( writeString( file, line ) );
for( size_t i = 0; i < length; i++ )
{
const sSegment& seg = segments[ i ];
line = "";
printTime( line, seg.time.begin, false );
line += " --> ";
printTime( line, seg.time.end, false );
line += "\r\n";
line += skipBlank( seg.text );
line += "\r\n\r\n";
CHECK( writeString( file, line ) );
}
return S_OK;
}
};
}
HRESULT writeText( Whisper::iContext* context, LPCTSTR audioPath, bool timestamps )
{
TextWriter writer{ timestamps };
return writer.write( context, audioPath, L".txt" );
}
HRESULT writeSubRip( Whisper::iContext* context, LPCTSTR audioPath )
{
SubRipWriter writer;
return writer.write( context, audioPath, L".srt" );
}
HRESULT writeWebVTT( Whisper::iContext* context, LPCTSTR audioPath )
{
VttWriter writer;
return writer.write( context, audioPath, L".vtt" );
}
|