Set Video Playback Speed with JavaScript

By  on  

I love that media has moved from custom plugins (Flash…gross) to basic HTML <video> and <audio> elements. Treating these media sources as just another element allows us to use CSS filters to adjust display, for example. The less we need to do with ffmpeg or plugins, the better.

I’ve been noticing that many video sites have implemented a feature to adjust video playback speed, which is awesome if you’re trying to get through some sports highlights faster or to see that monster slam dunk in super slow motion. I was hoping it didn’t require some special type of server to send the file in slower or faster chunks and I was right: all you need is the playbackRate property!

const video = document.querySelector("video");

// Slow it to 50% speed
video.playbackRate = 0.5;

// Twice as fast!
video.playbackRate = 2;

// Back to normal
video.playbackRate = 1;

Using a number less than 1 slows the video down, a number larger than 1 speeds the video up, and 1 restores the video to normal speed. Also note that playbackRate is not a HTML attribute — it’s a property.

Adjusting playback rate isn’t something you’d want to do for all video sites, but if you think your users may like it, it’s only one HTML element property away!

Recent Features

  • By
    CSS Gradients

    With CSS border-radius, I showed you how CSS can bridge the gap between design and development by adding rounded corners to elements.  CSS gradients are another step in that direction.  Now that CSS gradients are supported in Internet Explorer 8+, Firefox, Safari, and Chrome...

  • By
    fetch API

    One of the worst kept secrets about AJAX on the web is that the underlying API for it, XMLHttpRequest, wasn't really made for what we've been using it for.  We've done well to create elegant APIs around XHR but we know we can do better.  Our effort to...

Incredible Demos

  • By
    Animated Progress Bars Using MooTools: dwProgressBar

    I love progress bars. It's important that I know roughly what percentage of a task is complete. I've created a highly customizable MooTools progress bar class that animates to the desired percentage. The Moo-Generated XHTML This DIV structure is extremely simple and can be controlled...

  • By
    Editable Content Using MooTools 1.2, PHP, and MySQL

    Everybody and their aerobics instructor wants to be able to edit their own website these days. And why wouldn't they? I mean, they have a $500 budget, no HTML/CSS experience, and extraordinary expectations. Enough ranting though. Having a website that allows for...

Discussion

  1. I created a little bookmarklet that helps me speed up the really slow videos on YoutTube

    javascript:document.querySelector('video').playbackRate = parseFloat(prompt());
    
  2. Of course it works great on as well!

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!