diff options
author | Chocobozzz <me@florianbigard.com> | 2018-07-16 16:13:35 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2018-07-16 16:13:35 +0200 |
commit | 1f6824c958440ace14f7c7f83c890b848a71f5d9 (patch) | |
tree | f8bd7853f4283bfe72745f5ac491b93a3e618276 | |
parent | 329d9086601b7ae2127be12166ee8c892c289c6e (diff) | |
download | PeerTube-1f6824c958440ace14f7c7f83c890b848a71f5d9.tar.gz PeerTube-1f6824c958440ace14f7c7f83c890b848a71f5d9.tar.zst PeerTube-1f6824c958440ace14f7c7f83c890b848a71f5d9.zip |
Improve start time param
Can handle 2m42s for example
-rw-r--r-- | client/src/assets/player/peertube-player.ts | 2 | ||||
-rw-r--r-- | client/src/assets/player/peertube-videojs-plugin.ts | 8 | ||||
-rw-r--r-- | client/src/assets/player/peertube-videojs-typings.ts | 2 | ||||
-rw-r--r-- | client/src/assets/player/utils.ts | 44 | ||||
-rw-r--r-- | client/src/standalone/videos/embed.ts | 6 |
5 files changed, 48 insertions, 14 deletions
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: { | |||
32 | inactivityTimeout: number, | 32 | inactivityTimeout: number, |
33 | peertubeLink: boolean, | 33 | peertubeLink: boolean, |
34 | poster: string, | 34 | poster: string, |
35 | startTime: number | 35 | startTime: number | string |
36 | theaterMode: boolean, | 36 | theaterMode: boolean, |
37 | videoCaptions: VideoJSCaption[], | 37 | videoCaptions: VideoJSCaption[], |
38 | controls?: boolean, | 38 | 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' | |||
4 | import { renderVideo } from './video-renderer' | 4 | import { renderVideo } from './video-renderer' |
5 | import './settings-menu-button' | 5 | import './settings-menu-button' |
6 | import { PeertubePluginOptions, VideoJSCaption, VideoJSComponentInterface, videojsUntyped } from './peertube-videojs-typings' | 6 | import { PeertubePluginOptions, VideoJSCaption, VideoJSComponentInterface, videojsUntyped } from './peertube-videojs-typings' |
7 | import { isMobile, videoFileMaxByResolution, videoFileMinByResolution } from './utils' | 7 | import { isMobile, videoFileMaxByResolution, videoFileMinByResolution, timeToInt } from './utils' |
8 | import * as CacheChunkStore from 'cache-chunk-store' | 8 | import * as CacheChunkStore from 'cache-chunk-store' |
9 | import { PeertubeChunkStore } from './peertube-chunk-store' | 9 | import { PeertubeChunkStore } from './peertube-chunk-store' |
10 | import { | 10 | import { |
@@ -76,7 +76,7 @@ class PeerTubePlugin extends Plugin { | |||
76 | // Disable auto play on iOS | 76 | // Disable auto play on iOS |
77 | this.autoplay = options.autoplay && this.isIOS() === false | 77 | this.autoplay = options.autoplay && this.isIOS() === false |
78 | 78 | ||
79 | this.startTime = options.startTime | 79 | this.startTime = timeToInt(options.startTime) |
80 | this.videoFiles = options.videoFiles | 80 | this.videoFiles = options.videoFiles |
81 | this.videoViewUrl = options.videoViewUrl | 81 | this.videoViewUrl = options.videoViewUrl |
82 | this.videoDuration = options.videoDuration | 82 | this.videoDuration = options.videoDuration |
@@ -264,8 +264,8 @@ class PeerTubePlugin extends Plugin { | |||
264 | // Magnet hash is not up to date with the torrent file, add directly the torrent file | 264 | // Magnet hash is not up to date with the torrent file, add directly the torrent file |
265 | if (err.message.indexOf('incorrect info hash') !== -1) { | 265 | if (err.message.indexOf('incorrect info hash') !== -1) { |
266 | console.error('Incorrect info hash detected, falling back to torrent file.') | 266 | console.error('Incorrect info hash detected, falling back to torrent file.') |
267 | const options = { forcePlay: true } | 267 | const newOptions = { forcePlay: true, seek: options.seek } |
268 | return this.addTorrent(this.torrent['xs'], previousVideoFile, options, done) | 268 | return this.addTorrent(this.torrent['xs'], previousVideoFile, newOptions, done) |
269 | } | 269 | } |
270 | 270 | ||
271 | return console.warn(err) | 271 | 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 = { | |||
27 | playerElement: HTMLVideoElement | 27 | playerElement: HTMLVideoElement |
28 | videoViewUrl: string | 28 | videoViewUrl: string |
29 | videoDuration: number | 29 | videoDuration: number |
30 | startTime: number | 30 | startTime: number | string |
31 | autoplay: boolean, | 31 | autoplay: boolean, |
32 | videoCaptions: VideoJSCaption[] | 32 | videoCaptions: VideoJSCaption[] |
33 | } | 33 | } |
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 () { | |||
24 | } | 24 | } |
25 | 25 | ||
26 | function buildVideoLink (time?: number) { | 26 | function buildVideoLink (time?: number) { |
27 | let href = window.location.href.replace('/embed/', '/watch/') | 27 | const baseEmbedPath = window.location.pathname.replace('/embed/', '/watch/') |
28 | const baseEmbedURL = window.location.origin + baseEmbedPath | ||
29 | |||
28 | if (time) { | 30 | if (time) { |
29 | const timeInt = Math.floor(time) | 31 | const timeInt = Math.floor(time) |
30 | 32 | ||
31 | if (window.location.search) href += '&start=' + timeInt | 33 | const params = new URLSearchParams(window.location.search) |
32 | else href += '?start=' + timeInt | 34 | params.set('start', secondsToTime(timeInt)) |
35 | |||
36 | return baseEmbedURL + '?' + params.toString() | ||
33 | } | 37 | } |
34 | 38 | ||
35 | return href | 39 | return baseEmbedURL |
40 | } | ||
41 | |||
42 | function timeToInt (time: number | string) { | ||
43 | if (typeof time === 'number') return time | ||
44 | |||
45 | const reg = /^((\d+)h)?((\d+)m)?((\d+)s?)?$/ | ||
46 | const matches = time.match(reg) | ||
47 | |||
48 | if (!matches) return 0 | ||
49 | |||
50 | const hours = parseInt(matches[2] || '0', 10) | ||
51 | const minutes = parseInt(matches[4] || '0', 10) | ||
52 | const seconds = parseInt(matches[6] || '0', 10) | ||
53 | |||
54 | return hours * 3600 + minutes * 60 + seconds | ||
55 | } | ||
56 | |||
57 | function secondsToTime (seconds: number) { | ||
58 | let time = '' | ||
59 | |||
60 | let hours = Math.floor(seconds / 3600) | ||
61 | if (hours >= 1) time = hours + 'h' | ||
62 | |||
63 | seconds %= 3600 | ||
64 | let minutes = Math.floor(seconds / 60) | ||
65 | if (minutes >= 1) time += minutes + 'm' | ||
66 | |||
67 | seconds %= 60 | ||
68 | if (seconds >= 1) time += seconds + 's' | ||
69 | |||
70 | return time | ||
36 | } | 71 | } |
37 | 72 | ||
38 | function buildVideoEmbed (embedUrl: string) { | 73 | function buildVideoEmbed (embedUrl: string) { |
@@ -81,6 +116,7 @@ function videoFileMinByResolution (files: VideoFile[]) { | |||
81 | 116 | ||
82 | export { | 117 | export { |
83 | toTitleCase, | 118 | toTitleCase, |
119 | timeToInt, | ||
84 | buildVideoLink, | 120 | buildVideoLink, |
85 | buildVideoEmbed, | 121 | buildVideoEmbed, |
86 | videoFileMaxByResolution, | 122 | 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 { | |||
159 | muted = false | 159 | muted = false |
160 | loop = false | 160 | loop = false |
161 | enableApi = false | 161 | enableApi = false |
162 | startTime = 0 | 162 | startTime: number | string = 0 |
163 | scope = 'peertube' | 163 | scope = 'peertube' |
164 | 164 | ||
165 | static async main () { | 165 | static async main () { |
@@ -246,9 +246,7 @@ class PeerTubeEmbed { | |||
246 | this.scope = this.getParamString(params, 'scope', this.scope) | 246 | this.scope = this.getParamString(params, 'scope', this.scope) |
247 | 247 | ||
248 | const startTimeParamString = params.get('start') | 248 | const startTimeParamString = params.get('start') |
249 | const startTimeParamNumber = parseInt(startTimeParamString, 10) | 249 | if (startTimeParamString) this.startTime = startTimeParamString |
250 | |||
251 | if (isNaN(startTimeParamNumber) === false) this.startTime = startTimeParamNumber | ||
252 | } catch (err) { | 250 | } catch (err) { |
253 | console.error('Cannot get params from URL.', err) | 251 | console.error('Cannot get params from URL.', err) |
254 | } | 252 | } |