From ee07bbe569174b6fbf6fc3313cb8b7eb9dfa3882 Mon Sep 17 00:00:00 2001 From: Roy Shilkrot Date: Tue, 9 Jul 2024 17:02:58 -0400 Subject: [PATCH] refactor: Update file output option in transcription filter (#128) - Update the file output option in the transcription filter to use the new "Save to File" label instead of "Text File output" - Add a new boolean flag "save_to_file" in the transcription filter data structure to track the file output setting - Update the code in transcription-filter-callbacks.cpp and transcription-filter.cpp to use the new flag for file output logic - Update the properties and UI in transcription-filter-properties.cpp to reflect the changes --- data/locale/en-US.ini | 2 +- src/transcription-filter-callbacks.cpp | 2 +- src/transcription-filter-data.h | 1 + src/transcription-filter.cpp | 36 ++++++++++++++------------ 4 files changed, 23 insertions(+), 18 deletions(-) diff --git a/data/locale/en-US.ini b/data/locale/en-US.ini index 6baafef..eb5cac6 100644 --- a/data/locale/en-US.ini +++ b/data/locale/en-US.ini @@ -9,7 +9,7 @@ step_by_step_processing="Step-by-step processing (⚠️ increased processing)" step_size_msec="Step size (ms)" subtitle_sources="Subtitles Output" none_no_output="None / No output" -text_file_output="Text File output" +file_output_enable="Save to File" output_filename="Output filename" whisper_model="Whisper Model" external_model_file="External model file" diff --git a/src/transcription-filter-callbacks.cpp b/src/transcription-filter-callbacks.cpp index 13394b0..294d3e1 100644 --- a/src/transcription-filter-callbacks.cpp +++ b/src/transcription-filter-callbacks.cpp @@ -209,7 +209,7 @@ void set_text_callback(struct transcription_filter_data *gf, send_caption_to_stream(result, str_copy, gf); } - if (gf->output_file_path != "" && gf->text_source_name.empty()) { + if (gf->save_to_file && gf->output_file_path != "") { send_sentence_to_file(gf, result, str_copy); } }; diff --git a/src/transcription-filter-data.h b/src/transcription-filter-data.h index 84d1fef..b1fded4 100644 --- a/src/transcription-filter-data.h +++ b/src/transcription-filter-data.h @@ -65,6 +65,7 @@ struct transcription_filter_data { bool log_words; bool caption_to_stream; bool active = false; + bool save_to_file = false; bool save_srt = false; bool truncate_output_file = false; bool save_only_while_recording = false; diff --git a/src/transcription-filter.cpp b/src/transcription-filter.cpp index e56d35f..5429e9a 100644 --- a/src/transcription-filter.cpp +++ b/src/transcription-filter.cpp @@ -176,6 +176,7 @@ void transcription_filter_update(void *data, obs_data_t *s) gf->vad_enabled = obs_data_get_bool(s, "vad_enabled"); gf->log_words = obs_data_get_bool(s, "log_words"); gf->caption_to_stream = obs_data_get_bool(s, "caption_to_stream"); + gf->save_to_file = obs_data_get_bool(s, "file_output_enable"); gf->save_srt = obs_data_get_bool(s, "subtitle_save_srt"); gf->truncate_output_file = obs_data_get_bool(s, "truncate_output_file"); gf->save_only_while_recording = obs_data_get_bool(s, "only_while_recording"); @@ -194,6 +195,17 @@ void transcription_filter_update(void *data, obs_data_t *s) gf->filter_words_replace = deserialize_filter_words_replace(obs_data_get_string(s, "filter_words_replace")); + if (gf->save_to_file) { + gf->output_file_path = ""; + // set the output file path + const char *output_file_path = obs_data_get_string(s, "subtitle_output_filename"); + if (output_file_path != nullptr && strlen(output_file_path) > 0) { + gf->output_file_path = output_file_path; + } else { + obs_log(gf->log_level, "output file path is empty, but selected to save"); + } + } + if (new_buffered_output) { obs_log(gf->log_level, "buffered_output enable"); if (!gf->buffered_output || !gf->captions_monitor.isEnabled()) { @@ -295,19 +307,9 @@ void transcription_filter_update(void *data, obs_data_t *s) const char *new_text_source_name = obs_data_get_string(s, "subtitle_sources"); if (new_text_source_name == nullptr || strcmp(new_text_source_name, "none") == 0 || - strcmp(new_text_source_name, "(null)") == 0 || - strcmp(new_text_source_name, "text_file") == 0 || strlen(new_text_source_name) == 0) { + strcmp(new_text_source_name, "(null)") == 0 || strlen(new_text_source_name) == 0) { // new selected text source is not valid, release the old one gf->text_source_name.clear(); - gf->output_file_path = ""; - if (strcmp(new_text_source_name, "text_file") == 0) { - // set the output file path - const char *output_file_path = - obs_data_get_string(s, "subtitle_output_filename"); - if (output_file_path != nullptr && strlen(output_file_path) > 0) { - gf->output_file_path = output_file_path; - } - } } else { gf->text_source_name = new_text_source_name; } @@ -479,13 +481,12 @@ void *transcription_filter_create(obs_data_t *settings, obs_source_t *filter) return gf; } -bool subs_output_select_changed(obs_properties_t *props, obs_property_t *property, +bool file_output_select_changed(obs_properties_t *props, obs_property_t *property, obs_data_t *settings) { UNUSED_PARAMETER(property); // Show or hide the output filename selection input - const char *new_output = obs_data_get_string(settings, "subtitle_sources"); - const bool show_hide = (strcmp(new_output, "text_file") == 0); + const bool show_hide = obs_data_get_bool(settings, "file_output_enable"); for (const std::string &prop_name : {"subtitle_output_filename", "subtitle_save_srt", "truncate_output_file", "only_while_recording", "rename_file_to_match_recording"}) { @@ -605,10 +606,13 @@ obs_properties_t *transcription_filter_properties(void *data) OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_STRING); // Add "none" option obs_property_list_add_string(subs_output, MT_("none_no_output"), "none"); - obs_property_list_add_string(subs_output, MT_("text_file_output"), "text_file"); // Add text sources obs_enum_sources(add_sources_to_list, subs_output); + // add a checkbox for file output + obs_property_t *file_output_enable = + obs_properties_add_bool(ppts, "file_output_enable", MT_("file_output_enable")); + obs_properties_add_path(ppts, "subtitle_output_filename", MT_("output_filename"), OBS_PATH_FILE_SAVE, "Text (*.txt)", NULL); obs_properties_add_bool(ppts, "subtitle_save_srt", MT_("save_srt")); @@ -617,7 +621,7 @@ obs_properties_t *transcription_filter_properties(void *data) obs_properties_add_bool(ppts, "rename_file_to_match_recording", MT_("rename_file_to_match_recording")); - obs_property_set_modified_callback(subs_output, subs_output_select_changed); + obs_property_set_modified_callback(file_output_enable, file_output_select_changed); // Add a list of available whisper models to download obs_property_t *whisper_models_list =