How To Edit Videos Using FFmpeg

mainEditing videos can be a huge pain if you don’t have the right tools. We know that there are a lot of tools available for image editing. You can quickly crop, resize, or manipulate an image without skipping a beat, but you can’t do the same for videos. Let’s say you want to resize a video, extract a 10 second segment from it, and then convert it to a different format. Can you do it quickly? Well, not unless you know how to use some bloated software whose GUI usually has a steep learning curve! A lot of people tend to get stuck when they want to play around with videos. The good news is that you don’t need some fancy commercial software to edit videos. FFmpeg is here for you and you can do a lot of things with it. Let’s go ahead and see how to manipulate a video using this amazing tool, shall we?  

Installing FFmpeg

FFmpeg is a command line tool that’s really powerful and very easy to use. Let’s go ahead and install it using Homebrew. If you have Homebrew already installed, you can skip to the next section. To install Homebrew, you need Xcode command line tools. This is, of course, assuming that you have Xcode installed. If not, install Xcode from the app store (it’s free). Now install the command line tools using the following command:

$ xcode-select --install

We are now ready to install Homebrew. Run the following command from your terminal:

$ ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Verify the installation using the following command:

$ brew doctor

This command will check whether or not the installation was successful, along with checking if you can install formulas (Homebrew Packages).

We are now ready to install FFmpeg. Install it using the following command:

$ brew install ffmpeg --with-fdk-aac --with-ffplay --with-freetype --with-frei0r --with-libass --with-libvo-aacenc --with-libvorbis --with-libvpx --with-opencore-amr --with-openjpeg --with-opus --with-rtmpdump --with-schroedinger --with-speex --with-theora --with-tools

What can we do with FFmpeg?

Now that we have installed FFmpeg, it’s time to play around with it. You can do pretty much everything with videos you can possibly think of! Let’s explore it on a case by case basis.

1. Extract a part of the input video

Let’s say you want to extract a 23 second video starting at timestamp 1 min 45 seconds:

$ ffmpeg -i inputVideo.mp4 -ss 00:01:45.0 -codec copy -t 23 outputVideo.mp4

Here, “-ss” is the time offset parameter to specify the starting timestamp in the format “HH:MM:SS.ms”. The “-t” parameter specifies the duration of the video clip in seconds.

You can split the video into multiple parts as well:

$ ffmpeg -i inputVideo.mp4 -t 00:00:35 -c copy subVideo1.mp4 -ss 00:00:35 -codec copy subVideo2.mp4

The above command will create two output videos where the first one will be from start to 35 seconds and the second one will be from 35 seconds till the end.

2. Converting video formats

You can easily convert one video format into another:

$ ffmpeg -i inputVideo.h264 -c:v libx264 outputVideo.mp4

3. Merging multiple video files

If you have a bunch of videos and you want want merge all of them to create one big video, you can easily do it here. We just need to make sure that all those videos are in the same video codec format. Once we do that, we need to create a text file with a list of all videos we want to merge:

$ ffmpeg -f concat -i videoFileList.txt -c copy mergedVideo.mp4

4. Extracting/muting audio

To extract the audio track as an mp3 file:

$ ffmpeg -i inputVideo.mp4 -vn -ab 256 outputAudio.mp3

If you want to mute the audio:

$ ffmpeg -i inputVideo.mp4 -an mutedVideo.mp4

5. Creating animated gif files

This is actually fun! You can create animated gif files based on different parts of a video:

$ ffmpeg -i inputVideo.mp4 -vf scale=250:-1 -t 25 -r 15 outputImage.gif

The “scale” filter specifies the width of the GIF, “-t” specifies the duration in seconds, and “-r” specifies the frame rate.

6. Extracting image frames from video

To extract a particular image frame:

$ ffmpeg -ss 00:00:23 -i inputVideo.mp4 -vf scale=600:-1 -vframes 1 image.jpg

This will extract the image frame at the 23-second mark in the input video file. If you want to extract all the image frames:

$ ffmpeg -i inputVideo.mp4 -r 5 image_%05d.png

Here, ‘-r’ specifies the frame rate. The above command saves 5 frames every second.

7. Merge video and audio files

You can merge different video and audio tracks. You just need to make sure they are aligned. Pretty useful to create dubsmash videos!

$ ffmpeg -i inputVideo.mp4 -i inputAudio.mp3 -c:v copy -c:a aac -strict experimental outputVideo.mp4

8. Resizing a video

You can resize a video to create bigger or smaller video files:

$ ffmpeg -i inputVideo.mp4 -s 640x480 -c:a copy outputVideo.mp4

There are many more things you can do with FFmpeg. You can check out their documentation to fully experience its awesomeness!

———————————————————————————————————

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s