File size: 12,358 Bytes
ae8b600
e5e756a
 
ae8b600
e5e756a
ae8b600
 
 
cc826a1
e5e756a
ae8b600
 
 
 
 
 
 
 
 
 
 
 
 
 
e5e756a
ae8b600
 
 
 
 
 
cc826a1
ae8b600
 
 
 
 
 
e5e756a
 
 
 
 
 
 
 
 
 
 
ae8b600
 
e5e756a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cc826a1
e5e756a
 
 
 
 
 
 
ae8b600
 
e5e756a
 
 
 
 
 
 
 
 
 
 
 
 
 
ae8b600
e5e756a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ae8b600
 
 
e5e756a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cc826a1
e5e756a
 
 
 
 
 
 
ae8b600
e5e756a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
import base64
import tempfile
from pathlib import Path

from fastapi import APIRouter, HTTPException, UploadFile, Query
from pydantic import BaseModel

from .core import get_video_audio_service
from plugins.audio.asr_drivers import get_asr_registry
from app.plugins.run_log import get_run_log_service, RunStatus

router = APIRouter()
plugin = None


def set_plugin_instance(plugin_instance):
    global plugin
    plugin = plugin_instance


class VideoBase64Request(BaseModel):
    video_base64: str
    suffix: str = ".mp4"
    language: str | None = None
    provider: str | None = None


@router.get("/status")
async def get_status():
    if plugin is None:
        return {
            "name": "video",
            "enabled": False,
            "message": "插件未加载",
        }
    return plugin.get_status()


@router.post("/upload/transcribe")
async def transcribe_video_upload(
    file: UploadFile,
    language: str | None = None,
    provider: str | None = None,
    wait: bool = Query(False, description="是否同步等待转录完成"),
):
    """上传视频文件,提取音频并转录

    默认异步返回 run_id,设置 wait=true 同步等待结果。
    """
    if plugin is None or not plugin.enabled:
        raise HTTPException(status_code=400, detail="插件未启用")

    # 获取 ASR registry
    registry = get_asr_registry()
    provider_name = provider or registry.get_default_provider()
    driver = registry.get_driver(provider_name)
    if driver is None:
        raise HTTPException(status_code=400, detail=f"未知的 ASR provider: {provider_name}")

    # 检查 provider 可用性
    availability = driver.check_availability()
    if availability.value != "available":
        raise HTTPException(
            status_code=400,
            detail=f"ASR provider {provider_name} 不可用: {availability.value}",
        )

    # 创建 run
    run_service = get_run_log_service()
    run = run_service.create_run("video")

    # 写入上传事件
    run_service.add_event(
        run_id=run.run_id,
        stage="upload",
        message=f"视频文件已上传: {file.filename}",
        detail=f"provider={provider_name}, language={language}",
    )

    # 保存上传文件到临时目录
    content = await file.read()
    suffix = "." + file.filename.rsplit(".", 1)[-1] if "." in file.filename else ".mp4"
    with tempfile.NamedTemporaryFile(delete=False, suffix=suffix) as tmp_file:
        tmp_file.write(content)
        tmp_path = tmp_file.name

    try:
        # 写入音频提取开始事件
        run_service.add_event(
            run_id=run.run_id,
            stage="extract_audio",
            message="开始从视频提取音频",
        )

        # 提取音频
        video_service = get_video_audio_service()
        extraction = video_service.extract_audio(tmp_path)

        if not extraction["success"]:
            # 写入提取失败事件
            run_service.add_event(
                run_id=run.run_id,
                stage="extract_audio",
                message=f"音频提取失败: {extraction['error']}",
                level="error",
            )
            run_service.finish_run(
                run_id=run.run_id,
                status=RunStatus.FAILED,
                error=extraction["error"],
            )
            raise HTTPException(status_code=400, detail=extraction["error"])

        # 写入音频提取完成事件
        run_service.add_event(
            run_id=run.run_id,
            stage="extract_audio",
            message="音频提取完成",
        )

        # 写入模型探测事件
        run_service.add_event(
            run_id=run.run_id,
            stage="model_probe",
            message=f"模型探测: {driver.model_id}",
            detail=f"provider={provider_name}, status={availability.value}",
        )

        # 写入转录开始事件
        run_service.add_event(
            run_id=run.run_id,
            stage="transcribe",
            message="开始转录",
        )

        # 执行转录
        audio_path = extraction["audio_path"]
        result = driver.transcribe(audio_path, language)

        # 清理提取的音频文件
        Path(audio_path).unlink(missing_ok=True)

        if result.success:
            # 写入转录完成事件
            run_service.add_event(
                run_id=run.run_id,
                stage="transcribe",
                message=f"转录完成: {len(result.text)} 字符",
                detail=f"timings={result.timings}",
            )

            # 完成 run
            run_service.finish_run(
                run_id=run.run_id,
                status=RunStatus.SUCCEEDED,
                result={
                    "text": result.text,
                    "segments": result.segments,
                    "provider": result.provider,
                    "model": result.model,
                    "timings": result.timings,
                },
            )
        else:
            # 写入转录失败事件
            run_service.add_event(
                run_id=run.run_id,
                stage="transcribe",
                message=f"转录失败: {result.error}",
                level="error",
            )

            # 完成 run(失败)
            run_service.finish_run(
                run_id=run.run_id,
                status=RunStatus.FAILED,
                error=result.error,
            )

        # 如果同步等待,直接返回结果
        if wait:
            return {
                "run_id": run.run_id,
                "status": "succeeded" if result.success else "failed",
                "provider": result.provider,
                "model": result.model,
                "text": result.text,
                "segments": result.segments,
                "timings": result.timings,
                "error": result.error,
            }

        # 异步返回 run_id
        return {
            "run_id": run.run_id,
            "status": "running",
            "provider": provider_name,
            "model": driver.model_id,
        }

    except HTTPException:
        raise
    except Exception as e:
        # 写入异常事件
        run_service.add_event(
            run_id=run.run_id,
            stage="transcribe",
            message=f"转录异常: {str(e)}",
            level="error",
        )
        run_service.finish_run(
            run_id=run.run_id,
            status=RunStatus.FAILED,
            error=str(e),
        )
        raise HTTPException(status_code=500, detail=f"转录失败: {str(e)}")
    finally:
        # 清理临时文件
        Path(tmp_path).unlink(missing_ok=True)


