Spaces:
Sleeping
Sleeping
ref #68, #79 : fix segment time output
Browse files- main.cpp +8 -3
- whisper.cpp +2 -2
main.cpp
CHANGED
|
@@ -246,7 +246,7 @@ bool output_vtt(struct whisper_context * ctx, const char * fname) {
|
|
| 246 |
return true;
|
| 247 |
}
|
| 248 |
|
| 249 |
-
bool output_srt(struct whisper_context * ctx, const char * fname) {
|
| 250 |
std::ofstream fout(fname);
|
| 251 |
if (!fout.is_open()) {
|
| 252 |
fprintf(stderr, "%s: failed to open '%s' for writing\n", __func__, fname);
|
|
@@ -258,7 +258,12 @@ bool output_srt(struct whisper_context * ctx, const char * fname) {
|
|
| 258 |
const int n_segments = whisper_full_n_segments(ctx);
|
| 259 |
for (int i = 0; i < n_segments; ++i) {
|
| 260 |
const char * text = whisper_full_get_segment_text(ctx, i);
|
| 261 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 262 |
}
|
| 263 |
|
| 264 |
return true;
|
|
@@ -394,7 +399,7 @@ int main(int argc, char ** argv) {
|
|
| 394 |
// output to SRT file
|
| 395 |
if (params.output_srt) {
|
| 396 |
const auto fname_srt = fname_inp + ".srt";
|
| 397 |
-
output_srt(ctx, fname_srt.c_str());
|
| 398 |
}
|
| 399 |
}
|
| 400 |
}
|
|
|
|
| 246 |
return true;
|
| 247 |
}
|
| 248 |
|
| 249 |
+
bool output_srt(struct whisper_context * ctx, const char * fname, const whisper_params & params) {
|
| 250 |
std::ofstream fout(fname);
|
| 251 |
if (!fout.is_open()) {
|
| 252 |
fprintf(stderr, "%s: failed to open '%s' for writing\n", __func__, fname);
|
|
|
|
| 258 |
const int n_segments = whisper_full_n_segments(ctx);
|
| 259 |
for (int i = 0; i < n_segments; ++i) {
|
| 260 |
const char * text = whisper_full_get_segment_text(ctx, i);
|
| 261 |
+
const int64_t t0 = whisper_full_get_segment_t0(ctx, i);
|
| 262 |
+
const int64_t t1 = whisper_full_get_segment_t1(ctx, i);
|
| 263 |
+
|
| 264 |
+
fout << i + 1 + params.offset_n << "\n";
|
| 265 |
+
fout << to_timestamp(t0) << " --> " << to_timestamp(t1) << "\n";
|
| 266 |
+
fout << text << "\n\n";
|
| 267 |
}
|
| 268 |
|
| 269 |
return true;
|
|
|
|
| 399 |
// output to SRT file
|
| 400 |
if (params.output_srt) {
|
| 401 |
const auto fname_srt = fname_inp + ".srt";
|
| 402 |
+
output_srt(ctx, fname_srt.c_str(), params);
|
| 403 |
}
|
| 404 |
}
|
| 405 |
}
|
whisper.cpp
CHANGED
|
@@ -2526,7 +2526,7 @@ int whisper_full(
|
|
| 2526 |
// store the text from this iteration
|
| 2527 |
if (tokens_cur.size() > 0) {
|
| 2528 |
int i0 = 0;
|
| 2529 |
-
auto t0 = 2*(tokens_cur.front().tid - whisper_token_beg(ctx));
|
| 2530 |
|
| 2531 |
std::string text = "";
|
| 2532 |
|
|
@@ -2540,7 +2540,7 @@ int whisper_full(
|
|
| 2540 |
text += whisper_token_to_str(ctx, tokens_cur[i].id);
|
| 2541 |
}
|
| 2542 |
if (tokens_cur[i].id > whisper_token_beg(ctx)) {
|
| 2543 |
-
const auto t1 = 2*(tokens_cur[i].tid - whisper_token_beg(ctx));
|
| 2544 |
if (!text.empty()) {
|
| 2545 |
if (params.print_realtime) {
|
| 2546 |
if (params.print_timestamps) {
|
|
|
|
| 2526 |
// store the text from this iteration
|
| 2527 |
if (tokens_cur.size() > 0) {
|
| 2528 |
int i0 = 0;
|
| 2529 |
+
auto t0 = seek + 2*(tokens_cur.front().tid - whisper_token_beg(ctx));
|
| 2530 |
|
| 2531 |
std::string text = "";
|
| 2532 |
|
|
|
|
| 2540 |
text += whisper_token_to_str(ctx, tokens_cur[i].id);
|
| 2541 |
}
|
| 2542 |
if (tokens_cur[i].id > whisper_token_beg(ctx)) {
|
| 2543 |
+
const auto t1 = seek + 2*(tokens_cur[i].tid - whisper_token_beg(ctx));
|
| 2544 |
if (!text.empty()) {
|
| 2545 |
if (params.print_realtime) {
|
| 2546 |
if (params.print_timestamps) {
|