-
Notifications
You must be signed in to change notification settings - Fork 4.2k
cli : Support "-" for stdout like stdin #3050
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
danielzgtg
wants to merge
1
commit into
ggml-org:master
Choose a base branch
from
danielzgtg:feat/fd
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+57
−29
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ | |
#include <cstdio> | ||
#include <string> | ||
#include <thread> | ||
#include <utility> | ||
#include <vector> | ||
#include <cstring> | ||
|
||
|
@@ -1066,8 +1067,45 @@ int main(int argc, char ** argv) { | |
} | ||
|
||
for (int f = 0; f < (int) params.fname_inp.size(); ++f) { | ||
const auto fname_inp = params.fname_inp[f]; | ||
const auto fname_out = f < (int) params.fname_out.size() && !params.fname_out[f].empty() ? params.fname_out[f] : params.fname_inp[f]; | ||
const auto & fname_inp = params.fname_inp[f]; | ||
class FnameOutFactory { | ||
std::string buf; | ||
const size_t basename_length; | ||
bool used_stdout; | ||
|
||
public: | ||
const bool is_stdout; | ||
decltype(whisper_print_segment_callback) * const print_segment_callback; | ||
|
||
FnameOutFactory(const std::string & fname_out, const std::string & fname_inp, whisper_params & params) : | ||
buf{!fname_out.empty() ? fname_out : fname_inp}, | ||
basename_length{buf.size()}, | ||
is_stdout{buf == "-"}, used_stdout{params.diarize}, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps place |
||
print_segment_callback{is_stdout && !used_stdout ? nullptr : whisper_print_segment_callback} { | ||
if (!print_segment_callback) { | ||
params.no_timestamps = true; | ||
params.print_progress = false; | ||
} | ||
} | ||
|
||
const char * create(bool enable, const char * ext) { | ||
if (!enable) return nullptr; | ||
if (is_stdout) { | ||
if (std::exchange(used_stdout, true)) { | ||
fprintf(stderr, "Not appending multiple file formats to stdout\n"); | ||
return nullptr; | ||
} | ||
#ifdef _WIN32 | ||
return "CON"; | ||
#else | ||
return "/dev/stdout"; | ||
#endif | ||
} | ||
buf.resize(basename_length); | ||
buf += ext; | ||
return buf.c_str(); | ||
} | ||
} fname_out_factory{f < (int) params.fname_out.size() ? params.fname_out[f] : "", fname_inp, params}; | ||
|
||
std::vector<float> pcmf32; // mono-channel F32 PCM | ||
std::vector<std::vector<float>> pcmf32s; // stereo-channel F32 PCM | ||
|
@@ -1172,7 +1210,7 @@ int main(int argc, char ** argv) { | |
|
||
// this callback is called on each new segment | ||
if (!wparams.print_realtime) { | ||
wparams.new_segment_callback = whisper_print_segment_callback; | ||
wparams.new_segment_callback = fname_out_factory.print_segment_callback; | ||
wparams.new_segment_callback_user_data = &user_data; | ||
} | ||
|
||
|
@@ -1214,54 +1252,44 @@ int main(int argc, char ** argv) { | |
|
||
// output stuff | ||
{ | ||
printf("\n"); | ||
|
||
// output to text file | ||
if (params.output_txt) { | ||
const auto fname_txt = fname_out + ".txt"; | ||
output_txt(ctx, fname_txt.c_str(), params, pcmf32s); | ||
if (auto fname_out = fname_out_factory.create(params.output_txt, ".txt")) { | ||
output_txt(ctx, fname_out, params, pcmf32s); | ||
} | ||
|
||
// output to VTT file | ||
if (params.output_vtt) { | ||
const auto fname_vtt = fname_out + ".vtt"; | ||
output_vtt(ctx, fname_vtt.c_str(), params, pcmf32s); | ||
if (auto fname_out = fname_out_factory.create(params.output_vtt, ".vtt")) { | ||
output_vtt(ctx, fname_out, params, pcmf32s); | ||
} | ||
|
||
// output to SRT file | ||
if (params.output_srt) { | ||
const auto fname_srt = fname_out + ".srt"; | ||
output_srt(ctx, fname_srt.c_str(), params, pcmf32s); | ||
if (auto fname_out = fname_out_factory.create(params.output_srt, ".srt")) { | ||
output_srt(ctx, fname_out, params, pcmf32s); | ||
} | ||
|
||
// output to WTS file | ||
if (params.output_wts) { | ||
const auto fname_wts = fname_out + ".wts"; | ||
output_wts(ctx, fname_wts.c_str(), fname_inp.c_str(), params, float(pcmf32.size() + 1000)/WHISPER_SAMPLE_RATE, pcmf32s); | ||
if (auto fname_out = fname_out_factory.create(params.output_wts, ".wts")) { | ||
output_wts(ctx, fname_out, fname_inp.c_str(), params, float(pcmf32.size() + 1000)/WHISPER_SAMPLE_RATE, pcmf32s); | ||
} | ||
|
||
// output to CSV file | ||
if (params.output_csv) { | ||
const auto fname_csv = fname_out + ".csv"; | ||
output_csv(ctx, fname_csv.c_str(), params, pcmf32s); | ||
if (auto fname_out = fname_out_factory.create(params.output_csv, ".csv")) { | ||
output_csv(ctx, fname_out, params, pcmf32s); | ||
} | ||
|
||
// output to JSON file | ||
if (params.output_jsn) { | ||
const auto fname_jsn = fname_out + ".json"; | ||
output_json(ctx, fname_jsn.c_str(), params, pcmf32s, params.output_jsn_full); | ||
if (auto fname_out = fname_out_factory.create(params.output_jsn, ".json")) { | ||
output_json(ctx, fname_out, params, pcmf32s, params.output_jsn_full); | ||
} | ||
|
||
// output to LRC file | ||
if (params.output_lrc) { | ||
const auto fname_lrc = fname_out + ".lrc"; | ||
output_lrc(ctx, fname_lrc.c_str(), params, pcmf32s); | ||
if (auto fname_out = fname_out_factory.create(params.output_lrc, ".lrc")) { | ||
output_lrc(ctx, fname_out, params, pcmf32s); | ||
} | ||
|
||
// output to score file | ||
if (params.log_score) { | ||
const auto fname_score = fname_out + ".score.txt"; | ||
output_score(ctx, fname_score.c_str(), params, pcmf32s); | ||
if (auto fname_out = fname_out_factory.create(params.log_score, ".score.txt")) { | ||
output_score(ctx, fname_out, params, pcmf32s); | ||
} | ||
} | ||
} | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you rename this class to follow the naming conventions. In this case something like
fname_out_factory
perhaps.