By Benji Asperheim | 2024-08-29Blog Thumbnail

FFmpeg Examples Showing How to Convert Videos

In this article we will discuss the basics of FFmpeg, how to install it, and how to convert videos.

DISCLAIMER: The conclusion was written by ChatGPT.

Installing FFmpeg

Verify that the ffmpeg and ffprobe commands are working:

ffmpeg -version
# Should return something like: ffmpeg version 7.0.2 Copyright (c) 2000-2024 the FFmpeg developers

ffprobe -version
# Should return something like: ffprobe version 7.0.2 Copyright (c) 2007-2024 the FFmpeg developers

If you don't already have FFmpeg installed, you can use one of the static builds, or install it manually. If you decide to use a pre-compiled binary, on UNIX-like systems, you can just move the ffmpeg and ffprobe binaries to the /usr/local/bin directory.

Here's how you can download a static build and extract the binaries (this command assumes you are using an x86 (64-bit) Linux machine. Use the arch command if you're not sure):

curl -XGET "https://johnvansickle.com/ffmpeg/releases/ffmpeg-release-amd64-static.tar.xz" -o ffmpeg-release-amd64-static.tar.xz
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 39.9M  100 39.9M    0     0  6396k      0  0:00:06  0:00:06 --:--:-- 9412k

NOTE: As of Aug 2024, these examples use the latest v7.0.2 of FFmpeg. The latest (supported) version of FFmpeg may differ from what is explicitely stated in this article.

After downloading the tar.xz archive you can just use the tar command to extract the files:

tar -xvf ffmpeg-release-amd64-static.tar.xz

You should see it extract the files like so:

x ffmpeg-7.0.1-amd64-static/
x ffmpeg-7.0.1-amd64-static/GPLv3.txt
x ffmpeg-7.0.1-amd64-static/manpages/
x ffmpeg-7.0.1-amd64-static/manpages/ffmpeg-all.txt
x ffmpeg-7.0.1-amd64-static/manpages/ffmpeg-scaler.txt
...
x ffmpeg-7.0.1-amd64-static/ffprobe
...
x ffmpeg-7.0.1-amd64-static/readme.txt
x ffmpeg-7.0.1-amd64-static/ffmpeg

Then you can just use the chmod +x command to make the binaries executable before moving them:

chmod +x ffmpeg-7.0.1-amd64-static/ffmpeg
chmod +x ffmpeg-7.0.1-amd64-static/ffprobe

Finally, move them to your /usr/local/bin directory:

sudo mv ffmpeg-7.0.1-amd64-static/ffmpeg /usr/local/bin/ffmpeg
sudo mv ffmpeg-7.0.1-amd64-static/ffprobe /usr/local/bin/ffprobe

Installing FFmpeg on Ubuntu

On an Ubuntu machine you can use the APT repository to install FFmpeg as well:

sudo apt update
sudo apt install ffmpeg

Installing FFmpeg on macOS with Homebrew

1. Reinstall FFmpeg with Proper Dependencies:

  • If you used a package manager like Homebrew, you can reinstall FFmpeg, which should also handle missing dependencies:
brew reinstall ffmpeg

2. Build FFmpeg from Source:

  • If the above steps do not resolve the issue, consider building FFmpeg from source with the required libraries:
git clone https://github.com/FFmpeg/FFmpeg.git
cd FFmpeg
./configure
make
sudo make install

This approach ensures that FFmpeg and its dependencies are properly configured for your system.

Install the libass Library

As stated in their github repo: "libass is a portable subtitle renderer for the ASS/SSA (Advanced Substation Alpha/Substation Alpha) subtitle format. It is mostly compatible with VSFilter".

FFmpeg uses libass to render advanced subtitle formats that include rich text styling and effects. This is important for maintaining the visual quality and intended appearance of subtitles when processing video files.

You can try installing it via Homebrew on macOS:

brew install libass
Install libass on Debian-based Distros

You can use apt in Debian-based distros to install the runtime library:

sudo apt install libass9

For development files (headers and static libraries), which are useful if you're building software from source:

sudo apt install libass-dev

