Skip to content

Commit 755ccbb

Browse files
committed
cli : Support "-" for stdout like stdin
This changes examples/cli/cli.cpp to be like examples/common-whisper.cpp. "-of -" can be specified (or this can be inferred from "-" as the input file) to output to stdout. This is useful for piping to other applications. Closes #3048
1 parent 2a2d21c commit 755ccbb

File tree

1 file changed

+57
-29
lines changed

1 file changed

+57
-29
lines changed

examples/cli/cli.cpp

+57-29
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <cstdio>
1010
#include <string>
1111
#include <thread>
12+
#include <utility>
1213
#include <vector>
1314
#include <cstring>
1415

@@ -1066,8 +1067,45 @@ int main(int argc, char ** argv) {
10661067
}
10671068

10681069
for (int f = 0; f < (int) params.fname_inp.size(); ++f) {
1069-
const auto fname_inp = params.fname_inp[f];
1070-
const auto fname_out = f < (int) params.fname_out.size() && !params.fname_out[f].empty() ? params.fname_out[f] : params.fname_inp[f];
1070+
const auto & fname_inp = params.fname_inp[f];
1071+
class FnameOutFactory {
1072+
std::string buf;
1073+
const size_t basename_length;
1074+
bool used_stdout;
1075+
1076+
public:
1077+
const bool is_stdout;
1078+
typeof(whisper_print_segment_callback) * const print_segment_callback;
1079+
1080+
FnameOutFactory(const std::string & fname_out, const std::string & fname_inp, whisper_params & params) :
1081+
buf{!fname_out.empty() ? fname_out : fname_inp},
1082+
basename_length{buf.size()},
1083+
is_stdout{buf == "-"}, used_stdout{params.diarize},
1084+
print_segment_callback{is_stdout && !used_stdout ? nullptr : whisper_print_segment_callback} {
1085+
if (!print_segment_callback) {
1086+
params.no_timestamps = true;
1087+
params.print_progress = false;
1088+
}
1089+
}
1090+
1091+
const char * create(bool enable, const char * ext) {
1092+
if (!enable) return nullptr;
1093+
if (is_stdout) {
1094+
if (std::exchange(used_stdout, true)) {
1095+
fprintf(stderr, "Not appending multiple file formats to stdout\n");
1096+
return nullptr;
1097+
}
1098+
#ifdef _WIN32
1099+
return "CON";
1100+
#else
1101+
return "/dev/stdout";
1102+
#endif
1103+
}
1104+
buf.resize(basename_length);
1105+
buf += ext;
1106+
return buf.c_str();
1107+
}
1108+
} fname_out_factory{f < (int) params.fname_out.size() ? params.fname_out[f] : "", fname_inp, params};
10711109

10721110
std::vector<float> pcmf32; // mono-channel F32 PCM
10731111
std::vector<std::vector<float>> pcmf32s; // stereo-channel F32 PCM
@@ -1172,7 +1210,7 @@ int main(int argc, char ** argv) {
11721210

11731211
// this callback is called on each new segment
11741212
if (!wparams.print_realtime) {
1175-
wparams.new_segment_callback = whisper_print_segment_callback;
1213+
wparams.new_segment_callback = fname_out_factory.print_segment_callback;
11761214
wparams.new_segment_callback_user_data = &user_data;
11771215
}
11781216

@@ -1214,54 +1252,44 @@ int main(int argc, char ** argv) {
12141252

12151253
// output stuff
12161254
{
1217-
printf("\n");
1218-
12191255
// output to text file
1220-
if (params.output_txt) {
1221-
const auto fname_txt = fname_out + ".txt";
1222-
output_txt(ctx, fname_txt.c_str(), params, pcmf32s);
1256+
if (auto fname_out = fname_out_factory.create(params.output_txt, ".txt")) {
1257+
output_txt(ctx, fname_out, params, pcmf32s);
12231258
}
12241259

12251260
// output to VTT file
1226-
if (params.output_vtt) {
1227-
const auto fname_vtt = fname_out + ".vtt";
1228-
output_vtt(ctx, fname_vtt.c_str(), params, pcmf32s);
1261+
if (auto fname_out = fname_out_factory.create(params.output_vtt, ".vtt")) {
1262+
output_vtt(ctx, fname_out, params, pcmf32s);
12291263
}
12301264

12311265
// output to SRT file
1232-
if (params.output_srt) {
1233-
const auto fname_srt = fname_out + ".srt";
1234-
output_srt(ctx, fname_srt.c_str(), params, pcmf32s);
1266+
if (auto fname_out = fname_out_factory.create(params.output_srt, ".srt")) {
1267+
output_srt(ctx, fname_out, params, pcmf32s);
12351268
}
12361269

12371270
// output to WTS file
1238-
if (params.output_wts) {
1239-
const auto fname_wts = fname_out + ".wts";
1240-
output_wts(ctx, fname_wts.c_str(), fname_inp.c_str(), params, float(pcmf32.size() + 1000)/WHISPER_SAMPLE_RATE, pcmf32s);
1271+
if (auto fname_out = fname_out_factory.create(params.output_wts, ".wts")) {
1272+
output_wts(ctx, fname_out, fname_inp.c_str(), params, float(pcmf32.size() + 1000)/WHISPER_SAMPLE_RATE, pcmf32s);
12411273
}
12421274

12431275
// output to CSV file
1244-
if (params.output_csv) {
1245-
const auto fname_csv = fname_out + ".csv";
1246-
output_csv(ctx, fname_csv.c_str(), params, pcmf32s);
1276+
if (auto fname_out = fname_out_factory.create(params.output_csv, ".csv")) {
1277+
output_csv(ctx, fname_out, params, pcmf32s);
12471278
}
12481279

12491280
// output to JSON file
1250-
if (params.output_jsn) {
1251-
const auto fname_jsn = fname_out + ".json";
1252-
output_json(ctx, fname_jsn.c_str(), params, pcmf32s, params.output_jsn_full);
1281+
if (auto fname_out = fname_out_factory.create(params.output_jsn, ".json")) {
1282+
output_json(ctx, fname_out, params, pcmf32s, params.output_jsn_full);
12531283
}
12541284

12551285
// output to LRC file
1256-
if (params.output_lrc) {
1257-
const auto fname_lrc = fname_out + ".lrc";
1258-
output_lrc(ctx, fname_lrc.c_str(), params, pcmf32s);
1286+
if (auto fname_out = fname_out_factory.create(params.output_lrc, ".lrc")) {
1287+
output_lrc(ctx, fname_out, params, pcmf32s);
12591288
}
12601289

12611290
// output to score file
1262-
if (params.log_score) {
1263-
const auto fname_score = fname_out + ".score.txt";
1264-
output_score(ctx, fname_score.c_str(), params, pcmf32s);
1291+
if (auto fname_out = fname_out_factory.create(params.log_score, ".score.txt")) {
1292+
output_score(ctx, fname_out, params, pcmf32s);
12651293
}
12661294
}
12671295
}

0 commit comments

Comments
 (0)