Skip to content

Instantly share code, notes, and snippets.

@erikson1970
Last active April 25, 2021 19:37
Show Gist options
  • Select an option

  • Save erikson1970/4360db6cbfec94c820332fb9e6d3e4ea to your computer and use it in GitHub Desktop.

Select an option

Save erikson1970/4360db6cbfec94c820332fb9e6d3e4ea to your computer and use it in GitHub Desktop.
Modify Video Speed with ffmpeg

Modify Video Speed with ffmpeg

David Walsh

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment