feat: 绿幕视频创建和微课视频创建

This commit is contained in:
2024-08-17 17:56:35 +08:00
parent 24629f8720
commit e48a744f60
14 changed files with 797 additions and 41 deletions

View File

@@ -0,0 +1,297 @@
<script setup lang="ts">
import type { PropType } from 'vue'
import dayjs from 'dayjs'
import { useDownload } from '~/composables/useDownload'
import gsap from 'gsap'
import SRTEditor from '~/components/aigc/generation/SRTEditor.vue'
const toast = useToast()
const { metaSymbol } = useShortcuts()
const srtEditor = ref()
const props = defineProps({
course: {
type: Object as PropType<resp.gen.CourseGenItem>,
required: true,
},
})
const emit = defineEmits([
'delete',
])
defineShortcuts({
'p': {
handler: () => {
if (isDropdownOpen.value && isDownloadable.value) {
isPreviewModalOpen.value = true
}
},
},
'meta_d': {
handler: () => {
if (isDropdownOpen.value && isDownloadable.value) {
srtEditor.value.open()
isDropdownOpen.value = false
}
},
},
'meta_s': {
handler: async () => {
if (isDropdownOpen.value && isDownloadable.value) {
await startDownload(await fetchCourseSubtitleUrl(props.course), `眩生花微课_${ props.course.title }_${ props.course.task_id }.srt`)
}
},
},
'delete': {
handler: () => {
if (isDropdownOpen.value) {
emit('delete', props.course.task_id)
}
},
},
})
const isDropdownOpen = ref(false)
const isPreviewModalOpen = ref(false)
const stateDisplay = computed(() => {
if (props.course.progress === -1) return {
color: 'red',
text: '失败',
}
if (props.course.progress === 100) return {
color: 'green',
text: '完成',
}
return {
color: 'blue',
text: !!props.course.progress ? `${ tweenedGenerateProgress.value.toFixed(0) }%` : '队列中',
}
})
const isFailed = computed(() => props.course.progress === -1)
const isDownloadable = computed(() => !isFailed.value && props.course.progress === 100)
const generateProgress = computed(() => {
return props.course.progress || 0
})
const tweenedGenerateProgress = ref(0)
watch(generateProgress, (newValue) => {
gsap.to(tweenedGenerateProgress, {
duration: 5,
value: newValue,
})
}, {
immediate: true,
})
const downloadProgress = ref(0)
const startDownload = async (
url: string,
filename: string,
) => {
downloadProgress.value = 0
const {
download,
progressEmitter,
} = useDownload(url, filename)
progressEmitter.on('done', () => {
downloadProgress.value = 100
toast.add({
title: '下载完成',
description: '资源下载已完成',
color: 'green',
icon: 'i-tabler-check',
})
})
progressEmitter.on('progress', (progress) => {
downloadProgress.value = progress
})
progressEmitter.on('error', err => {
downloadProgress.value = 0
toast.add({
title: '下载失败',
description: err.message || '下载失败,未知错误',
color: 'red',
icon: 'i-tabler-alert-triangle',
})
})
download()
}
const copyTaskId = (extraMessage?: string) => {
navigator.clipboard.writeText(props.course.task_id + (extraMessage ? ` ${ extraMessage }` : ''))
toast.add({
title: '复制成功',
description: '已复制任务 ID',
color: 'green',
icon: 'i-tabler-check',
})
}
</script>
<template>
<div
class="w-full rounded-xl border border-neutral-200 dark:border-neutral-700 hover:shadow transition overflow-hidden"
>
<div class="relative w-full aspect-video">
<NuxtImg
class="w-full aspect-video object-cover pointer-events-none absolute inset-0"
v-if="!!course.video_cover"
:src="course.video_cover"
alt="image"
loading="lazy"
/>
<div
v-else
class="absolute inset-0 bg-gradient-to-br from-purple-400 to-primary-400 flex justify-center items-center"
>
<Icon
v-if="isFailed"
class="text-white text-[64px] opacity-50"
name="i-tabler-alert-triangle"
/>
<Icon v-else class="text-white text-[64px] animate-pulse" name="i-tabler-photo-video"/>
</div>
<div class="absolute inset-2 flex justify-end items-start">
<UTooltip :prevent="course.progress > -1" :text="course.message || ''">
<UBadge
:color="stateDisplay.color"
:variant="isFailed ? 'solid' : 'subtle'"
class="shadow"
size="sm"
>
<Icon
v-if="isFailed"
class="text-base mr-0.5"
name="i-tabler-alert-triangle"
/>
{{ stateDisplay.text }}
</UBadge>
</UTooltip>
</div>
</div>
<div class="px-2 pt-1 pb-2 flex justify-between">
<div class="flex-1 overflow-hidden pt-1">
<h1
:title="course.title"
class="inline-flex items-center text-sm font-medium overflow-hidden text-ellipsis text-nowrap"
>
<Icon class="text-base" name="i-tabler-book-2"/>
<span class="pl-0.5">{{ course.title }}</span>
</h1>
<p class="text-xs pt-0.5 text-neutral-400 space-x-2">
<span>{{ dayjs(course.create_time * 1000).format('YYYY-MM-DD HH:mm:ss') }}</span>
<button
v-if="course.task_id"
class="hover:text-primary font-medium"
tabindex="-1"
:title="course.task_id"
@click="copyTaskId(isFailed ? `\n\n${course.message}\n${course.ppt_url}` : '')"
>
复制ID
</button>
</p>
</div>
<div class="flex items-center gap-1">
<UButtonGroup>
<UButton
color="white"
:disabled="!isDownloadable"
:loading="downloadProgress > 0 && downloadProgress < 100"
:label="downloadProgress > 0 && downloadProgress < 100 ? `${downloadProgress.toFixed(0)}%` : '下载'"
leading-icon="i-tabler-download"
size="xs"
@click="startDownload(course.video_url, `眩生花微课_${ props.course.title }_${ props.course.task_id }.mp4`)"
/>
<UDropdown
v-model:open="isDropdownOpen"
:items="[
[{
label: '预览课程',
icon: 'i-tabler-play',
shortcuts: ['P'],
disabled: !isDownloadable,
click: () => isPreviewModalOpen = true,
}, {
label: '编辑字幕',
icon: 'i-solar-subtitles-linear',
shortcuts: [metaSymbol, 'D'],
disabled: !isDownloadable,
click: () => {
srtEditor.open()
isDropdownOpen = false
}
}, {
label: '下载字幕',
icon: 'i-tabler-file-download',
shortcuts: [metaSymbol, 'S'],
disabled: !isDownloadable,
click: async () => {
await startDownload(await fetchCourseSubtitleUrl(course), `眩生花微课_${ props.course.title }_${ props.course.task_id }.srt`)
}
}], [{
label: '删除记录',
icon: 'i-tabler-trash-x',
shortcuts: ['Delete'],
click: () => emit('delete', course.task_id)
}],
]"
:popper="{ placement: 'bottom-end' }"
>
<UButton
:disabled="course.progress > 1 && course.progress < 100"
color="white"
size="xs"
trailing-icon="i-tabler-dots"
/>
</UDropdown>
</UButtonGroup>
</div>
</div>
<UModal
v-model="isPreviewModalOpen"
>
<UCard :ui="{ ring: '', divide: 'divide-y divide-gray-100 dark:divide-gray-800' }">
<template #header>
<div class="flex items-center justify-between">
<div class="text-base font-semibold leading-6 text-gray-900 dark:text-white overflow-hidden">
<p>微课视频预览</p>
<p class="text-xs text-blue-500 w-full overflow-hidden text-nowrap text-ellipsis">
{{ course.title }}
</p>
</div>
<UButton
class="-my-1"
color="gray"
icon="i-tabler-x"
variant="ghost"
@click="isPreviewModalOpen = false"
/>
</div>
</template>
<video
class="w-full rounded shadow"
controls
autoplay
:src="course.video_url"
/>
</UCard>
</UModal>
<SRTEditor
ref="srtEditor"
:course="course"
/>
</div>
</template>
<style scoped>
</style>

