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,
peertubeLink: boolean,
poster: string,
startTime: number
startTime: number | string
theaterMode: boolean,
videoCaptions: VideoJSCaption[],
controls?: boolean,

View File

@ -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)

View File

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

View File

@ -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,

View File

@ -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)
}