Skip to content

Commit

Permalink
auto seeding video duration to database
Browse files Browse the repository at this point in the history
  • Loading branch information
mvp5464 committed May 9, 2024
1 parent a653737 commit 9bf6bb2
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 1 deletion.
25 changes: 25 additions & 0 deletions src/app/api/video/route.ts
Original file line number Diff line number Diff line change
@@ -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 });
}
}
30 changes: 29 additions & 1 deletion src/components/VideoPlayerSegment.tsx
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -21,6 +21,7 @@ interface VideoProps {
setQuality: React.Dispatch<React.SetStateAction<string>>;
thumbnails: Thumbnail[];
segments: Segment[];
duration: number;
subtitles: string;
videoJsOptions: any;
contentId: number;
Expand All @@ -30,12 +31,14 @@ interface VideoProps {
export const VideoPlayerSegment: FunctionComponent<VideoProps> = ({
setQuality,
contentId,
duration,
subtitles,
segments,
videoJsOptions,
onVideoEnd,
}) => {
const playerRef = useRef<Player | null>(null);
const [currentDuration, setCurrentDuration] = useState(0);

const thumbnailPreviewRef = useRef<HTMLDivElement>(null);

Expand Down Expand Up @@ -84,10 +87,35 @@ export const VideoPlayerSegment: FunctionComponent<VideoProps> = ({
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 (
<div className="mb-6">
<div className="flex-1 relative">
Expand Down
1 change: 1 addition & 0 deletions src/components/admin/ContentRenderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export const getMetadata = async (contentId: number) => {
slides: metadata['slides'],
//@ts-ignore
segments: metadata['segments'],
duration: metadata['duration'],
};
return {
//@ts-ignore
Expand Down
1 change: 1 addition & 0 deletions src/components/admin/ContentRendererClient.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 9bf6bb2

Please sign in to comment.