whisper_transcribe / set_up.py
chompionsawelo's picture
change warning to error
a497221
raw
history blame
3.07 kB
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