Spaces:
Running
Running
Laytan Laats
commited on
main : escape quotes in csv output (#815)
Browse files- examples/main/main.cpp +24 -23
examples/main/main.cpp
CHANGED
|
@@ -352,29 +352,6 @@ bool output_srt(struct whisper_context * ctx, const char * fname, const whisper_
|
|
| 352 |
return true;
|
| 353 |
}
|
| 354 |
|
| 355 |
-
bool output_csv(struct whisper_context * ctx, const char * fname) {
|
| 356 |
-
std::ofstream fout(fname);
|
| 357 |
-
if (!fout.is_open()) {
|
| 358 |
-
fprintf(stderr, "%s: failed to open '%s' for writing\n", __func__, fname);
|
| 359 |
-
return false;
|
| 360 |
-
}
|
| 361 |
-
|
| 362 |
-
fprintf(stderr, "%s: saving output to '%s'\n", __func__, fname);
|
| 363 |
-
|
| 364 |
-
const int n_segments = whisper_full_n_segments(ctx);
|
| 365 |
-
fout << "start,end,text\n";
|
| 366 |
-
for (int i = 0; i < n_segments; ++i) {
|
| 367 |
-
const char * text = whisper_full_get_segment_text(ctx, i);
|
| 368 |
-
const int64_t t0 = whisper_full_get_segment_t0(ctx, i);
|
| 369 |
-
const int64_t t1 = whisper_full_get_segment_t1(ctx, i);
|
| 370 |
-
|
| 371 |
-
//need to multiply times returned from whisper_full_get_segment_t{0,1}() by 10 to get milliseconds.
|
| 372 |
-
fout << 10 * t0 << "," << 10 * t1 << ",\"" << text << "\"\n";
|
| 373 |
-
}
|
| 374 |
-
|
| 375 |
-
return true;
|
| 376 |
-
}
|
| 377 |
-
|
| 378 |
char *escape_double_quotes_and_backslashes(const char *str) {
|
| 379 |
if (str == NULL) {
|
| 380 |
return NULL;
|
|
@@ -406,6 +383,30 @@ char *escape_double_quotes_and_backslashes(const char *str) {
|
|
| 406 |
return escaped;
|
| 407 |
}
|
| 408 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 409 |
bool output_json(struct whisper_context * ctx, const char * fname, const whisper_params & params) {
|
| 410 |
std::ofstream fout(fname);
|
| 411 |
int indent = 0;
|
|
|
|
| 352 |
return true;
|
| 353 |
}
|
| 354 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 355 |
char *escape_double_quotes_and_backslashes(const char *str) {
|
| 356 |
if (str == NULL) {
|
| 357 |
return NULL;
|
|
|
|
| 383 |
return escaped;
|
| 384 |
}
|
| 385 |
|
| 386 |
+
bool output_csv(struct whisper_context * ctx, const char * fname) {
|
| 387 |
+
std::ofstream fout(fname);
|
| 388 |
+
if (!fout.is_open()) {
|
| 389 |
+
fprintf(stderr, "%s: failed to open '%s' for writing\n", __func__, fname);
|
| 390 |
+
return false;
|
| 391 |
+
}
|
| 392 |
+
|
| 393 |
+
fprintf(stderr, "%s: saving output to '%s'\n", __func__, fname);
|
| 394 |
+
|
| 395 |
+
const int n_segments = whisper_full_n_segments(ctx);
|
| 396 |
+
fout << "start,end,text\n";
|
| 397 |
+
for (int i = 0; i < n_segments; ++i) {
|
| 398 |
+
const char * text = whisper_full_get_segment_text(ctx, i);
|
| 399 |
+
const int64_t t0 = whisper_full_get_segment_t0(ctx, i);
|
| 400 |
+
const int64_t t1 = whisper_full_get_segment_t1(ctx, i);
|
| 401 |
+
char * text_escaped = escape_double_quotes_and_backslashes(text);
|
| 402 |
+
|
| 403 |
+
//need to multiply times returned from whisper_full_get_segment_t{0,1}() by 10 to get milliseconds.
|
| 404 |
+
fout << 10 * t0 << "," << 10 * t1 << ",\"" << text_escaped << "\"\n";
|
| 405 |
+
}
|
| 406 |
+
|
| 407 |
+
return true;
|
| 408 |
+
}
|
| 409 |
+
|
| 410 |
bool output_json(struct whisper_context * ctx, const char * fname, const whisper_params & params) {
|
| 411 |
std::ofstream fout(fname);
|
| 412 |
int indent = 0;
|