如何改变HTML5视频的播放速度?

发布于 2024-09-05 13:15:17 字数 140 浏览 5 评论 0 原文

如何更改 HTML5 中的视频播放速度?我已经检查了 w3school 中的视频标签的属性,但无法解决该问题。

How to change the video play speed in HTML5? I've checked video tag's attributes in w3school but couldn't approach that.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(17

柳若烟 2024-09-12 13:15:17

根据此网站,这在 playbackRatedefaultPlaybackRate 属性中得到支持,可通过 DOM 访问。示例:

/* play video twice as fast */
document.querySelector('video').defaultPlaybackRate = 2.0;
document.querySelector('video').play();

/* now play three times as fast just for the heck of it */
document.querySelector('video').playbackRate = 3.0;

上述适用于 Chrome 43+、Firefox 20+、IE 9+、Edge 12+。

According to this site, this is supported in the playbackRate and defaultPlaybackRate attributes, accessible via the DOM. Example:

/* play video twice as fast */
document.querySelector('video').defaultPlaybackRate = 2.0;
document.querySelector('video').play();

/* now play three times as fast just for the heck of it */
document.querySelector('video').playbackRate = 3.0;

The above works on Chrome 43+, Firefox 20+, IE 9+, Edge 12+.

一个人的旅程 2024-09-12 13:15:17

输入即可。

document.querySelector('video').playbackRate = 1.25;

只需在现代浏览器的 JS 控制台中

Just type

document.querySelector('video').playbackRate = 1.25;

in JS console of your modern browser.

公布 2024-09-12 13:15:17

(在 YouTube 上播放视频时在 Chrome 中进行了测试,但应该可以在任何地方工作 - 对于加快在线培训视频的速度特别有用)。

对于想要将这些作为“小书签”(包含 JavaScript 代码而不是 URL 的书签)添加到浏览器的人,请使用这些浏览器书签名称和 URL,并将以下每个书签添加到您的浏览器顶部浏览器。 复制下面每个书签的“URL”部分时,请将整个多行代码块、换行符和所有内容复制到浏览器中书签创建工具的“URL”字段中。

< a href="https://i.sstatic.net/WU46Y.png" rel="noreferrer">在此处输入图像描述

名称: 0.5x
网址:

javascript:

document.querySelector('video').playbackRate = 0.5;

名称: 1.0x
网址:

javascript:

document.querySelector('video').playbackRate = 1.0;

名称: 1.5x
网址:

javascript:

document.querySelector('video').playbackRate = 1.5;

名称: 2.0x
URL:

javascript:

document.querySelector('video').playbackRate = 2.0;

以下是我所有的播放速度书签:

我将上述所有播放速度书签以及更多内容添加到书签栏上名为 1.00x 的文件夹中,如下所示:

“在此处输入图像描述”"

参考文献:

  1. Jeremy Visser 的主要答案
  2. 从我的 GitHub 要点复制到此处:
    1. 也可以在此处获取其他书签,例如在 GitHub 上为您提供帮助的书签。

(Tested in Chrome while playing videos on YouTube, but should work anywhere--especially useful for speeding up online training videos).

For anyone wanting to add these as "bookmarklets" (bookmarks containing JavaScript code instead of URLs) to your browser, use these browser bookmark names and URLs, and add each of the following bookmarks to the top of your browser. When copying the "URL" portion of each bookmark below, copy the entire multi-line code block, new-lines and all, into the "URL" field of your bookmark creation tool in your browser.

enter image description here

Name: 0.5x
URL:

javascript:

document.querySelector('video').playbackRate = 0.5;

Name: 1.0x
URL:

javascript:

document.querySelector('video').playbackRate = 1.0;

Name: 1.5x
URL:

javascript:

document.querySelector('video').playbackRate = 1.5;

Name: 2.0x
URL:

javascript:

document.querySelector('video').playbackRate = 2.0;

Here are all of my playback-speed bookmarklets:

I added all of the above playback speed bookmarklets, and more, into a folder named 1.00x on my bookmark bar, as shown here:

enter image description here

References:

  1. The main answer by Jeremy Visser
  2. Copied from my GitHub gist here: https://gist.github.com/ElectricRCAircraftGuy/0a788876da1386ca0daecbe78b4feb44#other-bookmarklets
    1. Get other bookmarklets here too, such as for aiding you on GitHub.
公布 2024-09-12 13:15:17

