add file rename

This commit is contained in:
Roy Shilkrot
2023-10-12 10:45:34 -04:00
parent f5d81d3382
commit 82a473fabb
5 changed files with 38 additions and 0 deletions

View File

@@ -40,3 +40,4 @@ length_penalty="Length penalty"
save_srt="Save in SRT format (no file truncation)"
only_while_recording="Write output only while recording"
process_while_muted="Process speech while source is muted"
rename_file_to_match_recording="Rename file to match recording"

View File

@@ -38,3 +38,7 @@ suppress_non_speech_tokens="Suprimir tokens não fala"
temperature="Temperatura"
max_initial_ts="Tempo inicial máximo"
length_penalty="Pena de comprimento"
save_srt="Salvar no formato SRT"
only_while_recording="Escreva durante a gravação"
process_while_muted="Processar enquanto está silenciada"
rename_file_to_match_recording="Renomear arquivo para corresponder à gravação"

View File

@@ -37,3 +37,7 @@ suppress_non_speech_tokens="Подавить токены, не относящи
temperature="Температура"
max_initial_ts="Максимальное начальное время"
length_penalty="Штраф за длину"
save_srt="Сохранить в формате SRT"
only_while_recording="Записывать вывод только во время записи"
process_while_muted="Обрабатывать речь, пока источник отключен"
rename_file_to_match_recording="Переименовать файл, чтобы соответствовать записи"

View File

@@ -74,6 +74,7 @@ struct transcription_filter_data {
bool save_srt = false;
bool save_only_while_recording = false;
bool process_while_muted = false;
bool rename_file_to_match_recording = false;
// Text source to output the subtitles
obs_weak_source_t *text_source = nullptr;

View File

@@ -348,6 +348,7 @@ void transcription_filter_update(void *data, obs_data_t *s)
: BUFFER_SIZE_MSEC;
gf->save_srt = obs_data_get_bool(s, "subtitle_save_srt");
gf->save_only_while_recording = obs_data_get_bool(s, "only_while_recording");
gf->rename_file_to_match_recording = obs_data_get_bool(s, "rename_file_to_match_recording");
// Get the current timestamp using the system clock
gf->start_timestamp_ms = now_ms();
gf->sentence_number = 1;
@@ -530,6 +531,8 @@ void *transcription_filter_create(obs_data_t *settings, obs_source_t *filter)
gf->log_level = (int)obs_data_get_int(settings, "log_level");
gf->save_srt = obs_data_get_bool(settings, "subtitle_save_srt");
gf->save_only_while_recording = obs_data_get_bool(settings, "only_while_recording");
gf->rename_file_to_match_recording =
obs_data_get_bool(settings, "rename_file_to_match_recording");
gf->process_while_muted = obs_data_get_bool(settings, "process_while_muted");
for (size_t i = 0; i < MAX_AUDIO_CHANNELS; i++) {
@@ -606,6 +609,26 @@ void *transcription_filter_create(obs_data_t *settings, obs_source_t *filter)
gf_->sentence_number = 1;
gf_->start_timestamp_ms = now_ms();
}
} else if (event == OBS_FRONTEND_EVENT_RECORDING_STOPPED) {
struct transcription_filter_data *gf_ =
static_cast<struct transcription_filter_data *>(
private_data);
if (gf_->save_srt && gf_->save_only_while_recording &&
gf_->rename_file_to_match_recording) {
obs_log(gf_->log_level,
"Recording stopped. Rename srt file.");
// rename file to match the recording file name with .srt extension
// use obs_frontend_get_last_recording to get the last recording file name
std::string recording_file_name =
obs_frontend_get_last_recording();
// remove the extension
recording_file_name = recording_file_name.substr(
0, recording_file_name.find_last_of("."));
std::string srt_file_name = recording_file_name + ".srt";
// rename the file
std::rename(gf_->output_file_path.c_str(),
srt_file_name.c_str());
}
}
},
gf);
@@ -645,6 +668,7 @@ void transcription_filter_defaults(obs_data_t *s)
obs_data_set_default_bool(s, "process_while_muted", false);
obs_data_set_default_bool(s, "subtitle_save_srt", false);
obs_data_set_default_bool(s, "only_while_recording", false);
obs_data_set_default_bool(s, "rename_file_to_match_recording", true);
obs_data_set_default_int(s, "step_size_msec", 1000);
// Whisper parameters
@@ -719,6 +743,8 @@ obs_properties_t *transcription_filter_properties(void *data)
OBS_PATH_FILE_SAVE, "Text (*.txt)", NULL);
obs_properties_add_bool(ppts, "subtitle_save_srt", MT_("save_srt"));
obs_properties_add_bool(ppts, "only_while_recording", MT_("only_while_recording"));
obs_properties_add_bool(ppts, "rename_file_to_match_recording",
MT_("rename_file_to_match_recording"));
obs_property_set_modified_callback(subs_output, [](obs_properties_t *props,
obs_property_t *property,
@@ -732,6 +758,8 @@ obs_properties_t *transcription_filter_properties(void *data)
obs_property_set_visible(obs_properties_get(props, "subtitle_save_srt"), show_hide);
obs_property_set_visible(obs_properties_get(props, "only_while_recording"),
show_hide);
obs_property_set_visible(
obs_properties_get(props, "rename_file_to_match_recording"), show_hide);
return true;
});