Spaces:
Runtime error
Runtime error
from ui.ui_component import * | |
from tool.file_name import * | |
from main.diarization import start_diarization | |
from main.transcribe import start_transcribe | |
from tool.ffmpeg_tool import * | |
import gradio as gr | |
import re | |
import os | |
import tool.text_file_tool as text_file_tool | |
def prepare_input(input_file, start_time, end_time, lang, model_size, progress=gr.Progress()): | |
gr.Info(current_ui_lang["progress_starting_process"]) | |
check_input_video_settings(input_file, start_time, end_time) | |
if lang is None: | |
raise gr.Error(current_ui_lang["lang_radio_warning"]) | |
if model_size is None: | |
raise gr.Error(current_ui_lang["model_dropdown_warning"]) | |
print(f"SOURCE: {input_file}") | |
# Convert video to audio | |
progress(0.2, desc=current_ui_lang["progress_preparing_video"]) | |
convert_video_to_audio(input_file, start_time, end_time) | |
# Start diarization | |
progress(0.4, desc=current_ui_lang["progress_acquiring_diarization"]) | |
start_diarization(dir_cut_audio_file) | |
# Start transcribing | |
progress(0.6, desc=current_ui_lang["progress_transcribing_audio"]) | |
start_transcribe(lang, model_size, progress) | |
# Cutting video | |
progress(0.8, desc=current_ui_lang["progress_cutting_video"]) | |
cut_video(input_file, start_time, end_time) | |
# Get complete transcribe into string | |
transcribe_txt_list, _ = text_file_tool.read_transcribe_subtitle_file( | |
False) | |
transcribe_txt = "\n".join(transcribe_txt_list) | |
# Return to output textbox, output files, and output video | |
return [ | |
transcribe_txt, | |
[dir_adjusted_transcribe_file, dir_adjusted_subtitle_file], | |
[dir_cut_video_file, dir_adjusted_subtitle_file] | |
] | |
def prepare_video_subtitle(input_file, start_time, end_time): | |
check_input_video_settings(input_file, start_time, end_time) | |
gr.Info(current_ui_lang["progress_add_subtitle"]) | |
# Add subtitle to video | |
add_subtitle_to_video() | |
# Return to output files | |
return [dir_base_transcribe_file, dir_base_subtitle_file, dir_video_subtitle_file] | |
def check_input_video_settings(input_file, start_time, end_time): | |
if input_file is None or not os.path.exists(input_file): | |
raise gr.Error(current_ui_lang["input_video_warning"]) | |
if validate_time_format(start_time) is False: | |
raise gr.Error(current_ui_lang["start_time_warning"]) | |
if validate_time_format(end_time) is False: | |
raise gr.Error(current_ui_lang["end_time_warning"]) | |
if (check_if_time_invalid(start_time, end_time)): | |
raise gr.Error(current_ui_lang["time_invalid"]) | |
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 | |