解决

  1. 方案 dom 事件 onloadstart="this.playbackRate = 1.5;"
  <video
    onloadstart="this.playbackRate = 1.5;"
    controls
    src="https://cdn.xgqfrms.xyz/HTML5/video/controlslist.mp4">
  </video>
  1. js video.playbackRate = 1.5;
  <video
    id="custom-video"
    controls
    src="https://cdn.xgqfrms.xyz/HTML5/video/controlslist.mp4">
  </video>

const video = document.querySelector('#custom-video');

if(video) {
  video.playbackRate = 1.5;
}

演示

https://codepen.io/xgqfrms/pen/dydwzjp

solutions

  1. dom event onloadstart="this.playbackRate = 1.5;"
  <video
    onloadstart="this.playbackRate = 1.5;"
    controls
    src="https://cdn.xgqfrms.xyz/HTML5/video/controlslist.mp4">
  </video>
  1. js video.playbackRate = 1.5;
  <video
    id="custom-video"
    controls
    src="https://cdn.xgqfrms.xyz/HTML5/video/controlslist.mp4">
  </video>

const video = document.querySelector('#custom-video');

if(video) {
  video.playbackRate = 1.5;
}

demo

https://codepen.io/xgqfrms/pen/dydwzjp

软糯酥胸 2024-09-12 13:15:17

我更喜欢对视频速度进行更精细的调整。我喜欢能够根据命令加快和减慢视频速度。因此我使用这个:

window.addEventListener("keypress", function(e) {
  if(e.key==="d") document.getElementsByTagName("video")[0].playbackRate += .1; else if(e.key==="s") document.getElementsByTagName("video")[0].playbackRate -= .1;
}, false);

按 d 加速,按 s 减速。

I prefer having a more fine tuned approach for video speed. I like being able to speed up and slow down the video on command. Thus I use this:

window.addEventListener("keypress", function(e) {
  if(e.key==="d") document.getElementsByTagName("video")[0].playbackRate += .1; else if(e.key==="s") document.getElementsByTagName("video")[0].playbackRate -= .1;
}, false);

Press d to speed up, s to slow down.

憧憬巴黎街头的黎明 2024-09-12 13:15:17

您可以使用此代码:

var vid = document.getElementById("video1");

function slowPlaySpeed() { 
    vid.playbackRate = 0.5;
} 

function normalPlaySpeed() { 
    vid.playbackRate = 1;
} 

function fastPlaySpeed() { 
    vid.playbackRate = 2;
}

You can use this code:

var vid = document.getElementById("video1");

function slowPlaySpeed() { 
    vid.playbackRate = 0.5;
} 

function normalPlaySpeed() { 
    vid.playbackRate = 1;
} 

function fastPlaySpeed() { 
    vid.playbackRate = 2;
}
岁吢 2024-09-12 13:15:17

在 Chrome 中,创建一个新书签
输入图片此处描述

输入任意名称,例如速度选择器,然后在 URL javascript 中输入以下代码

var speed = prompt("Please enter speed", "1");
document.querySelector('video').playbackRate = speed,void(0);

然后当您单击此书签时,会出现一个弹出窗口,然后您可以输入视频的速度

在此处输入图像描述

In chrome, create a new bookmark
enter image description here

Enter an arbitarary name for example speed selector then Enter the following code in the URL

javascript:

var speed = prompt("Please enter speed", "1");
document.querySelector('video').playbackRate = speed,void(0);

then when you click on this bookmark, a popup window appears then you can enter the speed of video

enter image description here

乄_柒ぐ汐 2024-09-12 13:15:17
javascript:document.getElementsByClassName("video-stream html5-main-video")[0].playbackRate = 0.1;

您可以在此处输入任何数字,但不要输入太多,以免计算机过载。

javascript:document.getElementsByClassName("video-stream html5-main-video")[0].playbackRate = 0.1;

you can put any number here just don't go to far so you don't overun your computer.

醉南桥 2024-09-12 13:15:17

它总是有效你可以尝试这个

var vid = document.getElementById("myVideo");
vid.playbackRate = 0.5;

It works always you can try this

var vid = document.getElementById("myVideo");
vid.playbackRate = 0.5;
蹲墙角沉默 2024-09-12 13:15:17

如上所述,一般的解决方案是:

document.querySelector('video').playbackRate = 2.0;

但是,如果这返回一个错误,例如:

caught TypeError: Cannot set properties of null (setting 'playbackRate')

