Abobasnik's picture
Update app.py
f3091e5 verified
import gradio as gr
from faster_whisper import WhisperModel
import yt_dlp
import os
import subprocess
def process_video(url):
try:
# Очистка файлов перед новым запуском
for f in ["in.mp4", "out.mp4", "s.srt", "audio.mp3"]:
if os.path.exists(f): os.remove(f)
# 1. Скачивание (добавили настройки совместимости)
ydl_opts = {
'format': 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best',
'outtmpl': 'in.mp4',
'overwrites': True,
}
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
ydl.download([url])
# Извлекаем звук для нейросети
subprocess.run(['ffmpeg', '-i', 'in.mp4', '-q:a', '0', '-map', 'a', 'audio.mp3', '-y'])
# 2. Нейросеть (модель tiny — самая быстрая)
model = WhisperModel("tiny", device="cpu", compute_type="int8")
segments, _ = model.transcribe("audio.mp3")
with open("s.srt", "w", encoding="utf-8") as f:
for i, seg in enumerate(segments):
f.write(f"{i+1}\n{int(seg.start//3600):02d}:{int((seg.start%3600)//60):02d}:{int(seg.start%60):02d},000 --> {int(seg.end//3600):02d}:{int((seg.end%3600)//60):02d}:{int(seg.end%60):02d},000\n{seg.text.strip()}\n\n")
# 3. Наложение субтитров
result = subprocess.run([
'ffmpeg', '-i', 'in.mp4', '-vf', 'subtitles=s.srt',
'-c:v', 'libx264', '-preset', 'ultrafast', '-y', 'out.mp4'
], capture_output=True, text=True)
if not os.path.exists("out.mp4"):
return f"Ошибка FFmpeg: {result.stderr}"
return "out.mp4"
except Exception as e:
return f"Критическая ошибка: {str(e)}"
demo = gr.Interface(
fn=process_video,
inputs=gr.Textbox(label="Вставь ссылку на YouTube"),
outputs=gr.Video(label="Результат"),
title="Переводчик видео"
)
demo.launch()