]> git.djapps.eu Git - pkg/ggml/sources/whisper.cpp/commitdiff
ref #68, #79 : fix segment time output
authorGeorgi Gerganov <redacted>
Sun, 23 Oct 2022 10:29:36 +0000 (13:29 +0300)
committerGeorgi Gerganov <redacted>
Sun, 23 Oct 2022 10:30:30 +0000 (13:30 +0300)
main.cpp
whisper.cpp

index cbe9aa9e610ca6164857e43bb68f9fb476e0bd67..fd1e89e284ff56ceb59bb7fcef15e18cb0b06364 100644 (file)
--- a/main.cpp
+++ b/main.cpp
@@ -246,7 +246,7 @@ bool output_vtt(struct whisper_context * ctx, const char * fname) {
     return true;
 }
 
-bool output_srt(struct whisper_context * ctx, const char * fname) {
+bool output_srt(struct whisper_context * ctx, const char * fname, const whisper_params & params) {
     std::ofstream fout(fname);
     if (!fout.is_open()) {
         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) {
     const int n_segments = whisper_full_n_segments(ctx);
     for (int i = 0; i < n_segments; ++i) {
         const char * text = whisper_full_get_segment_text(ctx, i);
-        fout << text;
+        const int64_t t0 = whisper_full_get_segment_t0(ctx, i);
+        const int64_t t1 = whisper_full_get_segment_t1(ctx, i);
+
+        fout << i + 1 + params.offset_n << "\n";
+        fout << to_timestamp(t0) << " --> " << to_timestamp(t1) << "\n";
+        fout << text << "\n\n";
     }
 
     return true;
@@ -394,7 +399,7 @@ int main(int argc, char ** argv) {
             // output to SRT file
             if (params.output_srt) {
                 const auto fname_srt = fname_inp + ".srt";
-                output_srt(ctx, fname_srt.c_str());
+                output_srt(ctx, fname_srt.c_str(), params);
             }
         }
     }
index 01f6b00bed6c845e8be6ddb93b68ce7d5e732128..0634bc254645510a06d5a2bc46c5c9abfa73be97 100644 (file)
@@ -2526,7 +2526,7 @@ int whisper_full(
         // store the text from this iteration
         if (tokens_cur.size() > 0) {
             int  i0 = 0;
-            auto t0 = 2*(tokens_cur.front().tid - whisper_token_beg(ctx));
+            auto t0 = seek + 2*(tokens_cur.front().tid - whisper_token_beg(ctx));
 
             std::string text = "";
 
@@ -2540,7 +2540,7 @@ int whisper_full(
                     text += whisper_token_to_str(ctx, tokens_cur[i].id);
                 }
                 if (tokens_cur[i].id > whisper_token_beg(ctx)) {
-                    const auto t1 = 2*(tokens_cur[i].tid - whisper_token_beg(ctx));
+                    const auto t1 = seek + 2*(tokens_cur[i].tid - whisper_token_beg(ctx));
                     if (!text.empty()) {
                         if (params.print_realtime) {
                             if (params.print_timestamps) {