Splitting Large Video Files For Editing with FFmpeg
I’ve been doing more video content than ever as of late, and working with video files is one of my favorite pastimes. Between streaming and editing video for YouTube, being able to manipulate video files – especially large ones – is becoming more and more of a necessity. A few days ago, I had an ambitious project: a 12-hour YouTube and Twitch stream. While I wasn’t able to make it the entire 12 hours, I did manage to get in 10+ solid hours of streaming. You can find the full archive video here:
Pretty intimidating, isn’t it? My computer – either of them, actually, couldn’t handle bringing the large file in for highlighting clips. It was just too big. I needed a way to split out particular chunks of the video in order to make my smaller, more manageable videos.
In case you aren’t familiar with it, FFmpeg is a command line tool used (primarily) to convert video from one filetype / codec to another. BUT, with one command, you can utilize this same tool to extract snippets from your large files for editing/processing later.
Step 0: Install FFmpeg
Mac: Homebrew is the absolute easiest way to install any software like this on a MacBook pro. Use the command
brew install ffmpeg
to allow Homebrew to do its thing and install FFmpeg.
PC: Greg Zaal has written a fantastic tutorial on how to do this. It’s a bit more involved, but it will get the job done.
The FFmpeg Command
$ ffmpeg -i /path/to/input/file.mp4 -ss 00:01:00 -t 00:30:00 -c copy /path/to/output/file.mp4
Let’s unpack this command:
- ffmpeg – the command itself.
- -i /path/to/input/file.mp4: this is our specified input. Point this to the file you wish to extract a clip from
- -ss 00:01:00: the seek argument. This will render all frames before this point as empty frames, and will discard them. This is how we specify FFmpeg to start the clip at a certain point. The example will start us at 1 minute into the clip.
- -t 00:30:00: the duration argument. This is how long you want your clip to be, in hours:minutes:seconds. The example is 30 minutes.
- -c copy: the codec argument. By passing ‘copy’ here, we are able to simply preserve the file in its original format without transcoding it to a different one. IF you are more familiar with FFmpeg, this can be changed to the codec/format of your choosing, but for mp4, I’m simply keeping it the way it is
- /path/to/output/file.mp4: our destination for the output file.
Using this command will stream our input file, start at the specified timestamp, clip a file for the length of the specified duration, and copy that clip to the specified file location.
A Note About Keyframes
As you are clipping, you may notice that sometimes your file will start with a blank, empty few seconds with audio in the background. Eventually, after 2 or 3 seconds, the video will ‘kick in’ and play the video as it should be played. This is because the first frame of your clipped file may not be a keyframe – a video frame that contain meta/transition information regarding the frames. Keyframes occur at regular intervals in a media file, and if you happen to cut between to keyframes, the information needed to display the video correctly will be missing. This is, unfortunately, a known issue and nearly impossible to work around.
A solution: simply ensure that you are cutting a few seconds (3-5 should suffice) around the clip you wish to extract. If you are splitting a large file into several sections, you may want to overlap your clips by several seconds to ensure you won’t lose any keyframes as you re-render or assemble your video.