File size: 3,069 Bytes
e698260
f36ca6d
e698260
 
 
fb7a690
e698260
 
 
 
 
 
3273372
46226f2
a497221
46226f2
a497221
46226f2
a497221
 
 
3273372
 
 
 
 
 
 
11bd532
e698260
 
 
a497221
e698260
a497221
 
 
fb7a690
a497221
e698260
a497221
e698260
a497221
e698260
eb6aaad
581b947
eb6aaad
e698260
 
 
581b947
e698260
 
 
581b947
e698260
 
 
581b947
e698260
 
581b947
 
e698260
 
 
581b947
e698260
 
 
 
 
 
fb7a690
 
 
 
 
 
 
 
 
 
 
 
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
from ui import *
from file_name import *
from diarization import start_diarization
from transcribe import start_transcribe
from video_tool import convert_video_to_audio, add_subtitle_to_video
from datetime import datetime
import gradio as gr
import re
import os
import utils


def prepare_video_subtitle(input_file, start_time, end_time, progress=gr.Progress()):
    if input_file is None or not os.path.exists(input_file):
        raise gr.Error(ui_lang["input_video_warning"])
    if validate_time_format(start_time) is False:
        raise gr.Error(ui_lang["start_time_warning"])
    if validate_time_format(end_time) is False:
        raise gr.Error(ui_lang["end_time_warning"])
    if (check_if_time_invalid(start_time, end_time)):
        raise gr.Error(ui_lang["time_invalid"])
    # Add subtitle to video
    progress(0.8, desc=ui_lang["progress_add_subtitle"])
    add_subtitle_to_video(input_file, base_subtitle_file,
                          video_subtitle_file, start_time, end_time)
    return video_subtitle_file


def prepare_input(input_file, start_time, end_time, lang, model_size, progress=gr.Progress()):
    gr.Info(ui_lang["progress_starting_process"])

    if input_file is None or not os.path.exists(input_file):
        raise gr.Error(ui_lang["input_video_warning"])
    if validate_time_format(start_time) is False:
        raise gr.Error(ui_lang["start_time_warning"])
    if validate_time_format(end_time) is False:
        raise gr.Error(ui_lang["end_time_warning"])
    if (check_if_time_invalid(start_time, end_time)):
        raise gr.Error(ui_lang["time_invalid"])
    if lang is None:
        raise gr.Error(ui_lang["lang_radio_warning"])
    if model_size is None:
        raise gr.Error(ui_lang["model_dropdown_warning"])

    print(f"SOURCE: {input_file}")
    print(f"AUDIO FILE: {audio_file}")

    # Convert video to audio
    progress(0.2, desc=ui_lang["progress_preparing_video"])
    convert_video_to_audio(
        input_file, audio_file, start_time, end_time)

    # Start diarization
    progress(0.4, desc=ui_lang["progress_acquiring_diarization"])
    start_diarization(audio_file)

    # Start transcribing
    progress(0.6, desc=ui_lang["progress_transcribing_audio"])
    start_transcribe(input_file, lang, model_size, progress)

    # Return video file link, transcribe string, transcribe.txt, subtitle.txt
    transcribe_txt_list, _ = utils.read_transcribe_subtitle_file(
        input_file, False)
    transcribe_txt = "\n".join(transcribe_txt_list)
    return [
        transcribe_txt,
        [transcribe_file, subtitle_file]
    ]


def validate_time_format(input_string):
    pattern = re.compile(r'^\d{2}:\d{2}:\d{2}$')
    return pattern.match(input_string) is not None


def check_if_time_invalid(start_time, end_time):
    start = get_total_seconds(start_time)
    end = get_total_seconds(end_time)
    return start >= end


def get_total_seconds(time_string):
    hours, minutes, seconds = map(int, time_string.split(":"))
    total_seconds = hours * 3600 + minutes * 60 + seconds
    return total_seconds