Learn Docker With My Newest Course

Dive into Docker takes you from "What is Docker?" to confidently applying Docker to your own projects. It's packed with best practices and examples. Start Learning Docker →

Create Video Clips with FFmpeg in Seconds

blog/cards/create-video-clips-with-ffmpeg-in-seconds.jpg

Provide a file along with a start / end time and ffmpeg will do the rest. We'll also make a tiny wrapper script to make it easier to use.

Quick Jump: The FFmpeg Command | The Shortcut Script | Demo Video

Prefer video? There’s a YouTube video of this blog post available that goes into a bit more detail about certain topics listed below.

FFmpeg can do a lot of things. It’s very handy if you have a large video and you want to create 1 or more smaller videos out of it. Perhaps it’s a live stream or YouTube video, podcast or anything else. It works with audio files too such as mp3s.

There’s something magical about turning a 2 hour video that’s 4 GB into (3) smaller videos that are 25-50 MB each in a couple of seconds. No video editing tools required, just FFmpeg which is a command line tool.

You can install FFmpeg with most package managers, such as sudo apt-get install ffmpeg or brew install ffmpeg. There’s also an official download page.

The FFmpeg Command

Let’s say you want to do the following:

  • You have an example.mp4 file
  • You want to create a new mp4 of it in between 1h 15m 42s and 1h 16m
  • You want the new file to be named example-1.mp4

Here’s the command to do it:

ffmpeg -ss 01:15:42 -to 01:16:00 -i example.mp4 -c copy example-1.mp4
  • -ss is the start time
  • -to is the end time
  • -i is the input file
  • -c copy is the output file (this copies the stream and skips decoding / encoding)

This boils down to being able to quickly create new videos without waiting a long time. You can extract out a ~1 minute clip from a massive video and it only takes a few seconds.

It’s worth pointing out if you have a 5 minute video and you want to clip in between 10 seconds and 20 seconds you can simply use 0:10 0:20 as the start and end times. You don’t need to always use HH:MM:SS.

Also you’re not limited to mp3 or mp4 files. The above command will take the source input and produce the correct output for a number of other compressed file formats such as mkv and others.

If you have an uncompressed file such as a wav (audio) file you can run the same command except you’ll need to remove the -c copy part of the command but still include the output file at the end, such as ... -i example.mp4 example-1.mp4.

The Shortcut Script

FFmpeg commands can be a little hard to remember and if you want your output file to be the same name as your input file except with a number or some type of unique identifier at the end you need to duplicate the file name.

We can make this a little bit easier to use with a custom script. I’ve named mine mkclip and here’s the contents of the script.

#!/usr/bin/env bash

set -o errexit
set -o pipefail
set -o nounset

input="${1}"
part="${2}"
start="${3}"
end="${4}"

IFS="." read -r -a input_split <<< "${input}"

filename="${input_split[0]}"
extension="${input_split[1]}"

ffmpeg -ss "${start}" -to "${end}" -i "${input}" -c copy "${filename}${part}.${extension}"

The script expects you’ll be clipping compressed files such as mp4 or mp3 files. You can make it smarter by detecting the file extension and doing different things but I didn’t go that deep into it.

Don’t forget to chmod +x mkclip before trying to run it.

Now you can run: ./mkclip example.mp4 -1 01:15:42 01:16:00 and it’ll create the same 18s example-1.mp4 file for you as the original command. If you prefer using an underscore for the separator you can use _1 instead of -1, you can use anything you want as long as it’s not empty. If it were empty then the input and output would have the same name!

  • example.mp4 is the source file path, it can be an absolute path such as /tmp/myfile.mp4
  • -1 will be used as the output file separator in between the file name and extension
  • 01:15:42 is the start time
  • 01:16:00 is the end time

That covers about 1% of what FFmpeg can do, the video below demonstrates using it to clip a video and it also covers how the custom script works in more detail. You’ll also learn how to make peanut butter and jelly since that’s the video we’ll be clipping in the demo.

Demo Video

Timestamps

  • 0:31 – Installing FFmpeg
  • 1:06 – Learning how to make peanut butter and jelly
  • 1:26 – Cutting out the fluff
  • 1:40 – Building up the FFmpeg command
  • 3:10 – Viewing the clipped video
  • 3:23 – The command is a bit hard to remember
  • 4:04 – Using the wrapper script
  • 5:20 – Seeing the output of the script
  • 6:01 – Splitting the filename from the extension
  • 6:44 – The script works with absolute paths too

References

What are some of your favorite FFmpeg commands? Let me know below.

Never Miss a Tip, Trick or Tutorial

Like you, I'm super protective of my inbox, so don't worry about getting spammed. You can expect a few emails per month (at most), and you can 1-click unsubscribe at any time. See what else you'll get too.



Comments