|
9 | 9 | #include <cstdio>
|
10 | 10 | #include <string>
|
11 | 11 | #include <thread>
|
| 12 | +#include <utility> |
12 | 13 | #include <vector>
|
13 | 14 | #include <cstring>
|
14 | 15 |
|
@@ -1066,8 +1067,45 @@ int main(int argc, char ** argv) {
|
1066 | 1067 | }
|
1067 | 1068 |
|
1068 | 1069 | 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}; |
1071 | 1109 |
|
1072 | 1110 | std::vector<float> pcmf32; // mono-channel F32 PCM
|
1073 | 1111 | std::vector<std::vector<float>> pcmf32s; // stereo-channel F32 PCM
|
@@ -1172,7 +1210,7 @@ int main(int argc, char ** argv) {
|
1172 | 1210 |
|
1173 | 1211 | // this callback is called on each new segment
|
1174 | 1212 | if (!wparams.print_realtime) {
|
1175 |
| - wparams.new_segment_callback = whisper_print_segment_callback; |
| 1213 | + wparams.new_segment_callback = fname_out_factory.print_segment_callback; |
1176 | 1214 | wparams.new_segment_callback_user_data = &user_data;
|
1177 | 1215 | }
|
1178 | 1216 |
|
@@ -1214,54 +1252,44 @@ int main(int argc, char ** argv) {
|
1214 | 1252 |
|
1215 | 1253 | // output stuff
|
1216 | 1254 | {
|
1217 |
| - printf("\n"); |
1218 |
| - |
1219 | 1255 | // 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); |
1223 | 1258 | }
|
1224 | 1259 |
|
1225 | 1260 | // 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); |
1229 | 1263 | }
|
1230 | 1264 |
|
1231 | 1265 | // 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); |
1235 | 1268 | }
|
1236 | 1269 |
|
1237 | 1270 | // 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); |
1241 | 1273 | }
|
1242 | 1274 |
|
1243 | 1275 | // 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); |
1247 | 1278 | }
|
1248 | 1279 |
|
1249 | 1280 | // 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); |
1253 | 1283 | }
|
1254 | 1284 |
|
1255 | 1285 | // 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); |
1259 | 1288 | }
|
1260 | 1289 |
|
1261 | 1290 | // 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); |
1265 | 1293 | }
|
1266 | 1294 | }
|
1267 | 1295 | }
|
|
0 commit comments