View File

@@ -0,0 +1,188 @@
<script lang="ts" setup>
import type { PropType } from 'vue'
const props = defineProps({
video: {
type: Object as PropType<GBVideoItem>,
required: true,
},
})
const emit = defineEmits({
delete: (video: GBVideoItem) => video,
})
const dayjs = useDayjs()
const toast = useToast()
const isFullContentOpen = ref(false)
const downloadingState = reactive({
subtitle: 0,
video: 0,
})
const startDownload = (url: string, filename: string) => {
if (url.endsWith('.ass')) {
downloadingState.subtitle = 0
} else {
downloadingState.video = 0
}
const {
download,
progressEmitter,
} = useDownload(url, filename)
progressEmitter.on('progress', progress => {
if (url.endsWith('.ass')) {
downloadingState.subtitle = progress
} else {
downloadingState.video = progress
}
console.log(downloadingState)
})
progressEmitter.on('done', () => {
if (url.endsWith('.ass')) {
downloadingState.subtitle = 100
} else {
downloadingState.video = 100
}
toast.add({
title: '下载完成',
description: '资源下载已完成',
color: 'green',
icon: 'i-tabler-check',
})
})
progressEmitter.on('error', err => {
if (url.endsWith('.ass')) {
downloadingState.subtitle = 0
} else {
downloadingState.video = 0
}
toast.add({
title: '下载失败',
description: err.message || '下载失败,未知错误',
color: 'red',
icon: 'i-tabler-alert-triangle',
})
})
download()
}
const onClick = () => {
console.log('click delete')
}
</script>
<template>
<div
class="w-full flex gap-2 rounded-xl border border-neutral-200 dark:border-neutral-700 hover:shadow transition overflow-hidden p-3"
>
<div class="flex-0 h-48 aspect-[10/16] flex flex-col items-center justify-center rounded-lg shadow overflow-hidden">
<div v-if="!video.video_cover" class="w-full h-full bg-primary flex flex-col justify-center items-center gap-2">
<UIcon class="animate-spin text-4xl text-white" name="tabler:loader"/>
<div class="flex flex-col items-center gap-0.5">
<span class="text-sm font-bold text-white/90">火速生成中</span>
<span class="text-xs font-medium text-white/50">{{ video.progress }}%</span>
</div>
</div>
<NuxtImg v-else :src="video.video_cover" class="brightness-90 object-cover"/>
</div>
<div class="flex-1 flex flex-col justify-between gap-2">
<div class="flex-1 rounded-lg bg-neutral-100 dark:bg-neutral-800 p-2 px-2.5">
<ul class="grid grid-cols-2 gap-1.5">
<li class="col-span-2">
<!-- <h2 class="text-2xs font-medium text-primary-500">标题</h2>-->
<p class="text-sm font-bold line-clamp-1">{{ video.title || '无标题' }}</p>
</li>
<li class="">
<h2 class="text-2xs font-medium text-primary-500">完成时间</h2>
<p class="text-xs line-clamp-1">{{ dayjs(video.complete_time * 1000).format('YYYY-MM-DD HH:mm:ss') }}</p>
</li>
<li class="">
<h2 class="text-2xs font-medium text-primary-500">生成耗时</h2>
<p class="text-xs line-clamp-1">{{ dayjs.duration(video.duration || 0).format('HH:mm:ss') }}</p>
</li>
<li class="col-span-2 cursor-pointer" @click="isFullContentOpen = true">
<h2 class="text-2xs font-medium text-primary-500">驱动文本</h2>
<p class="text-xs line-clamp-3 text-justify">{{ video.content }}</p>
</li>
</ul>
</div>
<div class="flex justify-end sm:justify-between items-center group flex-nowrap whitespace-nowrap">
<div
class="hidden sm:flex items-center gap-1 transition-all group-hover:opacity-0 group-hover:pointer-events-none">
<UIcon class="text-primary text-lg" name="i-tabler-user-square-rounded"/>
<p class="text-xs">数字人 {{ video.digital_human_id }}</p>
</div>
<div class="space-x-2">
<UButton
class="transition-all sm:opacity-0 sm:translate-x-4 sm:pointer-events-none group-hover:opacity-100 group-hover:translate-x-0 group-hover:pointer-events-auto"
color="red"
icon="i-tabler-trash"
size="xs"
variant="soft"
@click="emit('delete', video)"
/>
<UButtonGroup size="xs">
<UButton
:label="downloadingState.subtitle > 0 && downloadingState.subtitle < 100 ? `${downloadingState.subtitle.toFixed(0)}%` : '字幕'"
:loading="downloadingState.subtitle > 0 && downloadingState.subtitle < 100"
color="primary"
leading-icon="i-tabler-file-download"
variant="soft"
@click="startDownload(video.subtitle!, (video.title || video.task_id) + '.ass')"
/>
<UButton
:label="downloadingState.video > 0 && downloadingState.video < 100 ? `${downloadingState.video.toFixed(0)}%` : '视频'"
:loading="downloadingState.video > 0 && downloadingState.video < 100"
color="primary"
leading-icon="i-tabler-download"
variant="soft"
@click="startDownload(video.video_url!, (video.title || video.task_id) + '.mp4')"
/>
</UButtonGroup>
</div>
</div>
</div>
<!-- Full video content -->
<UModal v-model="isFullContentOpen">
<UCard :ui="{ ring: '', divide: 'divide-y divide-gray-100 dark:divide-gray-800' }">
<template #header>
<div class="flex items-center justify-between">
<h3 class="text-base font-semibold leading-6 text-gray-900 dark:text-white">
{{ video.title || '无标题' }}
<span class="block text-xs text-primary">驱动内容</span>
</h3>
<UButton
color="gray"
icon="i-tabler-x"
variant="ghost"
@click="isFullContentOpen = false"
/>
</div>
</template>
<div>
<article class="prose">
<p class="text-justify">{{ video.content }}</p>
</article>
</div>
<template #footer>
<div class="flex justify-end gap-2">
<UButton color="primary" @click="isFullContentOpen = false">关闭</UButton>
</div>
</template>
</UCard>
</UModal>
</div>
</template>
<style scoped>
</style>

