FFmpeg Cheat Sheet

Table of Contents

Ronnie Royston
3 min readJan 4, 2023

Introduction
Installing FFmpeg
Compress .mp4
Transcode Multimedia
Transcode .mp4 to .webm
Convert .gif to .mp4
Trimming A Multimedia File
Cut everything after 1 minute
Join Separate Audio and Video Files
Remove Audio from Video
Export First Frame to Image File Format
Add a Watermark to a Video
Add A Watermark
Zoom In Video
Reassembly of Transport Stream Video
Join .ts Files
Transcode .ts to .mp4

Introduction

HTML5 introduced native <audio> and <video> multimedia elements making audio and video enabled websites as easy as copy and paste. However since both the content and the codecs are subject to licensing issues there remains cross platform and cross browser challenges. Therefore it is recommended to serve more than one format of audio or video to reach the largest audience. FFmpeg is the tool for this job — formatting or transcoding a .wmv to .mp4, for example. This article does not focus on audio or video web development but on creating audio and video files using FFmpeg, the leading multimedia framework.

For more information about audio and video web development see MDN’s Video and audio content

The commands below include hand selected arguments / options for quality while minimizing file size. Users are encouraged to explore the full capabilities of the FFmpeg tool.

Installing FFmpeg

Begin by downloading and installing FFmpeg on your system. Below are the installation steps for a Windows system. Apple and linux system users should follow the steps specific to their platform which can be found online.

  1. Download the FFmpeg software from https://www.ffmpeg.org/
  2. Install the software on your system
  3. Create folder C:\Program Files\ffmpeg\
  4. Extract the contents of the FFmpeg zip package to this folder
  5. Right click This PC or Computer then click Properties > Advanced System Settings > Advanced > Environment Variables
  6. Edit the Path variable
  7. Append the location of ffmpeg.exe to the list, C:\Program Files\ffmpeg\bin\
  8. Verify installation by opening a command line and entering ffmpeg -version

Now that you have FFmpeg installed let’s look at how to do some basics — transcoding from one format to another, cutting a multimedia file, and adding a watermark to a video. We are using FFmpeg from the command line.

Compress .mp4

Reduce the filesize of an mp4 while maintaining good quality.

ffmpeg -i input.mp4 -vcodec libx264 -crf 20 output.mp4

Transcode Multimedia

FFmpeg allows us to transcode video as well as convert video to audio only.

Transcode .mp4 to .webm

ffmpeg -i input.mp4 -c:v libvpx -crf 10 -b:v 1M -c:a libvorbis output.webm

Convert video .mp4 to audio .mp3

ffmpeg -i my-video.mp4 my-video.mp3

Convert .gif to .mp4

ffmpeg -i animated.gif -movflags faststart -pix_fmt yuv420p -vf &#34;scale=trunc(iw/2)&#42;2:trunc(ih/2)&#42;2&#34; video.mp4

Trimming A Multimedia File

In case you need to cut portions of a multimedia file FFmpeg has you covered.

Cut everything after 1 minute

ffmpeg -i my-video.mp4 -ss 00:00:00 -t 00:01:00 -async 1 -c copy cut.mp4

Join Separate Audio and Video Files

Join separate audio and video files without re-encoding.

ffmpeg -i video.mp4 -i audio.wav -c copy output.mp4

Remove Audio from Video

ffmpeg -i input_file.mp4 -vcodec copy -an output_file.mp4

Export First Frame to Image File Format

ffmpeg -i my-video.mp4 -vframes 1 -f image2 output-image.jpg

Add a Watermark to a Video

Want to add your logo or watermark to your video? FFmpeg can do that. For best results use a .png image format as it supports transparency. You want this watermark to be small, usually somewhere between 16 and 36px. The overlay parameters specify the x:y offset in pixels.

Add A Watermark

ffmpeg -i my-video.mp4 -i my-watermark.png -filter_complex "overlay=36:36" -codec:a copy final.mp4

Zoom In Video

This is done by scaling then cropping. Example below zooms in x2.

ffmpeg -i input.mp4 -vf "scale=2*iw:-1,crop=iw/2:ih/2" output.mp4

Reassembly of Transport Stream Video

Sometimes you find that page source does not specify a single video file. FFmpeg can be used to join each individual transport stream files. Open browser developer tools Control + Shift + i and click the Network tab. If you see several .ts files search for a m3u8 file. Specifically, you want the index_0 file which is the highest resolution set. This file displays each segments address. It is telling you the web address to get each transport stream segment. Download each .ts file then use FFmpeg to join then all and transcode them to .mp4.

Join .ts Files


ffmpeg copy /b segment1_0_av.ts+segment2_0_av.ts+segment3_0_av.ts full.ts

Transcode .ts to .mp4


ffmpeg -i full.ts -acodec copy -vcodec copy full.mp4

--

--

Ronnie Royston

Delivering refined solutions via vigorous practice. Tulane ('97), Cisco CCIE# 6824, Google Certified Professional Cloud Architect, and USPA Master Skydiver