from ui import * from file_name import * from pydub import AudioSegment from utils import load_groups_json from video_tool import add_subtitle_to_video import gradio as gr import os import utils min_duration_ms = 10000 current_pos = 0 speaker_to_name = {"Speaker": "Name"} speaker_to_sample = {"Speaker": "File"} sample_groups = [] def get_current(): global speaker_to_name global speaker_to_sample print(f"GET CURRENT speaker_to_name: {speaker_to_name}") print(f"GET CURRENT speaker_to_sample: {speaker_to_sample}") name = list(speaker_to_name.values())[current_pos] sample = list(speaker_to_sample.values())[current_pos] print(f"CURRENT: {name} {sample}") return [name, sample] def prepare_output(input_file): if input_file is None or not os.path.exists(input_file): return [None, None, None, None, None] speakers = get_speakers() adjust_speaker_update = adjust_speaker.update( speakers[0], interactive=True) adjust_audio_update = adjust_audio.update(speakers[1], interactive=True) prev_button_update = prev_button.update(interactive=True) next_button_update = next_button.update(interactive=True) adjust_button_update = adjust_button.update(interactive=True) # Return adjust speaker, adjust audio, previous, next, adjust button return [adjust_speaker_update, adjust_audio_update, prev_button_update, next_button_update, adjust_button_update] def get_speakers(): global sample_groups global speaker_to_name global speaker_to_sample sample_groups, _ = load_groups_json() print(f"SAMPLE GROUP: {sample_groups}") speaker_to_name = {} speaker_to_sample = {} for speaker in sample_groups: for suffix in range(1, 100): file_path = f"{speaker}-{suffix}.wav" if os.path.exists(file_path): audio_segment = AudioSegment.from_file(file_path) if len(audio_segment) > min_duration_ms: print(f"Found file: {file_path}") print( f"File duration: {len(audio_segment) / 1000} seconds") break temp_file_name = f"{speaker}-sample.wav" audio_segment[:10 * 1000].export(temp_file_name, format="wav") speaker_to_sample[speaker] = temp_file_name speaker_to_name[speaker] = speaker print(f"speaker_to_name: {speaker_to_name}") print(f"speaker_to_sample: {speaker_to_sample}") return get_current() def change_name(to_name): global sample_groups global speaker_to_name global current_pos current_speaker = sample_groups[current_pos] speaker_to_name[current_speaker] = to_name print(str(get_current())) def get_speakers_next(to_name): change_name(to_name) global sample_groups global current_pos if (current_pos < len(sample_groups) - 1): current_pos += 1 return get_current() def get_speakers_previous(to_name): change_name(to_name) global current_pos if (current_pos > 0): current_pos -= 1 return get_current() def start_adjust(input_file, to_name, start_time, end_time, progress=gr.Progress()): change_name(to_name) # Replacing texts progress(0.4, desc=ui_lang["progress_adjust_speaker"]) transcribe_txt_list, subtitle_txt_list = utils.read_transcribe_subtitle_file(input_file, False) modified_transcribe = replace_text(transcribe_txt_list) modified_subtitle = replace_text(subtitle_txt_list) utils.write_transcribe_subtitle_file( input_file, modified_transcribe, modified_subtitle, True) # Adding subtitle to video progress(0.8, desc=ui_lang["progress_add_subtitle"]) add_subtitle_to_video( input_file, subtitle_adjusted_file, video_subtitle_file, start_time, end_time) # Return video file link, transcribe string, transcribe.txt, subtitle.txt transcribe_txt_list, _ = utils.read_transcribe_subtitle_file( input_file, True) print(line for line in transcribe_txt_list) transcribe_txt = "\n".join(transcribe_txt_list) return [ video_subtitle_file, transcribe_txt, [transcribe_adjusted_file, subtitle_adjusted_file] ] def replace_text(lines): modified_lines = [] for line in lines: for key, value in speaker_to_name.items(): line = line.replace(key, value) print(f"Replacing {key} with {value}") modified_lines.append(line) print(modified_lines) return modified_lines