File size: 4,618 Bytes
e698260
f36ca6d
e698260
 
 
 
 
 
 
 
 
 
 
 
 
b48459a
e698260
 
 
e9417bc
 
1c1c8cf
 
e9417bc
e698260
 
e9417bc
 
e698260
 
 
 
 
f6d4b17
e698260
795983d
e698260
 
 
 
 
 
 
 
 
 
f6d4b17
b48459a
e698260
 
 
b48459a
 
e698260
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1c1c8cf
 
e698260
 
 
 
e9417bc
 
 
 
e698260
 
 
 
 
 
 
e9417bc
e698260
 
 
 
 
 
 
 
 
 
 
 
 
 
581b947
e698260
 
 
 
581b947
 
e698260
 
 
581b947
e698260
 
 
 
9002374
e698260
 
581b947
 
e698260
 
 
581b947
e698260
581b947
e698260
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
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, base_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