FFmpeg Cheat Sheet


By Ronnie Royston

Originally Published 3/1/2018
Updated 8/7/2020


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
    1. Create folder C:\Program Files\ffmpeg\
    2. Extract the contents of the FFmpeg zip package to this folder
    3. Right click This PC or Computer then click Properties > Advanced System Settings > Advanced > Environment Variables
    4. Edit the Path variable
    5. Append the location of ffmpeg.exe to the list, C:\Program Files\ffmpeg\bin\
  3. 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.

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. We are using FFmpeg from the command line.

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

Automate a workflow or process today with standards based, library free JavaScript in the browser, in the cloud with NodeJS, or both. High Tekk can do that! See our services page for more details.

Dialog Title

Cell Phone Number

Your order ID is . The grand total is , or bitcoin.

This action cannot be undone. Continue?

Changes will be lost. Continue?

Delete location?

A signin link will be sent to your email address.

You privacy is top priority. Email addresses are never shared with a third party. For more information see our privacy policy.

Find a bug? Experience an error? How can we do better? We appreciate your feedback.

Delete order? This action cannot be undone.

Delete order? This action cannot be undone.

This action cannot be undone. Continue?