只要浏览器播放视频时用的是<video>
,而且当前页面只有这一个视频,就可以用这个脚本。
刚好也是世界杯 CSDN 博客的活动了,虽然对活动没什么兴趣但是还是写一句和活动相关的话吧——世界杯为什么要看直播呢!不如一起来倍速观看世界杯吧!
好吧,其实这是我很久以前为了倍速看MOOC视频,所以写的一个脚本,一直没有专门写博客去介绍它。刚刚,我想倍速看新闻联播,才发现忘记写博客了。
浏览器视频倍速播放的原理非常容易,我也是看了一下网页源代码才发现的。自从发现了这一点之后,我连本地视频都特别喜欢直接用浏览器播放。
文章目录
- 脚本链接
- 脚本功能
- 脚本代码
- 脚本原理
脚本链接
Greasy Fork 链接:https://greasyfork.org/zh-CN/scripts/449828-任意视频倍速播放。
注:如果你的浏览器有可以运行 JS 脚本的扩展插件,那么点击即可安装。
脚本功能
PC端,任意浏览器视频倍速播放,按键调速。
其实除了倍速播放,还有调进度、调音量的功能。这是因为我觉得挺方便,就一块儿加上了。
① +/-0.25倍速:按w/s键;
② 切换整数倍速:按1/2/3/4键;
③ +/-0.5s:按→/←键;
④ +/-10%音量:按↑/↓键。
注:目前测试了mooc、新闻联播网站、本地视频,都可以,如果有不可以的网站请反馈给我。
注:用浏览器播放本地视频,需要将扩展插件设置为“允许访问文件URL”。
脚本代码
// ==UserScript==
// @name 任意视频倍速播放
// @namespace http://tampermonkey.net/
// @version 0.2
// @description 任意浏览器视频倍速播放,按键调速。
// @author shandianchengzi
// @include *
// @icon data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMgAAADICAYAAACtWK6eAAAAAXNSR0IArs4c6QAAGJJJREFUeF7tnQm0dlVZx//OSAqSiZaiUgFmWgmlYipiTiQOOZSZppYJClo5gqaVE4pTpZiapWaDiZmahVNqRuKAmlYWYEKSpVALIcXUrNZvtc/i/e53793PHs4++5zzPGu9673fd/b43+f/7ukZriQXR8AR2BGBKzk2joAjsDMCThB/OxyBXRBwgvjr4Qg4QfwdcATyEPAZJA83z7USBJwgKxlo72YeAk6QPNw810oQcIKsZKC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Frc3A2FYimDcxQePkcPffCMYHA2ffTb+Zuk4EGIKt0eoirwskGMRm/DYYK6JIAMWmIJyrPmIETbyMbzn+pwN+GvCsTemzauRNRJkGFw27wNR2MS67I0AhxADMdiMr07WTJDNwX5wIEuzyEWdv2kEteGSlPgdqxYnyJ7Df8zG5VmJSv0cXyr2F1yI8jljjh0Yo81OkO1RRRGSy7Ths98Y4HdQ5mUbpIAYs1QoHBNHJ0gcXe4RNskSz9F/imGm4Hv0UMr9w7FzC50gaaPHsuvI8CHwPX/PQfCPix9jvvl4UFTjqDlBjEDtkAwXqRgVbX7KSqyTGwOwzQ8uPV0yEHCCZIAWyYJm7KFBQ3bz7+tXruqLktA2Pjd8b/5duar1FucEaTf2zDYQhotK1EH493bftAh1FX71t/vmog4y+KzQYOycIA1A9irmi4ATZL5j5y1vgIATpAHIXsV8EXCCzHfsvOUNEHCCNADZq5gvAk6Q+Y6dt7wBAk6QBiB7FfNFwAky37HzljdAwAnSAGSvYr4IOEHmO3be8gYIOEEagOxVzBcBJ8h8x85b3gABJ0gDkL2K+SLgBJnv2HnLGyDwf9V/IQWblhPcAAAAAElFTkSuQmCC
// @license MIT
// @grant none
// ==/UserScript==
//轻提醒
function Toast(msg, duration) {
let p1 = new Promise((resolve,reject)=>{
duration = isNaN(duration) ? 3000 : duration;
var m = document.createElement('div');
m.innerHTML = msg;
m.style.cssText = "font-family:siyuan;max-width:60%;min-width: 150px;padding:0 14px;height: 40px;color: rgb(255, 255, 255);line-height: 40px;text-align: center;border-radius: 4px;position: fixed;top: 50%;left: 50%;transform: translate(-50%, -50%);z-index: 999999;background: rgba(0, 0, 0,.7);font-size: 16px;";
document.body.appendChild(m);
setTimeout(function() {
var d = 0.5;
m.style.webkitTransition = '-webkit-transform ' + d + 's ease-in, opacity ' + d + 's ease-in';
m.style.opacity = '0';
setTimeout(function() {
document.body.removeChild(m)
}, d * 1000);
}, duration);
});
}
async function mainFunc(){
document.body.onkeydown = function(ev) {
var e = ev || event;
let video = document.getElementsByTagName('video')[0]
console.log('test');
if(video){
switch(e.keyCode){
case 87: //w键
video.playbackRate += 0.25;
Toast(video.playbackRate,100);
break;
case 83: //s键
video.playbackRate -= 0.25
Toast(video.playbackRate,100);
break;
case 39: //→
video.currentTime += 5;
break;
case 37: //←
video.currentTime -= 5;
break;
case 38: //↑
video.volume += 0.1;
Toast(video.volume,100);
break;
case 40: //↓
video.volume -= 0.1;
Toast(video.volume,100);
break;
case 49: //1
video.playbackRate = 1;
Toast(video.playbackRate,100);
break;
case 50: //2
video.playbackRate = 2;
Toast(video.playbackRate,100);
break;
case 51: //3
video.playbackRate = 3;
Toast(video.playbackRate,100);
break;
case 52: //4
video.playbackRate = 4;
Toast(video.playbackRate,100);
break;
default:
return e;
}
}
}
}
(function() {
'use strict';
window.onhashchange=mainFunc;
mainFunc();
// Your code here...
})();
脚本原理
原理究极无敌简单,就是修改video
这个DOM元素的playbackRate
或其他属性。
唯一体现了一点点难度的,可能就只是调整之后弹出来的Toast
轻提醒,如下:
我今天上号发现安装量还挺不错的~!说明蛮多人都有按键播放需求。所以来补一个博客,引一下流(bushi)。
如果后续代码有更新,我会优先更新 Greasy Fork 里的代码和介绍,虽说更新的几率微乎其微。转载或修改请标明出处:https://greasyfork.org/zh-CN/scripts/449828-任意视频倍速播放。