From 1f6824c958440ace14f7c7f83c890b848a71f5d9 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Mon, 16 Jul 2018 16:13:35 +0200 Subject: Improve start time param Can handle 2m42s for example --- client/src/assets/player/peertube-player.ts | 2 +- .../src/assets/player/peertube-videojs-plugin.ts | 8 ++-- .../src/assets/player/peertube-videojs-typings.ts | 2 +- client/src/assets/player/utils.ts | 44 ++++++++++++++++++++-- client/src/standalone/videos/embed.ts | 6 +-- 5 files changed, 48 insertions(+), 14 deletions(-) (limited to 'client/src') diff --git a/client/src/assets/player/peertube-player.ts b/client/src/assets/player/peertube-player.ts index bf02ce91c..1fca6a7d2 100644 --- a/client/src/assets/player/peertube-player.ts +++ b/client/src/assets/player/peertube-player.ts @@ -32,7 +32,7 @@ function getVideojsOptions (options: { inactivityTimeout: number, peertubeLink: boolean, poster: string, - startTime: number + startTime: number | string theaterMode: boolean, videoCaptions: VideoJSCaption[], controls?: boolean, diff --git a/client/src/assets/player/peertube-videojs-plugin.ts b/client/src/assets/player/peertube-videojs-plugin.ts index 3f6fc4cc6..0dcbe49b1 100644 --- a/client/src/assets/player/peertube-videojs-plugin.ts +++ b/client/src/assets/player/peertube-videojs-plugin.ts @@ -4,7 +4,7 @@ import { VideoFile } from '../../../../shared/models/videos/video.model' import { renderVideo } from './video-renderer' import './settings-menu-button' import { PeertubePluginOptions, VideoJSCaption, VideoJSComponentInterface, videojsUntyped } from './peertube-videojs-typings' -import { isMobile, videoFileMaxByResolution, videoFileMinByResolution } from './utils' +import { isMobile, videoFileMaxByResolution, videoFileMinByResolution, timeToInt } from './utils' import * as CacheChunkStore from 'cache-chunk-store' import { PeertubeChunkStore } from './peertube-chunk-store' import { @@ -76,7 +76,7 @@ class PeerTubePlugin extends Plugin { // Disable auto play on iOS this.autoplay = options.autoplay && this.isIOS() === false - this.startTime = options.startTime + this.startTime = timeToInt(options.startTime) this.videoFiles = options.videoFiles this.videoViewUrl = options.videoViewUrl this.videoDuration = options.videoDuration @@ -264,8 +264,8 @@ class PeerTubePlugin extends Plugin { // Magnet hash is not up to date with the torrent file, add directly the torrent file if (err.message.indexOf('incorrect info hash') !== -1) { console.error('Incorrect info hash detected, falling back to torrent file.') - const options = { forcePlay: true } - return this.addTorrent(this.torrent['xs'], previousVideoFile, options, done) + const newOptions = { forcePlay: true, seek: options.seek } + return this.addTorrent(this.torrent['xs'], previousVideoFile, newOptions, done) } return console.warn(err) diff --git a/client/src/assets/player/peertube-videojs-typings.ts b/client/src/assets/player/peertube-videojs-typings.ts index 9c0299237..993d5ee6b 100644 --- a/client/src/assets/player/peertube-videojs-typings.ts +++ b/client/src/assets/player/peertube-videojs-typings.ts @@ -27,7 +27,7 @@ type PeertubePluginOptions = { playerElement: HTMLVideoElement videoViewUrl: string videoDuration: number - startTime: number + startTime: number | string autoplay: boolean, videoCaptions: VideoJSCaption[] } diff --git a/client/src/assets/player/utils.ts b/client/src/assets/player/utils.ts index c27e630e5..c02e19929 100644 --- a/client/src/assets/player/utils.ts +++ b/client/src/assets/player/utils.ts @@ -24,15 +24,50 @@ function isMobile () { } function buildVideoLink (time?: number) { - let href = window.location.href.replace('/embed/', '/watch/') + const baseEmbedPath = window.location.pathname.replace('/embed/', '/watch/') + const baseEmbedURL = window.location.origin + baseEmbedPath + if (time) { const timeInt = Math.floor(time) - if (window.location.search) href += '&start=' + timeInt - else href += '?start=' + timeInt + const params = new URLSearchParams(window.location.search) + params.set('start', secondsToTime(timeInt)) + + return baseEmbedURL + '?' + params.toString() } - return href + return baseEmbedURL +} + +function timeToInt (time: number | string) { + if (typeof time === 'number') return time + + const reg = /^((\d+)h)?((\d+)m)?((\d+)s?)?$/ + const matches = time.match(reg) + + if (!matches) return 0 + + const hours = parseInt(matches[2] || '0', 10) + const minutes = parseInt(matches[4] || '0', 10) + const seconds = parseInt(matches[6] || '0', 10) + + return hours * 3600 + minutes * 60 + seconds +} + +function secondsToTime (seconds: number) { + let time = '' + + let hours = Math.floor(seconds / 3600) + if (hours >= 1) time = hours + 'h' + + seconds %= 3600 + let minutes = Math.floor(seconds / 60) + if (minutes >= 1) time += minutes + 'm' + + seconds %= 60 + if (seconds >= 1) time += seconds + 's' + + return time } function buildVideoEmbed (embedUrl: string) { @@ -81,6 +116,7 @@ function videoFileMinByResolution (files: VideoFile[]) { export { toTitleCase, + timeToInt, buildVideoLink, buildVideoEmbed, videoFileMaxByResolution, diff --git a/client/src/standalone/videos/embed.ts b/client/src/standalone/videos/embed.ts index 1275998b8..b2809467d 100644 --- a/client/src/standalone/videos/embed.ts +++ b/client/src/standalone/videos/embed.ts @@ -159,7 +159,7 @@ class PeerTubeEmbed { muted = false loop = false enableApi = false - startTime = 0 + startTime: number | string = 0 scope = 'peertube' static async main () { @@ -246,9 +246,7 @@ class PeerTubeEmbed { this.scope = this.getParamString(params, 'scope', this.scope) const startTimeParamString = params.get('start') - const startTimeParamNumber = parseInt(startTimeParamString, 10) - - if (isNaN(startTimeParamNumber) === false) this.startTime = startTimeParamNumber + if (startTimeParamString) this.startTime = startTimeParamString } catch (err) { console.error('Cannot get params from URL.', err) } -- cgit v1.2.3