refactor: 视频字幕AI并入视频工具 — 新增「AI智能」分组+AI字幕生成卡(语言选择/Whisper转录/SRT下载/AI翻译英日文/时间轴预览), 移除独立/video-subtitle入口, 授权与试用归入video-tools
This commit is contained in:
1 parent
6bcdfebf04
commit
98c0dedffd
8 files changed
+229
-460
No files matched your search
@@ -1,7 +1,7 @@
|
||||
<script setup lang="ts">
|
||||
import { computed, ref } from 'vue';
|
||||
import { useHead } from '@vueuse/head';
|
||||
import { licenseHeaders } from '../license/license';
|
||||
import { getMachineId, licenseHeaders } from '../license/license';
|
||||
import GenHistory from '@/tools/gen-history/GenHistory.vue';
|
||||
import { useGenHistory } from '@/tools/gen-history/gen-history';
|
||||
|
||||
@@ -30,6 +30,21 @@ interface VTool {
|
||||
}
|
||||
|
||||
const GROUPS: { title: string; subtitle: string; tools: VTool[] }[] = [
|
||||
{
|
||||
title: 'AI 智能',
|
||||
subtitle: '本地语音识别 + AI 翻译,视频字幕一条龙',
|
||||
tools: [
|
||||
{
|
||||
op: 'subtitle', name: 'AI 字幕生成', icon: '🎙️', color: '#7c3aed',
|
||||
desc: 'Whisper 本地转录生成 SRT 字幕,可 AI 翻译成英文/日文',
|
||||
tags: ['SRT', '本地识别', 'AI 翻译'],
|
||||
params: [{ key: 'lang', label: '语音语言', type: 'select', def: '', options: [
|
||||
{ label: '自动检测', value: '' }, { label: '中文', value: 'zh' }, { label: '英语', value: 'en' },
|
||||
{ label: '日语', value: 'ja' }, { label: '粤语', value: 'yue' },
|
||||
] }],
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
title: '格式转换',
|
||||
subtitle: '在不同视频 / GIF / 音频格式之间互转',
|
||||
@@ -215,6 +230,8 @@ function onDrop(e: DragEvent) {
|
||||
function setFile(f: File | null) {
|
||||
file.value = f;
|
||||
error.value = '';
|
||||
subSegs.value = [];
|
||||
translated.value = '';
|
||||
resultBlob.value = null;
|
||||
selBox.value = null;
|
||||
if (resultUrl.value) {
|
||||
@@ -223,6 +240,95 @@ function setFile(f: File | null) {
|
||||
}
|
||||
}
|
||||
|
||||
// ============ AI 字幕 ============
|
||||
interface SubSeg { start: number; end: number; text: string }
|
||||
const subSegs = ref<SubSeg[]>([]);
|
||||
const subLang = ref('');
|
||||
const subSpent = ref(0);
|
||||
const translating = ref(false);
|
||||
const translated = ref('');
|
||||
|
||||
function subFmt(t: number) {
|
||||
const h = Math.floor(t / 3600);
|
||||
const m = Math.floor((t % 3600) / 60);
|
||||
const s = Math.floor(t % 60);
|
||||
const ms = Math.round((t % 1) * 1000);
|
||||
return `${String(h).padStart(2, '0')}:${String(m).padStart(2, '0')}:${String(s).padStart(2, '0')},${String(ms).padStart(3, '0')}`;
|
||||
}
|
||||
|
||||
const subSrt = computed(() =>
|
||||
subSegs.value.map((s, i) => `${i + 1}\n${subFmt(s.start)} --> ${subFmt(s.end)}\n${s.text}\n`).join('\n'),
|
||||
);
|
||||
|
||||
function downloadText(content: string, name: string) {
|
||||
const blob = new Blob([content], { type: 'text/plain;charset=utf-8' });
|
||||
const a = document.createElement('a');
|
||||
a.href = URL.createObjectURL(blob);
|
||||
a.download = name;
|
||||
a.click();
|
||||
URL.revokeObjectURL(a.href);
|
||||
}
|
||||
|
||||
async function translateSrt(target: string) {
|
||||
if (!subSrt.value || translating.value) {
|
||||
return;
|
||||
}
|
||||
translating.value = true;
|
||||
error.value = '';
|
||||
try {
|
||||
const res = await fetch('/relay/ai/translate-srt', {
|
||||
method: 'POST',
|
||||
headers: { ...licenseHeaders(), 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ machine: getMachineId(), srt: subSrt.value, target }),
|
||||
});
|
||||
const j = await res.json().catch(() => ({}));
|
||||
if (!res.ok || j.code !== 200) {
|
||||
throw new Error(j.message || `HTTP ${res.status}`);
|
||||
}
|
||||
translated.value = j.data.reply;
|
||||
genHist.add({ title: `字幕翻译→${target === 'en' ? '英文' : '日文'}`, detail: `${subSegs.value.length} 条` });
|
||||
}
|
||||
catch (e: any) {
|
||||
error.value = String(e?.message || e);
|
||||
}
|
||||
finally {
|
||||
translating.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
async function runSubtitle(t0: number) {
|
||||
busy.value = true;
|
||||
error.value = '';
|
||||
subSegs.value = [];
|
||||
translated.value = '';
|
||||
try {
|
||||
const fd = new FormData();
|
||||
fd.append('file', file.value!);
|
||||
const res = await fetch(`/relay/subtitle?lang=${paramVals.value.lang || ''}`, {
|
||||
method: 'POST',
|
||||
body: fd,
|
||||
headers: licenseHeaders(),
|
||||
});
|
||||
const j = await res.json().catch(() => ({}));
|
||||
if (!res.ok || j.code !== 200) {
|
||||
throw new Error(j.message || `HTTP ${res.status}`);
|
||||
}
|
||||
subSegs.value = j.data.segments || [];
|
||||
subLang.value = `${j.data.language}(${j.data.probability})`;
|
||||
subSpent.value = Math.round((Date.now() - t0) / 1000);
|
||||
if (!subSegs.value.length) {
|
||||
throw new Error('未识别到语音内容');
|
||||
}
|
||||
genHist.add({ title: `字幕转录: ${file.value!.name}`, detail: `${subSegs.value.length} 条 · ${subLang.value}` });
|
||||
}
|
||||
catch (e: any) {
|
||||
error.value = String(e?.message || e);
|
||||
}
|
||||
finally {
|
||||
busy.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
async function process() {
|
||||
if (!file.value) {
|
||||
error.value = '请先选择文件';
|
||||
@@ -231,6 +337,10 @@ async function process() {
|
||||
busy.value = true;
|
||||
error.value = '';
|
||||
const t0 = Date.now();
|
||||
if (currentOp.value === 'subtitle') {
|
||||
await runSubtitle(t0);
|
||||
return;
|
||||
}
|
||||
try {
|
||||
const fd = new FormData();
|
||||
fd.append('file', file.value);
|
||||
@@ -487,6 +597,36 @@ function onSelUp(e: MouseEvent) {
|
||||
{{ error }}
|
||||
</div>
|
||||
|
||||
<!-- AI 字幕结果 -->
|
||||
<div v-if="currentOp === 'subtitle' && subSegs.length" class="vt-sub">
|
||||
<div class="vt-sub-bar">
|
||||
<span class="vt-sub-info">✓ {{ subSegs.length }} 条字幕 · 语言 {{ subLang }} · {{ subSpent }}s</span>
|
||||
<span class="vt-sub-acts">
|
||||
<button class="vt-btn vt-btn-sm" @click="downloadText(subSrt, 'subtitles.srt')">⬇ SRT</button>
|
||||
<button class="vt-btn vt-btn-sm vt-btn-ghost" :disabled="translating" @click="translateSrt('en')">
|
||||
{{ translating ? '⏳ 翻译中…' : '🇬🇧 译英文' }}
|
||||
</button>
|
||||
<button class="vt-btn vt-btn-sm vt-btn-ghost" :disabled="translating" @click="translateSrt('ja')">🇯🇵 译日文</button>
|
||||
</span>
|
||||
</div>
|
||||
<div class="vt-sub-list">
|
||||
<div v-for="(s, i) in subSegs" :key="i" class="vt-sub-seg">
|
||||
<span class="vt-sub-time">{{ subFmt(s.start).slice(3, 11) }}</span>
|
||||
<span class="vt-sub-text">{{ s.text }}</span>
|
||||
</div>
|
||||
</div>
|
||||
<template v-if="translated">
|
||||
<div class="vt-sub-bar">
|
||||
<span class="vt-sub-info">译文字幕</span>
|
||||
<span class="vt-sub-acts">
|
||||
<button class="vt-btn vt-btn-sm" @click="downloadText(translated, 'subtitles-translated.srt')">⬇ 译文 SRT</button>
|
||||
<button class="vt-btn vt-btn-sm vt-btn-ghost" @click="navigator.clipboard.writeText(translated)">📋 复制</button>
|
||||
</span>
|
||||
</div>
|
||||
<pre class="vt-sub-tr">{{ translated }}</pre>
|
||||
</template>
|
||||
</div>
|
||||
|
||||
<div v-if="resultUrl" class="vt-result">
|
||||
<video v-if="!isImageResult" :src="resultUrl" controls class="vt-preview" />
|
||||
<img v-else :src="resultUrl" class="vt-preview-img" alt="result">
|
||||
@@ -506,6 +646,90 @@ function onSelUp(e: MouseEvent) {
|
||||
</template>
|
||||
|
||||
<style lang="less" scoped>
|
||||
.vt-sub {
|
||||
margin-top: 10px;
|
||||
border: 1px solid rgba(124, 58, 237, 0.3);
|
||||
border-radius: 10px;
|
||||
padding: 11px 13px;
|
||||
background: rgba(124, 58, 237, 0.04);
|
||||
|
||||
.vt-sub-bar {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
flex-wrap: wrap;
|
||||
margin-bottom: 8px;
|
||||
|
||||
.vt-sub-info {
|
||||
font-size: 12.5px;
|
||||
font-weight: 700;
|
||||
color: #7c3aed;
|
||||
}
|
||||
|
||||
.vt-sub-acts {
|
||||
display: flex;
|
||||
gap: 6px;
|
||||
flex-wrap: wrap;
|
||||
}
|
||||
}
|
||||
|
||||
.vt-sub-list {
|
||||
max-height: 260px;
|
||||
overflow-y: auto;
|
||||
background: rgba(255, 255, 255, 0.7);
|
||||
border-radius: 8px;
|
||||
padding: 7px 9px;
|
||||
margin-bottom: 8px;
|
||||
}
|
||||
|
||||
.vt-sub-seg {
|
||||
display: flex;
|
||||
gap: 10px;
|
||||
font-size: 12.5px;
|
||||
padding: 4px 2px;
|
||||
border-bottom: 1px dashed rgba(128, 128, 128, 0.15);
|
||||
|
||||
&:last-child {
|
||||
border-bottom: none;
|
||||
}
|
||||
|
||||
.vt-sub-time {
|
||||
color: #7c3aed;
|
||||
font-family: monospace;
|
||||
font-size: 11.5px;
|
||||
flex-shrink: 0;
|
||||
}
|
||||
|
||||
.vt-sub-text {
|
||||
line-height: 1.55;
|
||||
}
|
||||
}
|
||||
|
||||
.vt-sub-tr {
|
||||
margin: 0;
|
||||
white-space: pre-wrap;
|
||||
word-break: break-word;
|
||||
font-family: inherit;
|
||||
font-size: 12.5px;
|
||||
line-height: 1.7;
|
||||
max-height: 220px;
|
||||
overflow-y: auto;
|
||||
background: rgba(255, 255, 255, 0.7);
|
||||
border-radius: 8px;
|
||||
padding: 9px 11px;
|
||||
}
|
||||
}
|
||||
|
||||
.vt-btn-ghost {
|
||||
background: transparent !important;
|
||||
color: #7c3aed !important;
|
||||
border: 1px solid rgba(124, 58, 237, 0.4) !important;
|
||||
|
||||
&:disabled {
|
||||
opacity: 0.5;
|
||||
}
|
||||
}
|
||||
.vt-root {
|
||||
padding-bottom: 16px;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user