From 9bf6bb2564c93b4d59ad6b532c681ac1a9828b59 Mon Sep 17 00:00:00 2001 From: mvking5464 Date: Thu, 9 May 2024 22:42:27 +0530 Subject: [PATCH] auto seeding video duration to database --- src/app/api/video/route.ts | 25 ++++++++++++++++ src/components/VideoPlayerSegment.tsx | 30 ++++++++++++++++++- src/components/admin/ContentRenderer.tsx | 1 + .../admin/ContentRendererClient.tsx | 1 + 4 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 src/app/api/video/route.ts diff --git a/src/app/api/video/route.ts b/src/app/api/video/route.ts new file mode 100644 index 000000000..57c2d6d02 --- /dev/null +++ b/src/app/api/video/route.ts @@ -0,0 +1,25 @@ +import { NextRequest, NextResponse } from 'next/server'; +import db from '@/db'; +import { getServerSession } from 'next-auth'; +import { authOptions } from '@/lib/auth'; + +export async function PUT(req: NextRequest) { + const { contentId, duration } = await req.json(); + const session = await getServerSession(authOptions); + if (!session || !session?.user) { + return NextResponse.json( + { message: 'Authentication Failed' }, + { status: 401 }, + ); + } + try { + await db.videoMetadata.update({ + where: { contentId }, + data: { duration }, + }); + + return NextResponse.json({ success: true }, { status: 200 }); + } catch (error) { + return NextResponse.json({ success: false }, { status: 400 }); + } +} diff --git a/src/components/VideoPlayerSegment.tsx b/src/components/VideoPlayerSegment.tsx index f21f3e688..d05d115e2 100644 --- a/src/components/VideoPlayerSegment.tsx +++ b/src/components/VideoPlayerSegment.tsx @@ -1,5 +1,5 @@ 'use client'; -import React, { FunctionComponent, useRef } from 'react'; +import React, { FunctionComponent, useEffect, useRef, useState } from 'react'; import { VideoPlayer } from '@/components/VideoPlayer2'; import { @@ -21,6 +21,7 @@ interface VideoProps { setQuality: React.Dispatch>; thumbnails: Thumbnail[]; segments: Segment[]; + duration: number; subtitles: string; videoJsOptions: any; contentId: number; @@ -30,12 +31,14 @@ interface VideoProps { export const VideoPlayerSegment: FunctionComponent = ({ setQuality, contentId, + duration, subtitles, segments, videoJsOptions, onVideoEnd, }) => { const playerRef = useRef(null); + const [currentDuration, setCurrentDuration] = useState(0); const thumbnailPreviewRef = useRef(null); @@ -84,10 +87,35 @@ export const VideoPlayerSegment: FunctionComponent = ({ const handlePlayerReady = async (player: Player) => { playerRef.current = player; + setCurrentDuration(player.cache_.duration); createSegmentMarkersWithoutDuration(player, segments); overrideUpdateTime(player); }; + async function addVideoDuration() { + const newDuration = +currentDuration.toFixed(0); + await fetch('/api/video', { + method: 'PUT', + + body: JSON.stringify({ + contentId, + duration: newDuration, + }), + + headers: { + 'Content-type': 'application/json', + }, + }); + } + + useEffect(() => { + if (currentDuration) { + if (duration !== +currentDuration.toFixed(0)) { + addVideoDuration(); + } + } + }, [currentDuration]); + return (
diff --git a/src/components/admin/ContentRenderer.tsx b/src/components/admin/ContentRenderer.tsx index e42be8808..419b1322c 100644 --- a/src/components/admin/ContentRenderer.tsx +++ b/src/components/admin/ContentRenderer.tsx @@ -39,6 +39,7 @@ export const getMetadata = async (contentId: number) => { slides: metadata['slides'], //@ts-ignore segments: metadata['segments'], + duration: metadata['duration'], }; return { //@ts-ignore diff --git a/src/components/admin/ContentRendererClient.tsx b/src/components/admin/ContentRendererClient.tsx index 46344a3f7..c5e46598f 100644 --- a/src/components/admin/ContentRendererClient.tsx +++ b/src/components/admin/ContentRendererClient.tsx @@ -96,6 +96,7 @@ export const ContentRendererClient = ({ subtitles={metadata.subtitles} thumbnails={[]} segments={metadata?.segments || []} + duration={metadata.duration} videoJsOptions={{ playbackrates: [0.5, 1, 1.25, 1.5, 1.75, 2], controls: true,