Skip to content

A self-hosted media server and file management system

refactor: break down transcript component into composables

aminnausin44064f655fc1
diff --git a/resources/js/components/video/transcript/transcriptParser.ts b/resources/js/components/video/transcript/transcriptParser.ts
new file mode 100644
index 00000000..9c07db6d
--- /dev/null
+++ b/resources/js/components/video/transcript/transcriptParser.ts
@@ -0,0 +1,46 @@
+import { timestampToSeconds } from '@/components/video/transcript/transcriptUtil';
+
+const ALLOWED_TAG_PATTERN = /<(?!\/?(?:i|u|br)\b)[^>]*>/gi;
+
+export interface TranscriptLine {
+ index: number;
+ text: string;
+ start: number;
+ end: number;
+}
+
+export function parseTranscript(rawTranscript: string): TranscriptLine[] {
+ const lines: TranscriptLine[] = [];
+
+ const vtt = rawTranscript.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, end] = line.split(' --> ');
+ const textLines: string[] = [];
+ i++;
+
+ while (i < cueLines.length && cueLines[i].trim() !== '') {
+ textLines.push(cueLines[i]);
+ i++;
+ }
+
+ const startTime = timestampToSeconds(start);
+ const endTime = timestampToSeconds(end);
+ const text = textLines.join('\n').trim().replace(ALLOWED_TAG_PATTERN, '');
+
+ if (!text) continue;
+
+ const previous = lines[lines.length - 1];
+ if (previous && previous.text === text && startTime <= previous.end) {
+ previous.end = Math.max(previous.end, endTime);
+ continue;
+ }
+
+ lines.push({ start: startTime, end: endTime, text, index: lines.length });
+ }
+ return lines;
+}
diff --git a/resources/js/components/video/transcript/transcriptUtil.ts b/resources/js/components/video/transcript/transcriptUtil.ts
new file mode 100644
index 00000000..061c9b0d
--- /dev/null
+++ b/resources/js/components/video/transcript/transcriptUtil.ts
@@ -0,0 +1,47 @@
+import type { SubtitleResource } from '@/contracts/media';
+import type { TranscriptLine } from '@/components/video/transcript/transcriptParser';
+
+export function timestampToSeconds(timestamp: string): number {
+ const parts = timestamp.trim().split(':').map(Number);
+ if (parts.length === 2) return parts[0] * 60 + parts[1];
+ if (parts.length === 3) return parts[0] * 3600 + parts[1] * 60 + parts[2];
+ return 0;
+}
+
+export 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`;
+}
+
+export function findActiveTranscriptIndex(lines: TranscriptLine[], time: number): number {
+ let low = 0;
+ let high = lines.length - 1;
+
+ while (low <= high) {
+ const mid = (low + high) >> 1;
+ const line = lines[mid];
+ const next = lines[mid + 1];
+
+ if (time < line.start) high = mid - 1;
+ else if (!next || time < next.start) return mid;
+ else low = mid + 1;
+ }
+
+ return -1;
+}
+
+export function formatTimestamp(totalSeconds: number, duration = 0): string {
+ 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}`;
+}
diff --git a/resources/js/components/video/transcript/useTranscript.ts b/resources/js/components/video/transcript/useTranscript.ts
index 0b6906f9..0f3370ef 100644
--- a/resources/js/components/video/transcript/useTranscript.ts
+++ b/resources/js/components/video/transcript/useTranscript.ts
@@ -1,98 +1,80 @@
import type { SubtitleResource } from '@/contracts/media';
+import type { TranscriptLine } from '@/components/video/transcript/transcriptParser';
import type { PlayerViewMode } from '@/components/video/VideoPlayer.vue';
+import type { Ref } from 'vue';
+import { buildTranscriptUrl, findActiveTranscriptIndex } from '@/components/video/transcript/transcriptUtil.ts';
import { computed, ref, shallowRef, watch } from 'vue';
+import { useReactiveBreakpoints } from '@/service/breakpoints/useReactiveBreakpoints';
+import { parseTranscript } from '@/components/video/transcript/transcriptParser';
+import { useAppStore } from '@/stores/AppStore';
+import { storeToRefs } from 'pinia';
import { toast } from '@aminnausin/cedar-ui';
-export interface TranscriptLine {
- index: number;
- text: string;
- start: number;
- end: number;
-}
-
-const ALLOWED_TAG_PATTERN = /<(?!\/?(?:i|u|br)\b)[^>]*>/gi;
+const { selectedSideBar } = storeToRefs(useAppStore());
+const { isDesktop } = useReactiveBreakpoints();
-const player = shallowRef<HTMLVideoElement | null>(null);
-const playerViewMode = ref<PlayerViewMode>('normal');
-const isNormalView = computed(() => playerViewMode.value === 'normal');
-const subtitleTrack = ref<SubtitleResource>();
+const isShowingTranscript = ref(false);
+const isLoading = ref(false);
-const rawTranscript = ref('');
const currentTime = ref(0);
-const isLoading = ref(false);
+const rawTranscript = ref('');
const loadedTranscriptUrl = ref<string>();
-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');
+const canUseSidebar = computed(() => isNormalView.value && isDesktop.value);
- for (let i = 0; i < cueLines.length; i++) {
- const line = cueLines[i].trim();
- if (!line.includes(' --> ')) continue;
-
- const [start, end] = line.split(' --> ');
- const textLines: string[] = [];
- i++;
+const placement = computed<'hidden' | 'sidebar' | 'overlay'>(() => {
+ if (!isShowingTranscript.value) return 'hidden';
+ return canUseSidebar.value && selectedSideBar.value === 'transcript' ? 'sidebar' : 'overlay';
+});
- while (i < cueLines.length && cueLines[i].trim() !== '') {
- textLines.push(cueLines[i]);
- i++;
- }
+const parsedTranscript = computed<TranscriptLine[]>(() => parseTranscript(rawTranscript.value));
- const startTime = timestampToSeconds(start);
- const endTime = timestampToSeconds(end);
- const text = textLines.join('\n').trim().replace(ALLOWED_TAG_PATTERN, '');
+const activeIndex = computed(() => findActiveTranscriptIndex(parsedTranscript.value, currentTime.value));
- if (!text) continue;
+//#region Context
- const previous = lines[lines.length - 1];
- if (previous && previous.text === text && startTime <= previous.end) {
- previous.end = Math.max(previous.end, endTime);
- continue;
- }
+type TranscriptContext = {
+ player: Readonly<Ref<HTMLVideoElement | null>>;
+ viewMode: Readonly<Ref<PlayerViewMode>>;
+ subtitleTrack: Readonly<Ref<SubtitleResource | undefined>>;
+ seek: (seconds: number) => void;
+ close: () => void;
+ generated: (track: SubtitleResource) => void;
+};
- lines.push({ start: startTime, end: endTime, text, index: lines.length });
- }
- return lines;
-});
+const context = shallowRef<TranscriptContext | null>(null);
-const activeIndex = computed(() => {
- const lines = parsedTranscript.value;
- const time = currentTime.value;
+const player = computed(() => context.value?.player.value ?? null);
+const isNormalView = computed(() => context.value?.viewMode.value === 'normal');
+const subtitleTrack = computed(() => context.value?.subtitleTrack.value);
- let low = 0;
- let high = lines.length - 1;
+function registerTranscriptContext(c: TranscriptContext) {
+ context.value = c;
+}
- while (low <= high) {
- const mid = (low + high) >> 1;
- const line = lines[mid];
- const next = lines[mid + 1];
+function unregisterTranscriptContext() {
+ context.value = null;
+}
- if (time < line.start) high = mid - 1;
- else if (!next || time < next.start) return mid;
- else low = mid + 1;
- }
+function seek(seconds: number) {
+ context.value?.seek(seconds);
+}
- return -1;
-});
+function close() {
+ context.value?.close();
+}
-function timestampToSeconds(timestamp: string): number {
- const parts = timestamp.trim().split(':').map(Number);
- if (parts.length === 2) return parts[0] * 60 + parts[1];
- if (parts.length === 3) return parts[0] * 3600 + parts[1] * 60 + parts[2];
- return 0;
+function generated(track: SubtitleResource) {
+ context.value?.generated(track);
}
-function buildTranscriptUrl(subtitle?: SubtitleResource): string | undefined {
- if (!subtitle) return undefined;
+//#endregion
- const { metadata_uuid, track_id, language } = subtitle;
- const languageSlug = track_id === 0 ? `.${language}` : '';
+//#region API
- return `/data/subtitles/${metadata_uuid}/${track_id}${languageSlug}.vtt`;
+function handleTimeUpdate(this: HTMLVideoElement) {
+ currentTime.value = this.currentTime;
}
async function loadTranscript() {
@@ -117,47 +99,22 @@ async function loadTranscript() {
}
}
-type TranscriptHandlers = {
- seek: (seconds: number) => void;
- close: () => void;
- generated: (track: SubtitleResource) => void;
-};
-
-let handlers: TranscriptHandlers | null = null;
-
-function registerTranscriptContext(h: TranscriptHandlers) {
- handlers = h;
-}
-
-function unregisterTranscriptContext() {
- handlers = null;
- player.value = null;
-}
-
-function seek(seconds: number) {
- handlers?.seek(seconds);
-}
-
-function close() {
- handlers?.close();
-}
-
-function generated(track: SubtitleResource) {
- handlers?.generated(track);
-}
-
-function handleTimeUpdate(this: HTMLVideoElement) {
- currentTime.value = this.currentTime;
-}
+//#endregion
watch(player, (newPlayer, oldPlayer) => {
oldPlayer?.removeEventListener('timeupdate', handleTimeUpdate);
newPlayer?.addEventListener('timeupdate', handleTimeUpdate);
});
+watch(isShowingTranscript, (show) => {
+ if (show && canUseSidebar.value) selectedSideBar.value = 'transcript';
+ else if (!show && selectedSideBar.value === 'transcript') selectedSideBar.value = '';
+});
+
export function useTranscript() {
return {
// transcript
+ isShowingTranscript,
rawTranscript,
currentTime,
isLoading,
@@ -165,15 +122,15 @@ export function useTranscript() {
parsedTranscript,
activeIndex,
loadTranscript,
+ placement,
// player-owned state
player,
- playerViewMode,
isNormalView,
subtitleTrack,
- // owner registration (VideoPlayer.vue calls these)
+ // context setup
registerTranscriptContext,
unregisterTranscriptContext,
- // consumer actions (PlayerTranscript.vue / TranscriptSidebar.vue call these)
+ // actions
seek,
close,
generated,