Files
it-tools-pro/src/tools/video-subtitle/video-subtitle.vue
T

440 lines
10 KiB
Vue
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<script setup lang="ts">
import { computed, ref } from 'vue';
import { useHead } from '@vueuse/head';
import { getMachineId, licenseHeaders } from '@/tools/license/license';
import { useGenHistory } from '@/tools/gen-history/gen-history';
useHead({ title: '视频字幕 AI - IT-TOOLS-PRO' });
const genHist = useGenHistory('video-subtitle');
interface Seg {
start: number;
end: number;
text: string;
}
const file = ref<File | null>(null);
const busy = ref(false);
const busyMsg = ref('');
const error = ref('');
const segs = ref<Seg[]>([]);
const lang = ref('');
const fileInput = ref<HTMLInputElement>();
const translating = ref(false);
const translated = ref('');
function fmt(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 srt = computed(() =>
segs.value.map((s, i) => `${i + 1}\n${fmt(s.start)} --> ${fmt(s.end)}\n${s.text}\n`).join('\n'),
);
function pick() {
fileInput.value?.click();
}
function onInput(e: Event) {
const input = e.target as HTMLInputElement;
file.value = input.files?.[0] ?? null;
input.value = '';
segs.value = [];
translated.value = '';
error.value = '';
}
async function transcribe() {
if (!file.value || busy.value) {
error.value = '请先选择视频文件';
return;
}
busy.value = true;
error.value = '';
translated.value = '';
busyMsg.value = '上传并提取音轨中…(长视频需要一些时间)';
try {
const fd = new FormData();
fd.append('file', file.value);
const res = await fetch(`/relay/subtitle?lang=`, {
method: 'POST',
body: fd,
headers: licenseHeaders('video-subtitle'),
});
busyMsg.value = 'AI 转录中…';
const j = await res.json().catch(() => ({}));
if (!res.ok || j.code !== 200) {
throw new Error(j.message || `HTTP ${res.status}`);
}
segs.value = j.data.segments || [];
lang.value = `${j.data.language}(${j.data.probability})`;
if (!segs.value.length) {
throw new Error('未识别到语音内容');
}
genHist.add({ title: `字幕转录: ${file.value.name}`, detail: `${segs.value.length} 条 · 语言 ${j.data.language}` });
}
catch (e: any) {
error.value = String(e?.message || e);
}
finally {
busy.value = false;
busyMsg.value = '';
}
}
async function translate(target: string) {
if (!srt.value || translating.value) {
return;
}
translating.value = true;
error.value = '';
try {
const res = await fetch('/relay/ai/translate-srt', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ machine: getMachineId(), srt: srt.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: `${j.data.reply.length} 字` });
}
catch (e: any) {
error.value = String(e?.message || e);
}
finally {
translating.value = false;
}
}
function download(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);
}
</script>
<template>
<div class="vs-root">
<div class="vs-head">
<div>
<div class="vs-title">🎬 视频字幕 AI</div>
<div class="vs-sub">本地 Whisper 语音识别 → 自动生成 SRT 字幕 → AI 一键翻译成英文/日文,与视频工具箱联动成完整链路</div>
</div>
</div>
<div class="vs-cols">
<div class="vs-left">
<input ref="fileInput" type="file" hidden accept="video/*,.mp4,.mkv,.mov,.webm,.avi" @change="onInput">
<div class="vs-drop" @click="pick">
<template v-if="!file">
<div class="vs-drop-hint">📁 选择视频文件<br><span>MP4 / MOV / MKV / WebM · ≤500MB</span></div>
</template>
<div v-else class="vs-file">
🎥 {{ file.name }}
<span class="vs-size">({{ (file.size / 1024 / 1024).toFixed(1) }} MB)</span>
<a href="javascript:;" class="vs-clear" @click.stop="file = null; segs = []">✕</a>
</div>
</div>
<div class="vs-sec">语言识别</div>
<div class="vs-chips">
<button class="vs-chip on">自动检测{{ lang ? ` · 已识别 ${lang}` : '' }}</button>
</div>
<button class="vs-go" :disabled="busy || !file" @click="transcribe()">
{{ busy ? `⏳ ${busyMsg}` : '🎙️ 生成字幕' }}
</button>
<template v-if="segs.length">
<div class="vs-sec">翻译</div>
<div class="vs-chips">
<button class="vs-chip" :disabled="translating" @click="translate('en')">
{{ translating ? '⏳ 翻译中…' : '🇬🇧 翻译成英文' }}
</button>
<button class="vs-chip" :disabled="translating" @click="translate('ja')">
🇯🇵 翻译成日文
</button>
</div>
</template>
<div v-if="error" class="vs-error">⚠️ {{ error }}</div>
<div class="vs-hint">💡 需要带语音的视频;无声/纯音乐视频会提示无语音。转录完全本地进行,视频不离开服务器。</div>
</div>
<div class="vs-right">
<div class="vs-sec">
转录结果 · {{ segs.length }} 条
<span v-if="segs.length" class="vs-actions">
<button class="vs-mini" @click="download(srt, 'subtitles.srt')">⬇ 下载 SRT</button>
<button class="vs-mini" @click="navigator.clipboard.writeText(srt)">📋 复制</button>
</span>
</div>
<div class="vs-out">
<div v-if="!segs.length" class="vs-empty">选择视频点「生成字幕」,时间轴预览在这里</div>
<template v-else>
<div v-for="(s, i) in segs" :key="i" class="vs-seg">
<span class="vs-time">{{ fmt(s.start).slice(3, 11) }}</span>
<span class="vs-text">{{ s.text }}</span>
</div>
</template>
</div>
<template v-if="translated">
<div class="vs-sec">
译文字幕
<span class="vs-actions">
<button class="vs-mini" @click="download(translated, 'subtitles-translated.srt')">⬇ 下载译文 SRT</button>
<button class="vs-mini" @click="navigator.clipboard.writeText(translated)">📋 复制</button>
</span>
</div>
<div class="vs-out vs-out-tr">
<pre class="vs-srt">{{ translated }}</pre>
</div>
</template>
<GenHistory tool="video-subtitle" label="字幕记录" class="mt-10px" />
</div>
</div>
</div>
</template>
<style lang="less" scoped>
.vs-root {
padding-bottom: 16px;
}
.vs-head {
margin-bottom: 14px;
.vs-title {
font-size: 17px;
font-weight: 800;
}
.vs-sub {
font-size: 12.5px;
opacity: 0.55;
margin-top: 3px;
}
}
.vs-cols {
display: flex;
gap: 14px;
align-items: flex-start;
}
.vs-left {
width: 380px;
flex-shrink: 0;
border: 1px solid rgba(128, 128, 128, 0.2);
border-radius: 12px;
padding: 14px;
}
.vs-right {
flex: 1;
min-width: 0;
}
.vs-drop {
border: 1.5px dashed rgba(128, 128, 128, 0.4);
border-radius: 10px;
padding: 18px 12px;
text-align: center;
cursor: pointer;
margin-bottom: 12px;
transition: border-color 0.15s;
&:hover {
border-color: #7c3aed;
}
.vs-drop-hint {
font-size: 13.5px;
font-weight: 600;
line-height: 1.7;
span {
font-size: 11.5px;
font-weight: 400;
opacity: 0.5;
}
}
.vs-file {
font-size: 12.5px;
font-weight: 600;
word-break: break-all;
.vs-size {
font-weight: 400;
opacity: 0.55;
}
.vs-clear {
color: #d03050;
margin-left: 8px;
text-decoration: none;
}
}
}
.vs-sec {
font-size: 12.5px;
font-weight: 700;
opacity: 0.75;
margin: 10px 0 7px;
display: flex;
justify-content: space-between;
align-items: center;
}
.vs-chips {
display: flex;
gap: 7px;
flex-wrap: wrap;
}
.vs-chip {
border: 1px solid rgba(128, 128, 128, 0.3);
background: #fff;
border-radius: 999px;
padding: 6px 14px;
font-size: 12.5px;
cursor: pointer;
&.on {
background: rgba(124, 58, 237, 0.1);
border-color: #7c3aed;
color: #7c3aed;
font-weight: 700;
}
&:disabled {
opacity: 0.55;
cursor: not-allowed;
}
}
.vs-go {
width: 100%;
margin-top: 13px;
border: none;
background: #7c3aed;
color: #fff;
border-radius: 9px;
padding: 11px;
font-size: 14px;
font-weight: 700;
cursor: pointer;
line-height: 1.5;
&:disabled {
opacity: 0.5;
cursor: not-allowed;
}
&:hover:not(:disabled) {
background: #6d28d9;
}
}
.vs-error {
color: #d03050;
font-size: 12.5px;
margin-top: 9px;
line-height: 1.6;
}
.vs-hint {
font-size: 11.5px;
opacity: 0.5;
line-height: 1.7;
margin-top: 10px;
}
.vs-actions {
display: flex;
gap: 6px;
}
.vs-mini {
border: 1px solid rgba(128, 128, 128, 0.3);
background: #fff;
border-radius: 6px;
padding: 3px 10px;
font-size: 11.5px;
cursor: pointer;
&:hover {
border-color: #18a058;
color: #18a058;
}
}
.vs-out {
background: rgba(128, 128, 128, 0.05);
border: 1px solid rgba(128, 128, 128, 0.15);
border-radius: 10px;
padding: 10px 12px;
max-height: 420px;
overflow-y: auto;
margin-bottom: 12px;
&.vs-out-tr {
max-height: 300px;
}
.vs-empty {
font-size: 13px;
opacity: 0.45;
text-align: center;
padding: 60px 0;
}
}
.vs-seg {
display: flex;
gap: 12px;
padding: 5px 2px;
border-bottom: 1px dashed rgba(128, 128, 128, 0.15);
font-size: 13px;
&:last-child {
border-bottom: none;
}
.vs-time {
color: #7c3aed;
font-family: monospace;
font-size: 12px;
flex-shrink: 0;
opacity: 0.85;
}
.vs-text {
line-height: 1.6;
}
}
.vs-srt {
margin: 0;
white-space: pre-wrap;
word-break: break-word;
font-family: inherit;
font-size: 12.5px;
line-height: 1.7;
}
</style>