-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
93 lines (68 loc) · 2.35 KB
/
Copy pathapp.py
File metadata and controls
93 lines (68 loc) · 2.35 KB
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
import os
import uuid
import subprocess
import whisper
from fastapi import FastAPI, UploadFile, File
from fastapi.responses import FileResponse, HTMLResponse
from fastapi.staticfiles import StaticFiles
UPLOAD_DIR = "uploads"
OUTPUT_DIR = "outputs"
os.makedirs(UPLOAD_DIR, exist_ok=True)
os.makedirs(OUTPUT_DIR, exist_ok=True)
app = FastAPI()
app.mount("/static", StaticFiles(directory="static"), name="static")
@app.get("/", response_class=HTMLResponse)
def home():
with open("static/index.html", "r", encoding="utf-8") as f:
return f.read()
# 🔹 Load Whisper model ONCE
model = whisper.load_model("base")
# 🔹 Custom SRT writer (VERSION SAFE)
def format_timestamp(seconds: float) -> str:
millis = int(seconds * 1000)
hours = millis // 3_600_000
minutes = (millis % 3_600_000) // 60_000
seconds = (millis % 60_000) // 1000
ms = millis % 1000
return f"{hours:02}:{minutes:02}:{seconds:02},{ms:03}"
def write_srt_file(segments, file):
for i, seg in enumerate(segments, start=1):
file.write(f"{i}\n")
file.write(
f"{format_timestamp(seg['start'])} --> "
f"{format_timestamp(seg['end'])}\n"
)
file.write(seg["text"].strip() + "\n\n")
@app.post("/upload")
async def upload_video(file: UploadFile = File(...)):
uid = str(uuid.uuid4())
video_path = f"{UPLOAD_DIR}/{uid}_{file.filename}"
audio_path = f"{UPLOAD_DIR}/{uid}.wav"
srt_path = f"{OUTPUT_DIR}/{uid}.srt"
# Save video
with open(video_path, "wb") as f:
f.write(await file.read())
# Video → audio
subprocess.run([
"ffmpeg", "-y", "-i", video_path,
"-ar", "16000", "-ac", "1",
audio_path
], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
# Whisper transcription (English only)
result = model.transcribe(audio_path, language="en")
# Write SRT
with open(srt_path, "w", encoding="utf-8") as f:
write_srt_file(result["segments"], f)
# Cleanup
os.remove(video_path)
os.remove(audio_path)
return {"id": uid}
@app.get("/download/{uid}")
def download(uid: str):
for f in os.listdir(OUTPUT_DIR):
if f.startswith(uid) and f.endswith(".srt"):
return FileResponse(
f"{OUTPUT_DIR}/{f}",
filename="subtitles.srt"
)
return {"error": "Subtitle not found"}