Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions examples/cli/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ struct cli_args {
bool quiet = false;
bool list_devices = false; // --list-devices: print devices and exit
bool batch_jsonl = false; // --batch-jsonl: output JSONL
std::string output_path; // -o: write raw text here
int repeat = 1;
Comment on lines 204 to 208
int n_threads = 0; // 0 = library default (all cores)
int n_ctx = 0; // 0 = model's true max; >0 lowers the cap
Expand Down Expand Up @@ -282,6 +283,7 @@ void print_usage(const char * argv0) {
" --target-language ISO target language for translation (e.g. de, es, fr)\n"
" -q, --quiet suppress library log output\n"
" -r, --repeat N run N times per file (benchmark)\n"
" -o, --output PATH write transcribed text to PATH (stdout unchanged)\n"
" --threads N CPU threads (default: all cores)\n"
Comment on lines 284 to 287
" --n-ctx N session context/KV cap in tokens (bounds decoder\n"
" KV memory; cannot extend the model): 0 = model\n"
Expand Down Expand Up @@ -528,6 +530,12 @@ bool parse_args(int argc, char ** argv, cli_args & out) {
std::fprintf(stderr, "error: --batch-size must be >= 0\n");
return false;
}
} else if (a == "-o" || a == "--output") {
const char * v = take_value(a.c_str());
if (!v) {
return false;
}
out.output_path = v;
} else if (a == "--initial-prompt") {
const char * v = take_value(a.c_str());
if (!v) {
Expand Down Expand Up @@ -688,6 +696,20 @@ void log_cb(transcribe_log_level level, const char * msg, void * userdata) {
std::fprintf(stderr, "%s %s%s", prefix, msg, (msg && *msg && msg[std::strlen(msg) - 1] == '\n') ? "" : "\n");
}

// Write transcription text to the output file if -o was given.
// Appends so it works for both single-file and batch mode.
static void write_output_file(const std::string & path, const char * text) {
if (path.empty() || text == nullptr || text[0] == '\0') {
return;
}
std::ofstream fout(path, std::ios::binary | std::ios::app);
if (!fout) {
std::fprintf(stderr, "error: cannot open %s for writing\n", path.c_str());
} else {
fout << text << '\n';
Comment on lines +705 to +709
}
}
Comment on lines +701 to +711

} // namespace

int main(int argc, char ** argv) {
Expand Down Expand Up @@ -957,6 +979,7 @@ int main(int argc, char ** argv) {
std::printf(" ERROR: %s\n", transcribe_status_string(ust));
}
}
write_output_file(args.output_path, text);
std::fflush(stdout);
}
}
Expand Down Expand Up @@ -1095,6 +1118,7 @@ int main(int argc, char ** argv) {
std::printf(" ERROR: %s\n", transcribe_status_string(run_st));
}
}
write_output_file(args.output_path, text);
std::fflush(stdout);
}
}
Expand Down Expand Up @@ -1341,6 +1365,7 @@ int main(int argc, char ** argv) {
if (result_present) {
const char * text = transcribe_full_text(ctx);
std::printf("text: %s\n", (text && *text) ? text : "(empty)");
write_output_file(args.output_path, text);

// A truncated decode hit the model's context/output budget before
// end-of-stream; the text above is incomplete.
Expand Down