Improve start time param

Can handle 2m42s for example
This commit is contained in:
Chocobozzz 2018-07-16 16:13:35 +02:00
parent 329d908660
commit 1f6824c958
No known key found for this signature in database
GPG Key ID: 583A612D890159BE
5 changed files with 48 additions and 14 deletions

View File

@ -32,7 +32,7 @@ function getVideojsOptions (options: {
inactivityTimeout: number, inactivityTimeout: number,
peertubeLink: boolean, peertubeLink: boolean,
poster: string, poster: string,
startTime: number startTime: number | string
theaterMode: boolean, theaterMode: boolean,
videoCaptions: VideoJSCaption[], videoCaptions: VideoJSCaption[],
controls?: boolean, controls?: boolean,

View File

@ -4,7 +4,7 @@ import { VideoFile } from '../../../../shared/models/videos/video.model'
import { renderVideo } from './video-renderer' import { renderVideo } from './video-renderer'
import './settings-menu-button' import './settings-menu-button'
import { PeertubePluginOptions, VideoJSCaption, VideoJSComponentInterface, videojsUntyped } from './peertube-videojs-typings' 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 * as CacheChunkStore from 'cache-chunk-store'
import { PeertubeChunkStore } from './peertube-chunk-store' import { PeertubeChunkStore } from './peertube-chunk-store'
import { import {
@ -76,7 +76,7 @@ class PeerTubePlugin extends Plugin {
// Disable auto play on iOS // Disable auto play on iOS
this.autoplay = options.autoplay && this.isIOS() === false this.autoplay = options.autoplay && this.isIOS() === false
this.startTime = options.startTime this.startTime = timeToInt(options.startTime)
this.videoFiles = options.videoFiles this.videoFiles = options.videoFiles
this.videoViewUrl = options.videoViewUrl this.videoViewUrl = options.videoViewUrl
this.videoDuration = options.videoDuration 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 // Magnet hash is not up to date with the torrent file, add directly the torrent file
if (err.message.indexOf('incorrect info hash') !== -1) { if (err.message.indexOf('incorrect info hash') !== -1) {
console.error('Incorrect info hash detected, falling back to torrent file.') console.error('Incorrect info hash detected, falling back to torrent file.')
const options = { forcePlay: true } const newOptions = { forcePlay: true, seek: options.seek }
return this.addTorrent(this.torrent['xs'], previousVideoFile, options, done) return this.addTorrent(this.torrent['xs'], previousVideoFile, newOptions, done)
} }
return console.warn(err) return console.warn(err)

View File

@ -27,7 +27,7 @@ type PeertubePluginOptions = {
playerElement: HTMLVideoElement playerElement: HTMLVideoElement
videoViewUrl: string videoViewUrl: string
videoDuration: number videoDuration: number
startTime: number startTime: number | string
autoplay: boolean, autoplay: boolean,
videoCaptions: VideoJSCaption[] videoCaptions: VideoJSCaption[]
} }

View File

@ -24,15 +24,50 @@ function isMobile () {
} }
function buildVideoLink (time?: number) { 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) { if (time) {
const timeInt = Math.floor(time) const timeInt = Math.floor(time)
if (window.location.search) href += '&start=' + timeInt const params = new URLSearchParams(window.location.search)
else href += '?start=' + timeInt 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) { function buildVideoEmbed (embedUrl: string) {
@ -81,6 +116,7 @@ function videoFileMinByResolution (files: VideoFile[]) {
export { export {
toTitleCase, toTitleCase,
timeToInt,
buildVideoLink, buildVideoLink,
buildVideoEmbed, buildVideoEmbed,
videoFileMaxByResolution, videoFileMaxByResolution,

View File

@ -159,7 +159,7 @@ class PeerTubeEmbed {
muted = false muted = false
loop = false loop = false
enableApi = false enableApi = false
startTime = 0 startTime: number | string = 0
scope = 'peertube' scope = 'peertube'
static async main () { static async main () {
@ -246,9 +246,7 @@ class PeerTubeEmbed {
this.scope = this.getParamString(params, 'scope', this.scope) this.scope = this.getParamString(params, 'scope', this.scope)
const startTimeParamString = params.get('start') const startTimeParamString = params.get('start')
const startTimeParamNumber = parseInt(startTimeParamString, 10) if (startTimeParamString) this.startTime = startTimeParamString
if (isNaN(startTimeParamNumber) === false) this.startTime = startTimeParamNumber
} catch (err) { } catch (err) {
console.error('Cannot get params from URL.', err) console.error('Cannot get params from URL.', err)
} }