From f5f999ae1b5ed52cdea45fa1146915a398a9de1f Mon Sep 17 00:00:00 2001 From: Roland Date: Tue, 15 Nov 2022 15:03:12 -0700 Subject: [PATCH] Replacing chapters in video description with links that seek to the chapter. --- app/src/components/VideoPlayer.js | 69 ++++++++++++++++++++++++++----- 1 file changed, 58 insertions(+), 11 deletions(-) diff --git a/app/src/components/VideoPlayer.js b/app/src/components/VideoPlayer.js index a34e553e..d77be3c9 100644 --- a/app/src/components/VideoPlayer.js +++ b/app/src/components/VideoPlayer.js @@ -1,7 +1,8 @@ import React, {useContext, useState} from 'react'; import {deleteVideos} from "../api"; import {Link, useNavigate, useParams} from "react-router-dom"; -import {humanFileSize, humanNumber, PageContainer, secondsToTimestamp, uploadDate, useTitle} from "./Common"; +import _ from "lodash"; +import {API_URI, humanFileSize, humanNumber, PageContainer, secondsToTimestamp, uploadDate, useTitle} from "./Common"; import {Confirm} from "semantic-ui-react"; import Grid from "semantic-ui-react/dist/commonjs/collections/Grid"; import Container from "semantic-ui-react/dist/commonjs/elements/Container"; @@ -18,6 +19,7 @@ function VideoPage({videoFile, prevFile, nextFile, setFavorite, ...props}) { const {theme} = useContext(ThemeContext); const navigate = useNavigate(); + const videoRef = React.useRef(); let [deleteOpen, setDeleteOpen] = useState(false); useTitle(videoFile === null || videoFile === undefined ? null : videoFile.video.title); @@ -26,6 +28,11 @@ function VideoPage({videoFile, prevFile, nextFile, setFavorite, ...props}) { video = videoFile.video; } + // Seeks to the `seconds` on video player. + const setVideoTime = (seconds) => { + videoRef.current.currentTime = seconds; + } + // Get the Video's channel, fallback to the URL's channel id. const {channelId} = useParams(); const {channel} = useChannel(video && video.channel_id ? video.channel_id : channelId); @@ -67,20 +74,19 @@ function VideoPage({videoFile, prevFile, nextFile, setFavorite, ...props}) { setFavorite(!favorite); } } - const favorite_button = ( - ); + const favorite_button = (); let descriptionPane = { menuItem: 'Description', render: () =>
-                {description || 'No description'}
+                {description ? chaptersInDescription(description, setVideoTime) : 'No description'}
             
}; @@ -133,6 +139,7 @@ function VideoPage({videoFile, prevFile, nextFile, setFavorite, ...props}) { id="player" playsInline={true} style={{maxWidth: '100%'}} + ref={videoRef} > @@ -189,3 +196,43 @@ function VideoPage({videoFile, prevFile, nextFile, setFavorite, ...props}) { } export default VideoPage; + +const chapterRegex = new RegExp('^(\\(?(?:((\\d?\\d):)?(?:(\\d?\\d):(\\d\\d)))\\)?)\\s+(.*)$', 'i'); + +function chaptersInDescription(description, setVideoTime) { + if (description && description.length > 0) { + try { + const lines = _.split(description, '\n'); + let newLines = []; + for (let i = 0; i <= lines.length; i++) { + const line = lines[i]; + const match = line ? line.match(chapterRegex) : null; + let link; + if (match) { + try { + const timestamp = match[1]; + const title = match[6]; + const [hour, minute, second] = match.slice(3, 6).map(i => parseInt(i) || 0); + const seconds = (((hour * 60) + minute) * 60) + second; + link = <> + setVideoTime(seconds)}>{timestamp} +  {title} + ; + } catch (e) { + console.error(e); + } + + newLines = [...newLines, link || line]; + } else if (line !== undefined) { + newLines = [...newLines, line]; + } + } + return <> + {newLines.map((i, idx) =>
{i}
)} + ; + } catch (e) { + console.error(e); + } + } + return description; +}