Skip to content

A self-hosted media server and file management system

feat: basic transcript overlay

aminnausin620d92be5153
diff --git a/resources/js/components/video/VideoPlayer.vue b/resources/js/components/video/VideoPlayer.vue
index b6f4e216..24fbc444 100755
--- a/resources/js/components/video/VideoPlayer.vue
+++ b/resources/js/components/video/VideoPlayer.vue
@@ -30,6 +30,7 @@ import VideoPopoverSlider from '@/components/video/popover/VideoPopoverSlider.vu
import AudioSpectrograph from '@/components/video/audio/AudioSpectrograph.vue';
import PlayerAudioTracks from '@/components/video/audio/PlayerAudioTracks.vue';
import VideoPopoverItem from '@/components/video/popover/VideoPopoverItem.vue';
+import PlayerTranscript from '@/components/video/transcript/PlayerTranscript.vue';
import PlayerSubtitles from '@/components/video/subtitles/PlayerSubtitles.vue';
import VideoPartyPanel from '@/components/video/plugins/party/VideoPartyPanel.vue';
import PlayerSkipIntro from '@/components/video/plugins/skip-intro/PlayerSkipIntro.vue';
@@ -182,6 +183,7 @@ const timeDisplay = ref<'timeElapsed' | 'timeRemaining'>('timeElapsed');
const isShowingAudioGraphSettings = ref(false);
const isPlayerSizeConstrained = computed(() => (isAudio.value || aspectRatio.value.isPortrait) && isNormalView.value); // Is size determined by album art or portrait video
const isThumbnailDismissed = ref(false);
+const isShowingTranscript = ref(false);
const isPictureInPicture = ref(false);
const isShowingControls = ref(false);
const isLoadingMetadata = ref(false);
@@ -323,6 +325,16 @@ const playerContextMenuItems = computed<ContextMenuItem[]>(() =>
isShowingParty.value = !isShowingParty.value;
},
},
+ {
+ text: 'Transcript',
+ icon: isShowingTranscript.value ? ProiconsCheckmark : undefined,
+ selected: isShowingTranscript.value,
+ disabled: stateVideo.value.subtitles.length == 0,
+ hidden: isAudio.value,
+ action: () => {
+ isShowingTranscript.value = !isShowingTranscript.value;
+ },
+ },
{
text: 'Show Miniplayer',
icon: isPictureInPicture.value ? ProiconsCheckmark : undefined,
@@ -1555,9 +1567,9 @@ defineExpose({
<!-- UI Panels Z-8 (Stats, Options)-->
<div
- style="z-index: 8; max-height: calc(100% - calc(var(--spacing) * 18))"
+ style="z-index: 7; height: calc(100% - calc(var(--spacing) * 18))"
:class="
- cn('ui-layer scrollbar-hide inset-x-0 flex h-fit flex-wrap justify-between gap-2 overflow-auto', 'xms:top-2 top-9 bottom-16 mx-2', {
+ cn('ui-layer scrollbar-hide inset-x-0 flex flex-wrap justify-between gap-2', 'xms:top-2 top-9 bottom-16 mx-2', {
'top-11! mx-4': isFullScreen || isTheatreView,
})
"
@@ -1583,14 +1595,26 @@ defineExpose({
</div>
<!-- UI Panels Right -->
- <div :class="cn('flex h-full flex-col gap-2', { 'xxs:mt-7': isNormalView })">
+ <div
+ :class="cn('flex h-full flex-1 flex-col items-end gap-2', { 'xxs:mt-7': isNormalView })"
+ :style="{
+ maxHeight: isNormalView ? 'calc(100% - var(--spacing) * 7)' : 'calc(100% - var(--spacing) * 11)',
+ }"
+ >
<!-- Watch Party (Z-7) -->
<VideoPartyPanel :is-showing-party="isShowingParty" />
+ <PlayerTranscript
+ :is-visible="isShowingTranscript && stateVideo.subtitles.length > 0"
+ :player="player"
+ :subtitle-track="playerSubtitles?.currentSubtitleTrack ?? playerSubtitles?.defaultSubtitleTrack"
+ @seek="handleManualSeek"
+ @close="isShowingTranscript = false"
+ />
</div>
</div>
<!-- Overlay Controls and Notifications Z-7 (Skip Intro, Timeline) -->
- <div style="z-index: 7" class="ui-layer inset-0 flex">
+ <div style="z-index: 8" class="ui-layer inset-0 flex">
<!-- Overlay controls -->
<div :class="['absolute bottom-18 xl:bottom-23', isNormalView ? 'left-2' : 'left-4', '-ms-1 flex h-fit max-h-28 max-w-42 flex-col-reverse gap-1 overflow-clip p-1']">
<!-- Skip Intro (Z-7) -->
diff --git a/resources/js/components/video/transcript/PlayerTranscript.vue b/resources/js/components/video/transcript/PlayerTranscript.vue
new file mode 100644
index 00000000..31780d32
--- /dev/null
+++ b/resources/js/components/video/transcript/PlayerTranscript.vue
@@ -0,0 +1,229 @@
+<script setup lang="ts">
+import type { SubtitleResource } from '@/contracts/media';
+
+import { computed, nextTick, onUnmounted, ref, useTemplateRef, watch, type ComponentPublicInstance } from 'vue';
+import { ButtonCorner } from '@/components/cedar-ui/button';
+
+import ProiconsCancel from '~icons/proicons/cancel';
+
+const props = defineProps<{ isVisible: boolean; player: HTMLVideoElement | null; subtitleTrack?: SubtitleResource }>();
+
+const emit = defineEmits<{ seek: [value: number]; close: [] }>();
+
+const currentTime = ref(0);
+const rawTranscript = ref('');
+const isLoading = ref(false);
+
+const transcriptContainer = useTemplateRef('transcript-container');
+
+interface TranscriptLine {
+ index: number;
+ text: string;
+ timestamp: number;
+}
+
+const ALLOWED_TAG_PATTERN = /<(?!\/?(?:i|u|br)\b)[^>]*>/gi;
+
+const parsedTranscript = computed<TranscriptLine[]>(() => {
+ const lines: TranscriptLine[] = [];
+
+ const vtt = rawTranscript.value.replace(/\r\n/g, '\n').replace(/\r/g, '\n');
+
+ const cueLines = vtt.split('\n');
+
+ for (let i = 0; i < cueLines.length; i++) {
+ const line = cueLines[i].trim();
+
+ if (!line.includes(' --> ')) continue;
+
+ const [start] = line.split(' --> ');
+ const textLines: string[] = [];
+
+ i++;
+
+ while (i < cueLines.length && cueLines[i].trim() !== '') {
+ textLines.push(cueLines[i]);
+ i++;
+ }
+
+ const text = textLines.join('\n').trim();
+
+ if (!text) continue;
+
+ lines.push({
+ timestamp: timestampToSeconds(start),
+ text: text.replace(ALLOWED_TAG_PATTERN, ''),
+ index: lines.length,
+ });
+ }
+ return lines;
+});
+
+const activeIndex = computed(() => {
+ const lines = parsedTranscript.value;
+ for (let i = 0; i < lines.length; i++) {
+ const next = lines[i + 1];
+ if (currentTime.value >= lines[i].timestamp && (!next || currentTime.value < next.timestamp)) {
+ return i;
+ }
+ }
+ return -1;
+});
+
+function timestampToSeconds(timestamp: string): number {
+ const parts = timestamp.trim().split(':').map(Number);
+
+ if (parts.length === 2) {
+ const [minutes, seconds] = parts;
+ return minutes * 60 + seconds;
+ }
+
+ if (parts.length === 3) {
+ const [hours, minutes, seconds] = parts;
+ return hours * 3600 + minutes * 60 + seconds;
+ }
+
+ return 0;
+}
+
+function formatTimestamp(totalSeconds: number): string {
+ const duration = props.player?.duration ?? 0;
+
+ const h = Math.floor(totalSeconds / 3600);
+ const m = Math.floor((totalSeconds % 3600) / 60);
+ const s = totalSeconds % 60;
+
+ const formattedSeconds = s.toFixed(0).padStart(2, '0');
+
+ if (duration >= 3600) return `${h}:${String(m).padStart(2, '0')}:${formattedSeconds}`;
+ if (duration >= 600) return `${String(m).padStart(2, '0')}:${formattedSeconds}`;
+ return `${m}:${formattedSeconds}`;
+}
+
+function buildTranscriptUrl(subtitle?: SubtitleResource): string | undefined {
+ if (!subtitle) return undefined;
+
+ const { metadata_uuid, track_id, language } = subtitle;
+ const languageSlug = track_id === 0 ? `.${language}` : '';
+
+ return `/data/subtitles/${metadata_uuid}/${track_id}${languageSlug}.vtt`;
+}
+
+const transcriptLineRefs = ref<(HTMLElement | null)[]>([]);
+
+function setTranscriptLineRef(index: number, el: Element | ComponentPublicInstance | null) {
+ transcriptLineRefs.value[index] = el as HTMLElement | null;
+}
+
+function handleUpdateEvent() {
+ currentTime.value = props.player?.currentTime ?? 0;
+}
+
+watch(
+ () => props.player,
+ (newPlayer, oldPlayer) => {
+ oldPlayer?.removeEventListener('timeupdate', handleUpdateEvent);
+ newPlayer?.addEventListener('timeupdate', handleUpdateEvent);
+ },
+ { immediate: true },
+);
+
+watch(
+ () => props.subtitleTrack,
+ async (track) => {
+ rawTranscript.value = '';
+ transcriptLineRefs.value = [];
+
+ const url = buildTranscriptUrl(track);
+ if (!url) return;
+
+ try {
+ isLoading.value = true;
+ const response = await fetch(url);
+ if (!response.ok) throw new Error(`Failed to fetch transcript: ${response.status}`);
+
+ rawTranscript.value = await response.text();
+ } catch (error) {
+ console.error('Failed to load transcript:', error);
+ } finally {
+ isLoading.value = false;
+ }
+ },
+ { immediate: true },
+);
+
+watch(activeIndex, async (index) => {
+ if (index === -1) return;
+
+ await nextTick();
+
+ const line = transcriptLineRefs.value[index];
+ const container = transcriptContainer.value;
+
+ if (!line || !container) return;
+
+ const lineTop = line.offsetTop;
+ const lineBottom = lineTop + line.offsetHeight;
+ const visibleTop = container.scrollTop;
+ const visibleBottom = visibleTop + container.clientHeight;
+
+ if (lineTop < visibleTop) {
+ container.scrollTo({ top: lineTop - 16, behavior: 'smooth' });
+ } else if (lineBottom > visibleBottom) {
+ container.scrollTo({ top: lineBottom - container.clientHeight + 16, behavior: 'smooth' });
+ }
+});
+
+onUnmounted(() => {
+ props.player?.removeEventListener('timeupdate', handleUpdateEvent);
+});
+</script>
+
+<template>
+ <div v-show="isVisible" class="pointer-events-auto w-full max-w-120 flex-1 overflow-y-auto rounded-xl border border-neutral-700/10 bg-neutral-800/90 p-1.5 backdrop-blur-md">
+ <div ref="transcript-container" class="scrollbar-minimal scrollbar-dark relative flex h-full gap-1 overflow-y-auto pe-1">
+ <div class="w-full space-y-1" v-if="isVisible">
+ <div class="sticky top-0 z-10 flex items-center justify-between gap-2 rounded-lg px-2 py-1.5 backdrop-blur-md">
+ <p class="text-sm font-medium text-white/90">Transcript</p>
+ <ButtonCorner
+ title="Close Transcript"
+ @click="emit('close')"
+ colour-classes="hover:bg-transparent"
+ text-classes=" hover:text-danger-2"
+ position-classes="size-5"
+ >
+ <template #icon> <ProiconsCancel /> </template>
+ </ButtonCorner>
+ </div>
+ <div v-if="isLoading">
+ <button type="button" :class="['group flex w-full animate-pulse cursor-pointer items-start gap-2 rounded-lg px-2 py-1.5 text-left transition-colors']">
+ <span class="shrink-0 rounded-md px-1.5 py-0.5 font-mono text-xs tabular-nums transition-colors" :class="'bg-white/5 text-transparent'"> 00:00 </span>
+ <span class="h-5 w-full rounded-md bg-white/15"></span>
+ </button>
+ </div>
+ <div class="space-y-0.5" v-else>
+ <button
+ v-for="line in parsedTranscript"
+ :key="line.index"
+ :ref="(el) => setTranscriptLineRef(line.index, el)"
+ :class="[
+ 'group flex w-full cursor-pointer items-start gap-2 rounded-lg px-2 py-1.5 text-left transition-colors',
+ line.index === activeIndex ? 'bg-white/12 text-white' : 'text-white/70 hover:bg-white/7 hover:text-white',
+ ]"
+ :title="`${line.index + 1} of ${parsedTranscript.length}`"
+ type="button"
+ @click="emit('seek', line.timestamp)"
+ >
+ <span
+ class="shrink-0 rounded-md px-1.5 py-0.5 font-mono text-xs tabular-nums transition-colors"
+ :class="line.index === activeIndex ? 'bg-white/15 text-white' : 'bg-white/5 text-white/50 group-hover:text-white/80'"
+ >
+ {{ formatTimestamp(line.timestamp) }}
+ </span>
+ <span class="font-dm-sans line-clamp-3 text-sm leading-5" v-html="line.text"> </span>
+ </button>
+ </div>
+ </div>
+ </div>
+ </div>
+</template>