DEV Community

Youngho Andrew Chaa
Youngho Andrew Chaa

Posted on

Know your videos: Inspecting & Transcoding with FFmpeg and Homebrew

Have you ever wondered what's inside a video file? Or maybe you need to convert a video to a different format for better compatibility or smaller file size. The command line tools FFmpeg and ffprobe are your secret weapons for these tasks. We'll walk you through how to install them and then use them to inspect and transcode a video.

Installing FFmpeg with Homebrew

If you're on a macOS, the easiest way to install FFmpeg is with Homebrew, the "missing package manager for macOS."

If you don't have Homebrew installed, you'll need to do that first by following the instructions on their official website. Once Homebrew is ready, installing FFmpeg is a breeze. Open your Terminal and type this command:

brew install ffmpeg
Enter fullscreen mode Exit fullscreen mode

After the installation is complete, it's a good idea to confirm that everything is working. You can do this by checking the version of FFmpeg that was installed:

ffmpeg -version
Enter fullscreen mode Exit fullscreen mode

If you see the version information displayed, you're all set!


Checking a Video's Codec with ffprobe

Before you change a video, it's smart to know what you're starting with. That's where ffprobe comes in. It's a powerful command-line tool that's part of the FFmpeg suite. It can gather a ton of information about your multimedia files, including the video and audio codecs.

To find out the codec of a video file named input.mp4, simply run this command in your Terminal:

ffprobe ./input.mp4
Enter fullscreen mode Exit fullscreen mode

This will print out a detailed report about the video, including its codec, resolution, framerate, and more. Look for the "Stream #0:0(eng): Video:" section to find the codec information.


Transcoding to H.264 with ffmpeg

Now for the fun part: changing the video! Transcoding is the process of converting a video file from one codec or format to another. The H.264 codec is a widely used and highly compatible standard, making it a great choice for web videos, mobile devices, and more.

The ffmpeg command has many options, and it can seem a bit intimidating at first. Let's break down a common command used to transcode a video to H.264:

ffmpeg -i input.mp4 -c:v libx264 -crf 23 -preset medium -c:a aac -b:a 128k output.mp4
Enter fullscreen mode Exit fullscreen mode

Let's dissect this command:

  • -i input.mp4: This is the input file. It tells ffmpeg which video to process.
  • -c:v libx264: This is the video codec. We're telling ffmpeg to use the libx264 encoder, which is the H.264 implementation.
  • -crf 23: This stands for Constant Rate Factor. It's a quality setting for the video. A lower number means higher quality and a larger file size, while a higher number means lower quality and a smaller file size. A value of 23 is a good starting point for a balanced output.
  • -preset medium: This option controls the encoding speed. Faster presets (like ultrafast) result in lower quality and larger file sizes, while slower presets (like veryslow) result in higher quality and smaller file sizes. medium is a great balance.
  • -c:a aac: This is the audio codec. We're specifying the AAC (Advanced Audio Coding) codec, which is a common and efficient choice.
  • -b:a 128k: This sets the audio bitrate to 128k, which is a good quality for most purposes.
  • output.mp4: This is the output file name. ffmpeg will save the transcoded video to this file.

After running this command, you'll have a new video file named output.mp4 that's ready for its new purpose!

Top comments (0)