Skip to content

Commit 70567ef

Browse files
authored
main : escape quotes in csv output (ggml-org#815)
1 parent 02ec83c commit 70567ef

File tree

1 file changed

+24
-23
lines changed

1 file changed

+24
-23
lines changed

examples/main/main.cpp

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -352,29 +352,6 @@ bool output_srt(struct whisper_context * ctx, const char * fname, const whisper_
352352
return true;
353353
}
354354

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-
378355
char *escape_double_quotes_and_backslashes(const char *str) {
379356
if (str == NULL) {
380357
return NULL;
@@ -406,6 +383,30 @@ char *escape_double_quotes_and_backslashes(const char *str) {
406383
return escaped;
407384
}
408385

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+
409410
bool output_json(struct whisper_context * ctx, const char * fname, const whisper_params & params) {
410411
std::ofstream fout(fname);
411412
int indent = 0;

0 commit comments

Comments
 (0)