From d9a6de6fbc42b507b18ca73e54a283d2a19ac73b Mon Sep 17 00:00:00 2001 From: DIYgod Date: Fri, 22 Sep 2017 02:33:12 +0800 Subject: [PATCH] webvtt subtitle --- demo/index.html | 12 +++++++++++- package.json | 2 +- src/DPlayer.js | 7 ++++++- src/DPlayer.scss | 11 +++++++++++ src/html.js | 19 +++++++++++-------- src/options.js | 16 +++++++++++----- src/subtitle.js | 34 ++++++++++++++++++++++++++++++++++ 7 files changed, 85 insertions(+), 16 deletions(-) create mode 100644 src/subtitle.js diff --git a/demo/index.html b/demo/index.html index 1214d3800..f352dd550 100644 --- a/demo/index.html +++ b/demo/index.html @@ -20,7 +20,10 @@

Quick Start

video: { url: 'http://devtest.qiniudn.com/若能绽放光芒.mp4', pic: 'http://devtest.qiniudn.com/若能绽放光芒.png', - thumbnails: 'http://devtest.qiniudn.com/thumbnails.jpg' + thumbnails: 'http://devtest.qiniudn.com/thumbnails.jpg', + }, + subtitle: { + url: 'http://devtest.qiniudn.com/若能绽放光芒2.vtt' }, danmaku: { id: '9E2E3368B56CDBB4', @@ -58,6 +61,13 @@

Options

type: 'auto', thumbnails: 'http://devtest.qiniudn.com/thumbnails.jpg' }, + subtitle: { + url: 'http://devtest.qiniudn.com/若能绽放光芒2.vtt', + type: 'webvtt', + fontSize: '25px', + bottom: '10%', + color: '#b7daff' + }, danmaku: { id: '9E2E3368B56CDBB4', api: 'https://api.prprpr.me/dplayer/', diff --git a/package.json b/package.json index f6a6049c5..f7b83aa58 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "dplayer", - "version": "1.11.0", + "version": "1.12.0", "description": "Wow, such a lovely HTML5 danmaku video player", "main": "dist/DPlayer.min.js", "style": "dist/DPlayer.min.css", diff --git a/src/DPlayer.js b/src/DPlayer.js index c5b2e04be..23eac11cc 100644 --- a/src/DPlayer.js +++ b/src/DPlayer.js @@ -10,6 +10,7 @@ import Thumbnails from './thumbnails'; import Events from './events'; import FullScreen from './fullscreen'; import User from './user'; +import Subtitle from './subtitle'; let index = 0; @@ -996,6 +997,10 @@ class DPlayer { } this.volume(this.user.get('volume'), true); + + if (this.options.subtitle) { + this.subtitle = new Subtitle(this.container.getElementsByClassName('dplayer-subtitle')[0], this.video, this.options.subtitle); + } } switchQuality (index) { @@ -1011,7 +1016,7 @@ class DPlayer { const paused = this.video.paused; this.video.pause(); - const videoHTML = html.video(false, null, this.options.screenshot, 'auto', this.quality.url); + const videoHTML = html.video(false, null, this.options.screenshot, 'auto', this.quality.url, this.options.subtitle); const videoEle = new DOMParser().parseFromString(videoHTML, 'text/html').body.firstChild; const parent = this.container.getElementsByClassName('dplayer-video-wrap')[0]; parent.insertBefore(videoEle, parent.getElementsByTagName('div')[0]); diff --git a/src/DPlayer.scss b/src/DPlayer.scss index 5676b4fd4..a88e504fc 100644 --- a/src/DPlayer.scss +++ b/src/DPlayer.scss @@ -247,6 +247,17 @@ } } + .dplayer-subtitle { + position: absolute; + bottom: 40px; + width: 90%; + left: 5%; + text-align: center; + color: #fff; + text-shadow: 0.5px 0.5px 0.5px rgba(0, 0, 0, 0.5); + font-size: 20px; + } + .dplayer-bezel { position: absolute; left: 0; diff --git a/src/html.js b/src/html.js index 87559e33b..a1a74fd25 100644 --- a/src/html.js +++ b/src/html.js @@ -1,19 +1,17 @@ const svg = require('./svg.js'); const html = { - main: (options, index, tran) => { - let videos = ``; - videos += html.video(true, options.video.pic, options.screenshot, options.preload, options.video.url); - return ` + main: (options, index, tran) => `
- ${videos} + ${html.video(true, options.video.pic, options.screenshot, options.preload, options.video.url, options.subtitle)} ${options.logo ? ` ` : ``}
+
${options.danmaku ? `${tran('Danmaku is loading')}` : ``} @@ -166,8 +164,7 @@ const html = {
${html.contextmenuList(options.contextmenu, tran)} -
`; - }, +
`, danmakumargin: (margin) => { let result = ''; @@ -199,7 +196,13 @@ const html = { return result; }, - video: (current, pic, screenshot, preload, url) => ``, + video: (current, pic, screenshot, preload, url, subtitle) => { + const enableSubtitle = subtitle && subtitle.type === 'webvtt'; + return ` + `; + }, setting: (tran) => ({ 'original': ` diff --git a/src/options.js b/src/options.js index aaaeb59fe..91f9aa901 100644 --- a/src/options.js +++ b/src/options.js @@ -18,7 +18,7 @@ module.exports = (options) => { screenshot: false, hotkey: true, preload: 'auto', - volume: '0.7', + volume: 0.7, apiBackend: defaultApiBackend, video: {}, contextmenu: [] @@ -28,11 +28,17 @@ module.exports = (options) => { options[defaultKey] = defaultOption[defaultKey]; } } - if (options.video && !options.video.hasOwnProperty('type')) { - options.video.type = 'auto'; + if (options.video) { + !options.video.type && (options.video.type = 'auto'); } - if (options.danmaku && !options.danmaku.hasOwnProperty('user')) { - options.danmaku.user = 'DIYgod'; + if (options.danmaku) { + !options.danmaku.user && (options.danmaku.user = 'DIYgod'); + } + if (options.subtitle) { + !options.subtitle.type && (options.subtitle.type = 'webvtt'); + !options.subtitle.fontSize && (options.subtitle.fontSize = '20px'); + !options.subtitle.bottom && (options.subtitle.bottom = '40px'); + !options.subtitle.color && (options.subtitle.color = '#fff'); } if (options.video.quality) { diff --git a/src/subtitle.js b/src/subtitle.js new file mode 100644 index 000000000..b01d71278 --- /dev/null +++ b/src/subtitle.js @@ -0,0 +1,34 @@ +class Subtitle { + constructor (container, video, options) { + this.container = container; + this.video = video; + this.options = options; + + this.init(); + } + + init () { + this.container.style.fontSize = this.options.fontSize; + this.container.style.bottom = this.options.bottom; + this.container.style.color = this.options.color; + + if (this.video.textTracks && this.video.textTracks[0]) { + const track = this.video.textTracks[0]; + + track.oncuechange = () => { + const cue = track.activeCues[0]; + if (cue) { + this.container.innerHTML = ''; + const p = document.createElement('p'); + p.appendChild(cue.getCueAsHTML()); + this.container.appendChild(p); + } + else { + this.container.innerHTML = ''; + } + }; + } + } +} + +module.exports = Subtitle; \ No newline at end of file