🎨Optimize video player

This commit is contained in:
Aicirou 2020-07-04 21:19:31 +08:00
parent 0aab140452
commit d1523e749d
5 changed files with 175 additions and 121 deletions

View File

@ -1,5 +1,5 @@
var authConfig = { var authConfig = {
version: '1.1.1', version: "1.1.1",
roots: [ roots: [
{ {
id: "", id: "",
@ -15,12 +15,12 @@ var authConfig = {
id: "", id: "",
name: "folder1", name: "folder1",
pass: "", pass: "",
} },
], ],
}; };
var themeOptions = { var themeOptions = {
// en/zh-chs/zh-cht // en/zh-chs/zh-cht
languages: 'en', languages: "en",
render: { render: {
head_md: true, head_md: true,
readme_md: true, readme_md: true,
@ -28,14 +28,14 @@ var authConfig = {
// Show file/folder description or not (not shown by default) // Show file/folder description or not (not shown by default)
desc: true, desc: true,
}, },
player: { video: {
api: "", api: "",
autoplay: true autoplay: true,
} },
} };
window.gdconfig = JSON.parse(JSON.stringify({ version: authConfig.version, themeOptions: themeOptions })); window.gdconfig = JSON.parse(
window.themeOptions = themeOptions; JSON.stringify({ version: authConfig.version, themeOptions: themeOptions })
window.gds = JSON.parse(
JSON.stringify(authConfig.roots.map((it) => it.name))
); );
window.themeOptions = themeOptions;
window.gds = JSON.parse(JSON.stringify(authConfig.roots.map((it) => it.name)));
window.current_drive_order = 0; window.current_drive_order = 0;

34
src/plugin/vplayer/flv.js Normal file
View File

@ -0,0 +1,34 @@
import flvjs from "flv.js";
function FlvPlayer({
src,
autoplay = false,
media,
options = {
type: "flv",
url: src,
},
callback = () => ({}),
}) {
if (flvjs.isSupported()) {
var player = flvjs.createPlayer(options);
player.attachMediaElement(media);
player.load();
player.on(flvjs.Events.ERROR, (event) => {
switch (event) {
case flvjs.ErrorTypes.NETWORK_ERROR:
player.load();
break;
default:
player.destroy();
break;
}
});
if (autoplay) {
player.play();
}
callback(player);
}
}
export default FlvPlayer;

32
src/plugin/vplayer/hls.js Normal file
View File

@ -0,0 +1,32 @@
import Hls from "hls.js";
function HlsPlayer({ src, autoplay = false, media, callback = () => ({}) }) {
// eslint-disable-next-line no-undef
if (Hls.isSupported()) {
// For more Hls.js options, see https://github.com/dailymotion/hls.js
// eslint-disable-next-line no-undef
const hls = new Hls();
hls.loadSource(src);
hls.attachMedia(media);
hls.on(Hls.Events.ERROR, function(event, data) {
if (data.fatal) {
switch (data.type) {
case Hls.ErrorTypes.NETWORK_ERROR:
hls.startLoad();
break;
default:
hls.destroy();
break;
}
}
});
if (autoplay) {
hls.on(Hls.Events.MANIFEST_PARSED, function() {
media.play();
});
}
callback(hls);
}
}
export default HlsPlayer;

View File

@ -0,0 +1,4 @@
import Flv from "./flv";
import Hls from "./hls";
export { Flv, Hls };

View File

@ -1,10 +1,10 @@
<template> <template>
<div class="content g2-content"> <div class="content g2-content">
<div v-if="player && player.api" class="video-content"> <div v-if="options && options.api" class="video-content">
<iframe <iframe
width="100%" width="100%"
height="100%" height="100%"
:src="apiUrl" :src="apiVideoUrl"
frameborder="0" frameborder="0"
border="0" border="0"
marginwidth="0" marginwidth="0"
@ -15,16 +15,10 @@
></iframe> ></iframe>
</div> </div>
<div v-else> <div v-else>
<vue-plyr ref="plyr" :options="{ autoplay: player.autoplay }"> <vue-plyr ref="plyr" :options="options">
<video> <video controls crossorigin playsinline>
<source :src="videoUrl" type="video/mp4" /> <source :src="videoUrl" type="video/mp4" />
<!-- <track <track kind="captions" :src="subtitle" default />
kind="captions"
label="English"
srclang="en"
src="captions-en.vtt"
default
/> -->
</video> </video>
</vue-plyr> </vue-plyr>
</div> </div>
@ -46,7 +40,7 @@
<div class="field"> <div class="field">
<label class="label">{{ $t("page.video.link") }}</label> <label class="label">{{ $t("page.video.link") }}</label>
<div class="control"> <div class="control">
<input class="input" type="text" :value="videoUrl" /> <input class="input" type="text" :value="downloadUrl" />
</div> </div>
</div> </div>
<div class="columns is-mobile is-multiline has-text-centered"> <div class="columns is-mobile is-multiline has-text-centered">
@ -73,94 +67,84 @@
<script> <script>
import { decode64 } from "@utils/AcrouUtil"; import { decode64 } from "@utils/AcrouUtil";
import Hls from "hls.js"; import { Hls, Flv } from "@/plugin/vplayer";
import flvjs from "flv.js";
export default { export default {
data: function() { data: function() {
return { return {
apiUrl: "", apiVideoUrl: "",
player: window.themeOptions.player,
videoUrl: "", videoUrl: "",
downloadUrl: "",
subtitle: "",
}; };
}, },
mounted() {
this.render();
},
methods: { methods: {
render() { render() {
let path = encodeURI(this.url); let path = encodeURI(this.url);
let index = path.lastIndexOf("."); let index = path.lastIndexOf(".");
this.suffix = path.substring(index + 1, path.length); this.suffix = path.substring(index + 1, path.length);
this.videoUrl = window.location.origin + encodeURI(this.url); this.loadSub(path, index);
this.apiUrl = this.player.api + this.videoUrl; this.videoUrl = path;
if (!this.player || !this.player.api) { this.downloadUrl = window.location.origin + path;
let options = { src: this.videoUrl, media: this.plyr.media }; this.apiVideoUrl = this.options.api + this.videoUrl;
if (!this.options.api) {
let options = {
src: this.videoUrl,
autoplay: this.options.autoplay,
media: this.player.media,
};
if (this.suffix === "m3u8") { if (this.suffix === "m3u8") {
this.hls(options); Hls({
} else if (this.suffix === "flv") { ...options,
this.flv(options); callback: (hls) => {
}
}
},
hls({ src, media }) {
// eslint-disable-next-line no-undef
if (Hls.isSupported()) {
// For more Hls.js options, see https://github.com/dailymotion/hls.js
// eslint-disable-next-line no-undef
const hls = new Hls();
hls.loadSource(src);
hls.attachMedia(media);
hls.on(Hls.Events.ERROR, function(event, data) {
if (data.fatal) {
switch (data.type) {
case Hls.ErrorTypes.NETWORK_ERROR:
hls.startLoad();
break;
default:
hls.destroy();
break;
}
}
});
if (this.player.autoplay) {
hls.on(Hls.Events.MANIFEST_PARSED, function() {
media.play();
});
}
window.hls = hls;
// Handle changing captions // Handle changing captions
this.plyr.on("languagechange", () => { this.player.on("languagechange", () => {
// Caption support is still flaky. See: https://github.com/sampotts/plyr/issues/994 // Caption support is still flaky. See: https://github.com/sampotts/plyr/issues/994
setTimeout(() => (hls.subtitleTrack = this.plyr.currentTrack), 50); setTimeout(
() => (hls.subtitleTrack = this.player.currentTrack),
50
);
}); });
}
}, },
flv({ src, media }) {
if (flvjs.isSupported()) {
var flvPlayer = flvjs.createPlayer({
type: "flv",
url: src,
}); });
flvPlayer.attachMediaElement(media); } else if (this.suffix === "flv") {
flvPlayer.load(); Flv(options);
flvPlayer.on(flvjs.Events.ERROR, (event) => {
switch (event) {
case flvjs.ErrorTypes.NETWORK_ERROR:
flvPlayer.load();
break;
default:
flvPlayer.destroy();
break;
}
});
if (this.player.autoplay) {
flvPlayer.play();
} }
} }
}, },
loadSub(path, index) {
this.subtitle = path.substring(0, index) + ".vtt";
}, },
activated() {
this.render();
}, },
computed: { computed: {
plyr() { options() {
var options = window.themeOptions.video;
return {
...options,
autoplay: options.autoplay || false,
invertTime: options.invertTime || false,
controls: options.controls || [
"play-large",
"restart",
"play",
"progress",
"current-time",
"duration",
"mute",
"volume",
"captions",
"settings",
"pip",
"airplay",
"download",
"fullscreen",
],
settings: options.settings || ["quality", "speed", "loop"]
};
},
player() {
return this.$refs.plyr.player; return this.$refs.plyr.player;
}, },
url() { url() {