Modify Video Speed with ffmpeg

By  on  

I watch a lot of sports and recently I've become fascinated with some of the methodologies they use to illustrate events within the game.  I don't have their advanced TV software or hardware, of course, but I do have a love for ffmpeg, which allows me to do everything from clip videos to change video formats, create tacky highlight videos with emo techno music combine audio and video, and more.

One of my favorite TV sports illustration techniques is speeding video up (time-lapse) or slowing it way down (slo-mo replay); naturally I wanted to know how to manipulate video speed with ffmpeg.  It turns out all you need to do is pass in filter with a PTS (presentation timestamp) value:

Faster Video Speed

ffmpeg -i input.mp4 -filter:v "setpts=0.5*PTS" output.mp4

Slower Video Speed

ffmpeg -i input.mp4 -filter:v "setpts=2*PTS" output.mp4

The lower the PTS value, the faster the time-lapse video is generated.  If you use a larger value, the video will display in slower motion.

Adjust video and audio

If you care to sync the audio speed with the video speed, things get a bit more complicated:

ffmpeg -i input.mp4 -filter_complex "[0:v]setpts=0.5*PTS[v];[0:a]atempo=2.0[a]" -map "[v]" -map "[a]" output.mp4

More confusing is that the atempo setting seems to require reverse logic to setpts ; i.e. doubling speed seems to require a different multiple.

I recently used this slow-mo technique on a highlights video downloaded from YouTube to draw my own conclusions about a specific play.  The ability to manipulate video so easily, without a UI, also makes for easy automation of media, especially when you combine this technique with clipping video -- you get just the frames you want at the speed you want.  ffmpeg FTW!

Recent Features

  • By
    Vibration API

    Many of the new APIs provided to us by browser vendors are more targeted toward the mobile user than the desktop user.  One of those simple APIs the Vibration API.  The Vibration API allows developers to direct the device, using JavaScript, to vibrate in...

  • By
    9 Mind-Blowing Canvas Demos

    The <canvas> element has been a revelation for the visual experts among our ranks.  Canvas provides the means for incredible and efficient animations with the added bonus of no Flash; these developers can flash their awesome JavaScript skills instead.  Here are nine unbelievable canvas demos that...

Incredible Demos

  • By
    CSS Rounded Corners

    The ability to create rounded corners with CSS opens the possibility of subtle design improvements without the need to include images.  CSS rounded corners thus save us time in creating images and requests to the server.  Today, rounded corners with CSS are supported by all of...

  • By
    Redacted Font

    Back when I created client websites, one of the many things that frustrated me was the initial design handoff.  It would always go like this: Work hard to incorporate client's ideas, dream up awesome design. Create said design, using Lorem Ipsum text Send initial design concept to the client...

Discussion

  1. Rob

    How can I decrease the speed of a video & audio by 7.5%?

  2. Al

    Very helpful, thanks! One tip about the relation between setpts setting and atempo setting: You don’t actually need to calculate the other one, use the following syntax (e.g. to have a 1.3x faster output):

    ffmpeg.exe -i input.mp4 -filter_complex "[0:v]setpts=PTS/1.3[v];[0:a]atempo=1.3[a]" -map "[v]" -map "[a]" output.mp4
  3. Tarun

    @AI thanks for the tip!! it really is amazing!!

  4. Kay

    Just ran ffmpeg basic command (no speed switches), e.g.

    ffmpeg -i  

    and got a video that is smaller than the original but:
    – all the video is fast and at the front of the file then
    – the rest is blank and audio only
    I don’t get it?

    Never had this issue before…

  5. Kay

    ok that should have been:

    ffmpeg -i inputfile outputfile
    

    so the basic command

  6. Viktor

    10x up & 10x down:

    ffmpeg -y -hide_banner -i 01.mp4 -filter_complex [0:v]setpts=PTS/10,setpts=PTS*10;[0:a]atempo=10.0,atempo=1/2,atempo=1/2,atempo=1/2,atempo=1/1.25 005_10x10.mp4
    
  7. Viktor
    10=2*2*2*1.25
    

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