-
-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathrecord.py
More file actions
207 lines (165 loc) · 8.59 KB
/
record.py
File metadata and controls
207 lines (165 loc) · 8.59 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#!/usr/bin/env python3
from fastapi import FastAPI, Request, Form, HTTPException
from fastapi.responses import HTMLResponse, JSONResponse
from fastapi.templating import Jinja2Templates
import whisper
import subprocess
import random
import os
import logging
import glob
import argparse
import threading
import time
import uvicorn
# Configurer la journalisation
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
app = FastAPI()
templates = Jinja2Templates(directory="templates")
model = whisper.load_model("medium")
# Liste pour suivre les processus
processes = []
# Fonction de surveillance des processus
def monitor_processes():
while True:
current_time = time.time()
for process_info in processes:
process, start_time = process_info
if current_time - start_time > 3600: # 3600 secondes = 1 heure
logger.info("Killing process with PID: %d due to timeout", process.pid)
process.kill()
processes.remove(process_info)
time.sleep(60) # Vérifier toutes les minutes
# Démarrer le thread de surveillance
monitor_thread = threading.Thread(target=monitor_processes, daemon=True)
monitor_thread.start()
@app.get("/", response_class=HTMLResponse)
async def index(request: Request, room: str = "", record: str = ""):
logger.info("Serving index page with room: %s (%s)", room, record)
return templates.TemplateResponse("index.html", {"request": request, "room": room, "record": record})
@app.api_route("/rec", methods=["GET", "POST"])
async def start_recording(request: Request, room: str = Form(None), record: str = Form(None)):
room = room or request.query_params.get("room")
record = record or request.query_params.get("record")
if not room or not record:
raise HTTPException(status_code=400, detail="Room and record parameters must not be empty")
logger.info("Starting recording for room: %s with record ID: %s", room, record)
# Créer un pipe pour rediriger les logs de publish.py
read_pipe, write_pipe = os.pipe()
# Lancer l'enregistrement audio en arrière-plan avec les logs redirigés vers le pipe
process = subprocess.Popen(["python3", "publish.py", "--room", room, "--record", record, "--novideo"], stdout=write_pipe, stderr=write_pipe)
logger.info("Started publish.py process with PID: %d", process.pid)
# Fermer le côté écriture du pipe dans le processus parent
os.close(write_pipe)
# Ajouter le processus à la liste avec l'heure de début
processes.append((process, time.time()))
# Afficher un bouton pour ouvrir la nouvelle page de visioconférence
return templates.TemplateResponse("recording.html", {"request": request, "room": room, "record": record, "process_pid": process.pid})
@app.post("/stop")
async def stop_recording(record: str = Form(...), process_pid: int = Form(...), language: str = Form(...)):
logger.info("Stopping recording for record ID: %s with process PID: %d", record, process_pid)
# Arrêter le processus d'enregistrement
process = subprocess.Popen(["kill", str(process_pid)])
process.wait()
logger.info("Stopped publish.py process with PID: %d", process_pid)
# Trouver le fichier audio correspondant
audio_files = glob.glob(f"{record}_*_audio.ts")
if not audio_files:
logger.error("No audio file found for record ID: %s", record)
return {"error": f"No audio file found for record ID: {record}"}
audio_file = audio_files[0]
logger.info("Transcribing audio file: %s", audio_file)
try:
speech = model.transcribe(audio_file, language=language)['text']
logger.info("Transcription completed for record ID: %s", record)
except Exception as e:
logger.error("Failed to transcribe audio file: %s", str(e))
return {"error": f"Failed to transcribe audio file: {str(e)}"}
# Écrire la transcription dans un fichier texte
transcript_file = f"stt/{record}_speech.txt"
with open(transcript_file, "w") as f:
f.write(speech)
logger.info("Transcription saved to: %s", transcript_file)
# Supprimer le fichier audio
os.remove(audio_file)
logger.info("Audio file %s removed.", audio_file)
return {"transcription": speech}
@app.get("/stt")
async def get_transcription(id: str):
transcript_file = f"stt/{id}_speech.txt"
if not os.path.exists(transcript_file):
logger.error("No transcription file found for record ID: %s", id)
return JSONResponse(status_code=404, content={"error": f"No transcription file found for record ID: {id}"})
with open(transcript_file, "r") as f:
transcription = f.read()
# Ajouter le fichier à IPFS
try:
logger.info(f"Adding file to IPFS: {transcript_file}")
result = subprocess.run(["ipfs", "add", transcript_file], capture_output=True, text=True)
cid = result.stdout.split()[1]
logger.info("Added file to IPFS: %s with CID: %s", transcript_file, cid)
except Exception as e:
logger.error("Failed to add file to IPFS: %s", str(e))
return JSONResponse(status_code=500, content={"error": f"Failed to add file to IPFS: {str(e)}"})
logger.info("Returning transcription and CID for record ID: %s", id)
return {"transcription": transcription, "cid": cid}
def start_recording_cli(room, record):
logger.info("Starting recording for room: %s with record ID: %s", room, record)
# Créer un pipe pour rediriger les logs de publish.py
read_pipe, write_pipe = os.pipe()
# Lancer l'enregistrement audio en arrière-plan avec les logs redirigés vers le pipe
process = subprocess.Popen(["python3", "publish.py", "--room", room, "--record", record, "--novideo"], stdout=write_pipe, stderr=write_pipe)
logger.info("Started publish.py process with PID: %d", process.pid)
# Fermer le côté écriture du pipe dans le processus parent
os.close(write_pipe)
# Ajouter le processus à la liste avec l'heure de début
processes.append((process, time.time()))
return process.pid
def stop_recording_cli(record, process_pid, language):
logger.info("Stopping recording for record ID: %s with process PID: %d", record, process_pid)
# Arrêter le processus d'enregistrement
process = subprocess.Popen(["kill", str(process_pid)])
process.wait()
logger.info("Stopped publish.py process with PID: %d", process_pid)
# Trouver le fichier audio correspondant
audio_files = glob.glob(f"{record}_*_audio.ts")
if not audio_files:
logger.error("No audio file found for record ID: %s", record)
return {"error": f"No audio file found for record ID: {record}"}
audio_file = audio_files[0]
logger.info("Transcribing audio file: %s", audio_file)
try:
speech = model.transcribe(audio_file, language=language)['text']
logger.info("Transcription completed for record ID: %s", record)
except Exception as e:
logger.error("Failed to transcribe audio file: %s", str(e))
return {"error": f"Failed to transcribe audio file: {str(e)}"}
# Écrire la transcription dans un fichier texte
transcript_file = f"stt/{record}_speech.txt"
with open(transcript_file, "w") as f:
f.write(speech)
logger.info("Transcription saved to: %s", transcript_file)
# Supprimer le fichier audio
os.remove(audio_file)
logger.info("Audio file %s removed.", audio_file)
return {"transcription": speech}
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Démarrer le serveur FastAPI avec des paramètres personnalisés.")
parser.add_argument("--host", type=str, default="0.0.0.0", help="Adresse hôte pour le serveur FastAPI.")
parser.add_argument("--port", type=int, default=9000, help="Port pour le serveur FastAPI.")
parser.add_argument("--room", type=str, help="Room name for the recording session.")
parser.add_argument("--record", type=str, help="Record ID for the session.")
parser.add_argument("--stop", action="store_true", help="Stop the recording.")
parser.add_argument("--pid", type=int, help="Process PID to stop.")
parser.add_argument("--language", type=str, default="en", help="Language for transcription.")
args = parser.parse_args()
if args.room and args.record and not args.stop:
pid = start_recording_cli(args.room, args.record)
print(f"Recording started with PID: {pid}")
elif args.stop and args.pid and args.record:
result = stop_recording_cli(args.record, args.pid, args.language)
print(result)
else:
logger.info("Starting FastAPI server")
uvicorn.run(app, host=args.host, port=args.port)