@router.post("/video-base64/transcribe")
async def transcribe_video_base64(request: VideoBase64Request):
    """Base64 视频转录(兼容旧接口)"""
    if plugin is None or not plugin.enabled:
        raise HTTPException(status_code=400, detail="插件未启用")

    # 获取 ASR registry
    registry = get_asr_registry()
    provider_name = request.provider or registry.get_default_provider()
    driver = registry.get_driver(provider_name)
    if driver is None:
        raise HTTPException(status_code=400, detail=f"未知的 ASR provider: {provider_name}")

    # 检查 provider 可用性
    availability = driver.check_availability()
    if availability.value != "available":
        raise HTTPException(
            status_code=400,
            detail=f"ASR provider {provider_name} 不可用: {availability.value}",
        )

    # 创建 run
    run_service = get_run_log_service()
    run = run_service.create_run("video")

    # 写入上传事件
    run_service.add_event(
        run_id=run.run_id,
        stage="upload",
        message="Base64 视频已接收",
        detail=f"provider={provider_name}, language={request.language}",
    )

    # 解码 Base64 并保存到临时文件
    try:
        video_bytes = base64.b64decode(request.video_base64)
    except Exception as e:
        run_service.finish_run(run_id=run.run_id, status=RunStatus.FAILED, error=f"Base64 解码失败: {e}")
        raise HTTPException(status_code=400, detail=f"Base64 解码失败: {e}")

    suffix = request.suffix if request.suffix.startswith(".") else f".{request.suffix}"
    with tempfile.NamedTemporaryFile(delete=False, suffix=suffix) as tmp_file:
        tmp_file.write(video_bytes)
        tmp_path = tmp_file.name

    try:
        # 写入音频提取开始事件
        run_service.add_event(
            run_id=run.run_id,
            stage="extract_audio",
            message="开始从视频提取音频",
        )

        # 提取音频
        video_service = get_video_audio_service()
        extraction = video_service.extract_audio(tmp_path)

        if not extraction["success"]:
            # 写入提取失败事件
            run_service.add_event(
                run_id=run.run_id,
                stage="extract_audio",
                message=f"音频提取失败: {extraction['error']}",
                level="error",
            )
            run_service.finish_run(
                run_id=run.run_id,
                status=RunStatus.FAILED,
                error=extraction["error"],
            )
            return {
                "run_id": run.run_id,
                "status": "failed",
                "error": extraction["error"],
            }

        # 写入音频提取完成事件
        run_service.add_event(
            run_id=run.run_id,
            stage="extract_audio",
            message="音频提取完成",
        )

        # 写入模型探测事件
        run_service.add_event(
            run_id=run.run_id,
            stage="model_probe",
            message=f"模型探测: {driver.model_id}",
            detail=f"provider={provider_name}, status={availability.value}",
        )

        # 写入转录开始事件
        run_service.add_event(
            run_id=run.run_id,
            stage="transcribe",
            message="开始转录",
        )

        # 执行转录
        audio_path = extraction["audio_path"]
        result = driver.transcribe(audio_path, request.language)

        # 清理提取的音频文件
        Path(audio_path).unlink(missing_ok=True)

        if result.success:
            # 写入转录完成事件
            run_service.add_event(
                run_id=run.run_id,
                stage="transcribe",
                message=f"转录完成: {len(result.text)} 字符",
                detail=f"timings={result.timings}",
            )

            # 完成 run
            run_service.finish_run(
                run_id=run.run_id,
                status=RunStatus.SUCCEEDED,
                result={
                    "text": result.text,
                    "segments": result.segments,
                    "provider": result.provider,
                    "model": result.model,
                    "timings": result.timings,
                },
            )
        else:
            # 写入转录失败事件
            run_service.add_event(
                run_id=run.run_id,
                stage="transcribe",
                message=f"转录失败: {result.error}",
                level="error",
            )

            # 完成 run(失败)
            run_service.finish_run(
                run_id=run.run_id,
                status=RunStatus.FAILED,
                error=result.error,
            )

        return {
            "run_id": run.run_id,
            "status": "succeeded" if result.success else "failed",
            "provider": result.provider,
            "model": result.model,
            "text": result.text,
            "segments": result.segments,
            "timings": result.timings,
            "error": result.error,
        }

    except Exception as e:
        # 写入异常事件
        run_service.add_event(
            run_id=run.run_id,
            stage="transcribe",
            message=f"转录异常: {str(e)}",
            level="error",
        )
        run_service.finish_run(
            run_id=run.run_id,
            status=RunStatus.FAILED,
            error=str(e),
        )
        raise HTTPException(status_code=500, detail=f"转录失败: {str(e)}")
    finally:
        # 清理临时文件
        Path(tmp_path).unlink(missing_ok=True)