View File

@@ -0,0 +1,307 @@
<script setup lang="ts">
import type { PropType } from 'vue'
import { encode } from '@monosky/base64'
interface Subtitle {
start: string;
end: string;
text: string;
active?: boolean
}
const props = defineProps({
course: {
type: Object as PropType<resp.gen.CourseGenItem>,
required: true,
},
})
const dayjs = useDayjs()
const toast = useToast()
const loginState = useLoginState()
const isDrawerActive = ref(false)
const isLoading = ref(true)
const isSaving = ref(false)
const rawSrt = ref<string | null>(null)
const subtitles = ref<Subtitle[]>([])
const modified = ref(false)
const videoElement = ref<HTMLVideoElement | null>(null)
const loadSrt = async () => {
isLoading.value = true
try {
// const response = await fetch(props.course.subtitle_url)
const response = await fetch(await fetchCourseSubtitleUrl(props.course))
const text = await response.text()
rawSrt.value = text
parseSrt(text)
} catch (err) {
toast.add({
title: '加载字幕失败',
description: `${ err }` || '未知错误',
color: 'red',
})
} finally {
isLoading.value = false
}
}
const parseSrt = (srt: string) => {
const lines = srt.split(/\r?\n/)
const regex = /(\d{2}:\d{2}:\d{2},\d{3}) --> (\d{2}:\d{2}:\d{2},\d{3})/
let subtitle: Subtitle | null = null
lines.forEach(line => {
if (/^\d+$/.test(line.trim())) return
const match = line.match(regex)
if (match) {
if (subtitle) {
subtitles.value.push(subtitle)
}
subtitle = {
start: match[1],
end: match[2],
text: '',
}
} else if (subtitle) {
subtitle.text += (line.trim() ? line : '')
}
})
if (subtitle) {
subtitles.value.push(subtitle)
}
}
const generateSrt = () => {
return subtitles.value.map((subtitle, index) => {
return `${ index + 1 }\n${ subtitle.start } --> ${ subtitle.end }\n${ subtitle.text }\n`
}).join('\n')
}
const formatTime = (time: string) => {
const parts = time.split(',')
const timeParts = parts[0].split(':')
return {
hours: parseInt(timeParts[0]),
minutes: parseInt(timeParts[1]),
seconds: parseInt(timeParts[2]),
milliseconds: parseInt(parts[1]),
}
}
const formatTimeToDayjs = (time: string) => {
const parts = time.split(',')
const timeParts = parts[0].split(':')
return dayjs().hour(parseInt(timeParts[0])).minute(parseInt(timeParts[1])).second(parseInt(timeParts[2])).millisecond(parseInt(parts[1]))
}
const syncSubtitles = () => {
if (!videoElement.value) return
const currentTime = videoElement.value.currentTime * 1000 // convert to milliseconds
subtitles.value.forEach(subtitle => {
const start = formatTime(subtitle.start)
const end = formatTime(subtitle.end)
const startTime = (start.hours * 3600 + start.minutes * 60 + start.seconds) * 1000 + start.milliseconds
const endTime = (end.hours * 3600 + end.minutes * 60 + end.seconds) * 1000 + end.milliseconds
subtitle.active = currentTime >= startTime && currentTime <= endTime
// scroll active subtitle into view
if (subtitle.active) {
const element = document.getElementById(`subtitle-${ subtitles.value.indexOf(subtitle) }`)!
const parent = element?.parentElement
// scroll element to the center of parent
parent?.scrollTo({
top: element.offsetTop,
})
}
})
}
const onSubtitleInputClick = (subtitle: Subtitle) => {
if (!videoElement.value) return
if (!subtitle.active) {
videoElement.value.currentTime = formatTime(subtitle.start).hours * 3600 +
formatTime(subtitle.start).minutes * 60 +
formatTime(subtitle.start).seconds + 1
}
videoElement.value.pause()
}
const saveNewSubtitle = () => {
isSaving.value = true
const encodedSubtitle = encode(generateSrt())
useFetchWrapped<
req.gen.CourseSubtitleCreate & AuthedRequest,
BaseResponse<resp.gen.CourseSubtitleCreate>
>('App.Digital_VideoSubtitle.CreateFile', {
token: loginState.token!,
user_id: loginState.user.id,
sub_type: 1,
sub_content: encodedSubtitle,
task_id: props.course?.task_id,
}).then(_ => {
modified.value = false
toast.add({
color: 'green',
title: '字幕已保存',
description: '修改后的字幕文件已保存',
})
}).finally(() => {
isSaving.value = false
})
}
onMounted(() => {
if (rawSrt.value) {
parseSrt(rawSrt.value)
}
})
defineExpose({
open() {
isDrawerActive.value = true
if (!rawSrt.value) loadSrt()
},
close() {
isDrawerActive.value = false
},
})
</script>
<template>
<div>
<USlideover v-model="isDrawerActive" :ui="{ width: 'max-w-xl' }">
<UCard
class="flex flex-col flex-1 overflow-hidden"
:ui="{
body: { base: 'overflow-auto flex-1' },
ring: '',
divide: 'divide-y divide-gray-100 dark:divide-gray-800'
}"
>
<template #header>
<UButton
color="gray"
variant="ghost"
size="sm"
icon="i-tabler-x"
class="flex sm:hidden absolute end-5 top-5 z-10"
square
padded
@click="isDrawerActive = false"
/>
<div class="flex flex-col">
<h3 class="text-base font-semibold leading-6 text-gray-900 dark:text-white">
字幕编辑器
</h3>
<h3 class="text-xs font-semibold text-blue-500" v-if="course.title">
{{ course.title }}
</h3>
</div>
</template>
<div v-if="isLoading" class="flex justify-center items-center text-primary">
<svg xmlns="http://www.w3.org/2000/svg" width="32" height="32" viewBox="0 0 24 24">
<defs>
<filter id="svgSpinnersGooeyBalls20">
<feGaussianBlur in="SourceGraphic" result="y" stdDeviation="1"/>
<feColorMatrix in="y" result="z" values="1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 18 -7"/>
<feBlend in="SourceGraphic" in2="z"/>
</filter>
</defs>
<g filter="url(#svgSpinnersGooeyBalls20)">
<circle cx="5" cy="12" r="4" fill="currentColor">
<animate attributeName="cx" calcMode="spline" dur="2s" keySplines=".36,.62,.43,.99;.79,0,.58,.57"
repeatCount="indefinite" values="5;8;5"/>
</circle>
<circle cx="19" cy="12" r="4" fill="currentColor">
<animate attributeName="cx" calcMode="spline" dur="2s" keySplines=".36,.62,.43,.99;.79,0,.58,.57"
repeatCount="indefinite" values="19;16;19"/>
</circle>
<animateTransform attributeName="transform" dur="0.75s" repeatCount="indefinite" type="rotate"
values="0 12 12;360 12 12"/>
</g>
</svg>
</div>
<div v-else class="flex flex-col h-full overflow-hidden overscroll-y-none overshadow">
<div class="relative">
<div class="absolute w-fit mx-auto inset-x-0 bottom-3">
<span class="text-white font-bold text-shadow-lg">
{{ subtitles.find(sub => sub.active)?.text }}
</span>
</div>
<video
controls
ref="videoElement"
class="rounded"
style="-webkit-user-drag: none;"
:src="course.video_url"
@timeupdate="syncSubtitles"
/>
</div>
<ul class="flex-1 px-0.5 pb-[100%] overflow-y-auto mt-2 space-y-0.5 scroll-smooth relative">
<li
v-for="(subtitle, index) in subtitles"
:key="index"
:id="'subtitle-' + index"
>
<div :class="{'text-primary': subtitle.active}">
<span class="text-xs font-medium opacity-60">
{{ formatTimeToDayjs(subtitle.start).format('HH:mm:ss') }}
-
{{ formatTimeToDayjs(subtitle.end).format('HH:mm:ss') }}
<span class="opacity-50">
[{{ formatTimeToDayjs(subtitle.end).diff(formatTimeToDayjs(subtitle.start), 'second') }}s]
</span>
</span>
<UInput
v-model="subtitle.text"
class="w-full"
placeholder="请输入字幕内容"
:name="'subtitle-' + index"
:autofocus="false"
:color="subtitle.active ? 'primary' : undefined"
@click="onSubtitleInputClick(subtitle)"
@input="() => { if(!modified) modified = true }"
>
<template #trailing>
<Icon v-if="subtitle.active" name="tabler:keyframe-align-vertical-filled"/>
</template>
</UInput>
</div>
</li>
</ul>
</div>
<template #footer>
<div class="flex justify-end items-center gap-2">
<span v-if="modified" class="text-sm text-yellow-500 font-medium">已更改但未保存</span>
<UButton :disabled="!modified" :loading="isSaving" icon="i-tabler-device-floppy" @click="saveNewSubtitle">
保存{{ isSaving ? '' : '' }}
</UButton>
</div>
</template>
</UCard>
</USlideover>
</div>
</template>
<style scoped>
.overshadow {
@apply relative;
}
.overshadow:after {
content: "";
inset: 80% 0 0;
position: absolute;
@apply bg-gradient-to-b from-transparent to-white dark:to-neutral-950;
}
</style>