Skip to content

Commit

Permalink
enable transcript rename for non-srt files (#179)
Browse files Browse the repository at this point in the history
* enable transcript rename for non-srt files

* Update transcription-filter-callbacks.cpp

fix edit mistake

* tweak formatting

* Refactor file renaming logic in transcription-filter-callbacks.cpp

* Refactor file renaming logic and handle recording state changes in transcription-filter-callbacks.cpp

* Refactor file renaming logic and handle recording state changes in transcription-filter-callbacks.cpp

---------

Co-authored-by: Stephen <stephen@lenovo-thinkpad.lan>
Co-authored-by: Stephen Schrauger <6665521+schrauger@users.noreply.github.com>
Co-authored-by: schrauger <schrauger@users.noreply.github.com>
  • Loading branch information
4 people authored Oct 11, 2024
1 parent e26819c commit 58005da
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 15 deletions.
59 changes: 48 additions & 11 deletions src/transcription-filter-callbacks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <regex>
#include <string>
#include <vector>
#include <filesystem>

#include "transcription-filter-callbacks.h"
#include "transcription-utils.h"
Expand Down Expand Up @@ -297,6 +298,23 @@ void set_text_callback(struct transcription_filter_data *gf,
}
};

/**
* @brief Callback function to handle recording state changes in OBS.
*
* This function is triggered by OBS frontend events related to recording state changes.
* It performs actions based on whether the recording is starting or stopping.
*
* @param event The OBS frontend event indicating the recording state change.
* @param data Pointer to user data, expected to be a struct transcription_filter_data.
*
* When the recording is starting:
* - If saving SRT files and saving only while recording is enabled, it resets the SRT file,
* truncates the existing file, and initializes the sentence number and start timestamp.
*
* When the recording is stopping:
* - If saving only while recording or renaming the file to match the recording is not enabled, it returns immediately.
* - Otherwise, it renames the output file to match the recording file name with the appropriate extension.
*/
void recording_state_callback(enum obs_frontend_event event, void *data)
{
struct transcription_filter_data *gf_ =
Expand All @@ -315,19 +333,38 @@ void recording_state_callback(enum obs_frontend_event event, void *data)
gf_->start_timestamp_ms = now_ms();
}
} else if (event == OBS_FRONTEND_EVENT_RECORDING_STOPPED) {
if (gf_->save_srt && gf_->save_only_while_recording &&
gf_->rename_file_to_match_recording) {
if (!gf_->save_only_while_recording || !gf_->rename_file_to_match_recording) {
return;
}

namespace fs = std::filesystem;

char *recordingFileName = obs_frontend_get_last_recording();
std::string recordingFileNameStr(recordingFileName);
bfree(recordingFileName);
fs::path recordingPath(recordingFileName);
fs::path outputPath(gf_->output_file_path);

fs::path newPath = recordingPath.stem();

if (gf_->save_srt) {
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());
newPath.replace_extension(".srt");
} else {
obs_log(gf_->log_level, "Recording stopped. Rename transcript file.");
std::string newExtension = outputPath.extension().string();

if (newExtension == recordingPath.extension().string()) {
newExtension += ".txt";
}

newPath.replace_extension(newExtension);
}

// make sure newPath is next to the recording file
newPath = recordingPath.parent_path() / newPath.filename();

fs::rename(outputPath, newPath);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/transcription-filter-properties.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,7 @@ void transcription_filter_defaults(obs_data_t *s)
obs_data_set_default_string(s, "translation_model_path_external", "");
obs_data_set_default_int(s, "translate_input_tokenization_style", INPUT_TOKENIZAION_M2M100);
obs_data_set_default_double(s, "sentence_psum_accept_thresh", 0.4);
obs_data_set_default_bool(s, "partial_group", false);
obs_data_set_default_bool(s, "partial_group", true);
obs_data_set_default_int(s, "partial_latency", 1100);

// translation options
Expand Down
9 changes: 7 additions & 2 deletions src/transcription-filter-utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@ void create_obs_text_source_if_needed()
// set source settings
obs_data_t *source_settings = obs_source_get_settings(source);
obs_data_set_bool(source_settings, "word_wrap", true);
obs_data_set_int(source_settings, "custom_width", 1760);
obs_data_set_bool(source_settings, "extents", true);
obs_data_set_bool(source_settings, "outline", true);
obs_data_set_int(source_settings, "outline_color", 4278190080);
obs_data_set_int(source_settings, "outline_size", 7);
obs_data_set_int(source_settings, "extents_cx", 1500);
obs_data_set_int(source_settings, "extents_cy", 230);
obs_data_t *font_data = obs_data_create();
obs_data_set_string(font_data, "face", "Arial");
obs_data_set_string(font_data, "style", "Regular");
Expand Down Expand Up @@ -62,7 +67,7 @@ void create_obs_text_source_if_needed()

bool add_sources_to_list(void *list_property, obs_source_t *source)
{
auto source_id = obs_source_get_id(source);
const char *source_id = obs_source_get_id(source);
if (strcmp(source_id, "text_ft2_source_v2") != 0 &&
strcmp(source_id, "text_gdiplus_v3") != 0 &&
strcmp(source_id, "text_gdiplus_v2") != 0) {
Expand Down
5 changes: 4 additions & 1 deletion src/transcription-filter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ void transcription_filter_update(void *data, obs_data_t *s)
}
}

if (gf->context != nullptr && obs_source_enabled(gf->context)) {
if (gf->context != nullptr && (obs_source_enabled(gf->context) || gf->initial_creation)) {
if (gf->initial_creation) {
obs_log(LOG_INFO, "Initial filter creation and source enabled");

Expand All @@ -423,6 +423,8 @@ void transcription_filter_update(void *data, obs_data_t *s)
update_whisper_model(gf);
}
}
} else {
obs_log(LOG_INFO, "Filter not enabled, not updating whisper model.");
}
}

Expand All @@ -449,6 +451,7 @@ void *transcription_filter_create(obs_data_t *settings, obs_source_t *filter)
obs_data_get_bool(settings, "rename_file_to_match_recording");
gf->process_while_muted = obs_data_get_bool(settings, "process_while_muted");
gf->buffered_output = obs_data_get_bool(settings, "buffered_output");
gf->initial_creation = true;

for (size_t i = 0; i < gf->channels; i++) {
circlebuf_init(&gf->input_buffers[i]);
Expand Down

0 comments on commit 58005da

Please sign in to comment.