Spaces:
Running
Running
Ajout des paramètres Avancer
Browse files- data/generate_subtitles.py +20 -28
data/generate_subtitles.py
CHANGED
|
@@ -1,50 +1,42 @@
|
|
| 1 |
# Fichier: /data/generate_subtitles.py
|
| 2 |
import sys
|
| 3 |
-
import
|
| 4 |
-
import torch
|
| 5 |
import json
|
| 6 |
import os
|
| 7 |
|
| 8 |
-
def format_timestamp(seconds: float):
|
| 9 |
-
"""Formatage SRT standard (00:00:00,000)"""
|
| 10 |
-
td = float(seconds)
|
| 11 |
-
hours = int(td // 3600)
|
| 12 |
-
minutes = int((td % 3600) // 60)
|
| 13 |
-
secs = int(td % 60)
|
| 14 |
-
msecs = int((td - int(td)) * 1000)
|
| 15 |
-
return f"{hours:02d}:{minutes:02d}:{secs:02d},{msecs:03d}"
|
| 16 |
-
|
| 17 |
def run():
|
| 18 |
-
# On attend
|
| 19 |
-
if len(sys.argv) <
|
| 20 |
-
error = {"status": "error", "message": "Usage: python3 generate_subtitles.py <input_audio> <output_srt>"}
|
| 21 |
print(json.dumps(error))
|
| 22 |
sys.exit(1)
|
| 23 |
|
| 24 |
input_audio = sys.argv[1]
|
| 25 |
output_srt = sys.argv[2]
|
|
|
|
| 26 |
|
| 27 |
try:
|
| 28 |
-
# Chargement
|
| 29 |
-
model =
|
| 30 |
|
| 31 |
-
# Transcription avec
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
| 41 |
|
| 42 |
# Réponse JSON pour n8n
|
| 43 |
response = {
|
| 44 |
"status": "success",
|
| 45 |
-
"input_file": input_audio,
|
| 46 |
"output_file": output_srt,
|
| 47 |
-
"
|
| 48 |
}
|
| 49 |
print(json.dumps(response))
|
| 50 |
|
|
|
|
| 1 |
# Fichier: /data/generate_subtitles.py
|
| 2 |
import sys
|
| 3 |
+
import stable_whisper
|
|
|
|
| 4 |
import json
|
| 5 |
import os
|
| 6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
def run():
|
| 8 |
+
# On attend 3 arguments : l'audio, le SRT de sortie, et le TEXTE ORIGINAL
|
| 9 |
+
if len(sys.argv) < 4:
|
| 10 |
+
error = {"status": "error", "message": "Usage: python3 generate_subtitles.py <input_audio> <output_srt> <original_text>"}
|
| 11 |
print(json.dumps(error))
|
| 12 |
sys.exit(1)
|
| 13 |
|
| 14 |
input_audio = sys.argv[1]
|
| 15 |
output_srt = sys.argv[2]
|
| 16 |
+
original_text = sys.argv[3]
|
| 17 |
|
| 18 |
try:
|
| 19 |
+
# 1. Chargement de stable-whisper (plus précis pour le timing)
|
| 20 |
+
model = stable_whisper.load_model("base")
|
| 21 |
|
| 22 |
+
# 2. Transcription forcée avec le texte original (initial_prompt)
|
| 23 |
+
# Cela empêche Whisper d'inventer des mots si Tacotron prononce mal
|
| 24 |
+
result = model.transcribe(
|
| 25 |
+
input_audio,
|
| 26 |
+
language="fr",
|
| 27 |
+
initial_prompt=original_text,
|
| 28 |
+
vad=True # Détecte le silence pour s'arrêter pile quand l'audio finit
|
| 29 |
+
)
|
| 30 |
+
|
| 31 |
+
# 3. Génération du SRT avec balises Karaoké (word_level=True)
|
| 32 |
+
# C'est ce paramètre qui permet au bleu de défiler mot par mot
|
| 33 |
+
result.to_srt_vtt(output_srt, word_level=True)
|
| 34 |
|
| 35 |
# Réponse JSON pour n8n
|
| 36 |
response = {
|
| 37 |
"status": "success",
|
|
|
|
| 38 |
"output_file": output_srt,
|
| 39 |
+
"text_detected": result.text
|
| 40 |
}
|
| 41 |
print(json.dumps(response))
|
| 42 |
|