File size: 4,296 Bytes
8b4831d
 
275e48a
8b4831d
1768850
3219df3
2de3a57
275e48a
a6be743
7848193
e00b158
f7fa4ee
e00b158
8b4831d
5a957c1
 
 
2de3a57
1768850
 
8b4831d
3219df3
 
 
8b4831d
12ea292
275e48a
740becd
 
 
 
 
e00b158
275e48a
740becd
275e48a
2de3a57
a0e508d
e00b158
 
a0e508d
 
 
 
 
 
 
 
 
 
 
 
 
 
2de3a57
9fd2740
e00b158
a0e508d
 
 
 
 
 
 
 
 
 
074efe8
2de3a57
bc5e2dc
740becd
9fd2740
 
 
fdbd781
 
 
 
740becd
fdbd781
 
740becd
 
 
 
f7fa4ee
 
740becd
 
275e48a
e00b158
 
 
2de3a57
8b4831d
 
 
 
1768850
 
8b4831d
3219df3
1768850
558e6f8
8b4831d
 
2de3a57
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
import gradio as gr
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
from subprocess import run
from faster_whisper import WhisperModel
import json
import tempfile
import os
import ffmpeg
from zipfile import ZipFile
import stat
import uuid
import subprocess


ZipFile("ffmpeg.zip").extractall()
st = os.stat('ffmpeg')
os.chmod('ffmpeg', st.st_mode | stat.S_IEXEC)

with open('language_codes.json', 'r') as f:
    lang_codes = json.load(f)

tokenizer = AutoTokenizer.from_pretrained("facebook/nllb-200-distilled-600M")
model = AutoModelForSeq2SeqLM.from_pretrained("facebook/nllb-200-distilled-600M")
whisper_model = WhisperModel("large-v2", device="cuda", compute_type="float16")

def process_video(Video, target_language):
    print("Iniciando process_video")

    print("Checking FFmpeg availability...")
    run(["ffmpeg", "-version"])

    
    audio_file = f"{uuid.uuid4()}.wav"
    run(["ffmpeg", "-i", Video, audio_file])
    
    print("Iniciando transcrição com Whisper")
    segments, _ = whisper_model.transcribe(audio_file, beam_size=5)
    segments = list(segments)
    transcript_file = f"{uuid.uuid4()}.srt"
    with open(transcript_file, "w", encoding="utf-8") as f:
        counter = 1
        for segment in segments:
            start_minutes = int(segment.start // 60)
            start_seconds = int(segment.start % 60)
            start_milliseconds = int((segment.start - int(segment.start)) * 1000)
            end_minutes = int(segment.end // 60)
            end_seconds = int(segment.end % 60)
            end_milliseconds = int((segment.end - int(segment.end)) * 1000)
            formatted_start = f"{start_minutes:02d}:{start_seconds:02d},{start_milliseconds:03d}"
            formatted_end = f"{end_minutes:02d}:{end_seconds:02d},{end_milliseconds:03d}"
            f.write(f"{counter}\n")
            f.write(f"{formatted_start} --> {formatted_end}\n")
            f.write(f"{segment.text}\n\n")
            counter += 1
    flores_code = lang_codes.get(target_language, "eng_Latn")
    translated_file = f"{uuid.uuid4()}.srt" 
    with open(transcript_file, "r", encoding="utf-8") as infile, open(translated_file, "w", encoding="utf-8") as outfile:
        for line in infile:
            if line.strip().isnumeric() or "-->" in line:
                outfile.write(line)
            elif line.strip() != "":
                inputs = tokenizer(line.strip(), return_tensors="pt")
                translated_tokens = model.generate(**inputs, forced_bos_token_id=tokenizer.lang_code_to_id[flores_code], max_length=100)
                translated_text = tokenizer.batch_decode(translated_tokens, skip_special_tokens=True)[0]
                outfile.write(translated_text + "\n")
            else:
                outfile.write("\n")

    output_video = "output_video.mp4"
    # Debugging: Validate FFmpeg command for subtitle embedding
    print("Validating FFmpeg command for subtitle embedding...")
    print(f"Translated SRT file: {translated_file}")
    with open(translated_file, 'r', encoding='utf-8') as f:
        print(f"First few lines of translated SRT: {f.readlines()[:10]}")
    if os.path.exists(translated_file):
        print(f"{translated_file} exists.")
    else:
        print(f"{translated_file} does not exist.")
    try:
        translated_file_abs_path = os.path.abspath(translated_file)
        result = subprocess.run(["ffmpeg", "-i", Video, "-vf", f"subtitles={translated_file_abs_path}", output_video], capture_output=True, text=True)
        if result.returncode == 0:
            print("FFmpeg executed successfully.")
        else:
            print(f"FFmpeg failed with return code {result.returncode}.")
            print("Stdout:", result.stdout)
            print("Stderr:", result.stderr)
    except Exception as e:
        print(f"Exception occurred: {e}")
    print("process_video concluído com sucesso")
    os.unlink(audio_file)
    os.unlink(transcript_file)
    os.unlink(translated_file)
    return output_video

iface = gr.Interface(
    fn=process_video,
    inputs=[
        gr.Video(),
        gr.Dropdown(choices=list(lang_codes.keys()), label="Target Language for Dubbing", value="English"),
    ],
    outputs=gr.Video(),
    live=False,
    title="VIDEO TRANSCRIPTION AND TRANSLATION"
)

iface.launch()