Now you can check that the library path is correctly set in the system's environment, and installed, by locating it:

find /usr/local/ -name 'libass*'

If the library is found, ensure that FFmpeg can locate it by updating or exporting your DYLD_LIBRARY_PATH path (in your ~/.bashrc, ~/.zshrc, or ~/.profile file), or create some symbolic links (if necessary).

Convert an Apple QuickTime (MOV) Video

To convert an Apple .MOV file to an MP4 file (with the H.264 codec) using FFmpeg while maintaining quality but reducing file size, you can use the following command:

ffmpeg -i input.mov -c:v libx264 -crf 23 -preset medium -c:a aac -b:a 192k output.mp4

Here's a breakdown of the options used:

  • -i input.mov: Specifies the input file.
  • -c:v libx264: Sets the video codec to H.264.
  • -crf 23: Constant Rate Factor; lower values mean better quality (and larger file size). A value between 18-28 is usually a good range.
  • -preset medium: Balances encoding speed and compression. You can use slow or veryslow for better compression at the cost of slower, but more efficient, encoding.
  • -c:a aac: Sets the audio codec to AAC.
  • -b:a 192k: Sets the audio bitrate to 192 kbps.

Reduce the File Size of an MP4 (Using the h.264 Codec)

To reduce the file size of your MP4 video, while keeping the quality as high as possible, you can use the -crf (Constant Rate Factor) option in FFmpeg. Since the video is already encoded in H.264, it makes it super easy to use this option in order to re-encode it with a better balance of quality and file size.

Here's an example ffmpeg command of how this could be done:

ffmpeg -i input.mp4 -c:v libx264 -crf 23 -preset slow -c:a copy output.mp4

Explanation

  • -i input.mp4: MP4 input path and file.
  • -c:v libx264: Specifies H.264 video codec.
  • -crf 23: This sets the "Constant Rate Factor": The lower the value, the better the quality (i.e. The higher the CRF number, the lower the video quality). A value between 18 and 28 is typical, and 23 is the default.
  • -preset slow: Sets the encoding speed vs. compression tradeoff. Options range from ultrafast to veryslow. slow is a good balance for quality and compression.
  • -c:a copy: Copies the audio stream without re-encoding it, which saves processing time and avoids quality loss in the audio.

Additional Tips:

  • Adjusting CRF: If you find that -crf 23 results in a file that is still too large or too small, you can adjust the value. For higher quality (and larger file size), use a lower CRF value (e.g., 18). For more compression (and lower quality), use a higher CRF value (e.g., 28).
  • Two-Pass Encoding: For even better quality and file size management, you can use two-pass encoding, though it will take more time:
ffmpeg -i input.mp4 -c:v libx264 -b:v 1500k -preset slow -pass 1 -an -f mp4 /dev/null
ffmpeg -i input.mp4 -c:v libx264 -b:v 1500k -preset slow -pass 2 -c:a copy output.mp4
  • -b:v 1500k: Sets the video bitrate. Adjust this based on your desired output file size.

If you're not getting the results you want, you may need to tweak these settings based on your specific requirements for quality and file size. Keep in mind that larger video files may take quite awhile to convert (depending on your hardware).

Conclusion

FFmpeg is a powerful CLI-based tool for video conversion, capable of handling a wide range of formats and encoding options. With a few simple commands, you can convert videos, reduce file sizes, and ensure compatibility with different devices and platforms. Whether you're converting Apple QuickTime files to MP4 or adjusting video quality with the H.264 codec, FFmpeg provides the flexibility and control needed for high-quality video processing.

Remember to choose the appropriate settings for your specific needs, and don't hesitate to experiment with different options like CRF values and presets to achieve the perfect balance between quality and file size. Additionally, make sure all necessary libraries, such as libass for subtitle rendering, are installed and properly configured on your system.

By following the guidelines and examples provided in this article, you'll be well-equipped to handle most video conversion tasks using FFmpeg, whether on macOS, Linux, or other platforms.

Discover expert insights and tutorials on adaptive software development, Python, DevOps, creating website builders, and more at Learn Programming. Elevate your coding skills today!