由于第一个函数返回 null(意味着它找不到指定的元素),那么简单的解决方案是使用典型的选择器( ID、类别等)。但我发现,当抛出此错误时,即使它不应该抛出错误,因为有一个视频元素并且它是页面上唯一的一个,问题与 tabindex 的 HTML 属性设置为“- 1”。只需使用浏览器检查来访问视频,然后删除“tabindex”属性即可修复如上所示的错误。

在这里看到是“tabindex=-1”,删除它将使控制台能够找到该元素

As posted above, the general solution to this is:

document.querySelector('video').playbackRate = 2.0;

However, if this returns an error like:

caught TypeError: Cannot set properties of null (setting 'playbackRate')

due to the first function returning null (meaning it couldn't find the element specified), then the simple solution is to use a typical selector (ID, Class, etc.). But what I've found is that when this error is thrown even though it should not throw an error as there's a video element and it's the only one on the page, the issue is related to the HTML property of tabindex being set to "-1". Simply using browser inspect to get to the video, then removing the "tabindex" property can fix an error as displayed above.

seen here is "tabindex=-1", removing this will allow the console to locate the element

遗弃M 2024-09-12 13:15:17

假设您的视频/音频 id 是 myVideo,那么您可以简单地使用 JavaScript 来完成您想做的事情,只需键入以下简单的 JS 代码: -playbackspeed

var vid = document.getElementById("myVideo");
vid.playbackRate = 0.5;`

That will decrease the speed of your video/audio to it's half speed.

指示

音频/的当前播放速度视频。

示例值:

1.0 是正常速度

0.5 是半速(较慢)

2.0 是双倍速度(较快)

-1.0 是向后,正常速度

-0.5 是向后,半速

来源: w3schools.com

suppose that your video/audio id is myVideo, then you can simply use JavaScript for doing that you wanna do, By just typing the following simple JS code:-

var vid = document.getElementById("myVideo");
vid.playbackRate = 0.5;`

That will decrease the speed of your video/audio to it's half speed.

playbackspeed

Indicates the current playback speed of the audio/video.

Example values:

1.0 is normal speed

0.5 is half speed (slower)

2.0 is double speed (faster)

-1.0 is backwards, normal speed

-0.5 is backwards, half speed

source: w3schools.com

时间海 2024-09-12 13:15:17

只需在浏览器的 JavaScript 控制台中键入以下命令:

document.querySelector('video').playbackRate = 2.0;

在此处输入图像描述

您可以通过从右键菜单中选择检查选项来获取它,如下所示:
输入图片此处描述

Just type the following command in the javascript console of your browser:

document.querySelector('video').playbackRate = 2.0;

enter image description here

You can get it by choosing the inspect option from the right-click menu as follows:
enter image description here

涙—继续流 2024-09-12 13:15:17

右键单击

Firefox 速度控制上下文菜单

Firefox has a speed control context menu when you right-click

Firefox speed control context menu.

清风疏影 2024-09-12 13:15:17

如果页面上有多个视频,则大多数其他答案只会更改第一个视频。

javascript:document.querySelectorAll('video').forEach( (vid) => vid.playbackRate = 1.5 );

^^ 此书签将加快打开页面上所有视频的速度。

If there are multiple videos on the page, most of other answers will only change the first one.

javascript:document.querySelectorAll('video').forEach( (vid) => vid.playbackRate = 1.5 );

^^ this bookmarklet will speed up all videos on the open page.

一直在等你来 2024-09-12 13:15:17

使用反应:

onLoadStart={(e) => {
    e.target.playbackRate = 1.5;
}}

using React:

onLoadStart={(e) => {
    e.target.playbackRate = 1.5;
}}
伪心 2024-09-12 13:15:17

<视频
src={USERBG}
自动播放
环形
静音
控制
onLoadStart={(e) =>; (e.target.playbackRate = 0.5)}
className="object-cover object-center w-full h-full 固定 top-0 left-0 z-10"
>

<video
src={USERBG}
autoPlay
loop
muted
controls
onLoadStart={(e) => (e.target.playbackRate = 0.5)}
className="object-cover object-center w-full h-full fixed top-0 left-0 z-10"
>

少钕鈤記 2024-09-12 13:15:17

document.querySelector('video').playbackRate = 16.0;
在 Chrome 2024 中最大速度 16

document.querySelector('video').playbackRate = 16.0;
In Chrome 2024 max speed 16

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文