FFmpeg Cheat Sheet
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
.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.
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.
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.
- Download the FFmpeg software from https://www.ffmpeg.org/
Install the software on your system
- Create folder C:\Program Files\ffmpeg\
- Extract the contents of the FFmpeg zip package to this folder
- Right click This PC or Computer then click Properties > Advanced System Settings > Advanced > Environment Variables
- Edit the Path variable
- Append the location of ffmpeg.exe to the list, C:\Program Files\ffmpeg\bin\
- 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.
Reduce the filesize of an mp4 while maintaining good quality.
ffmpeg -i input.mp4 -vcodec libx264 -crf 20 output.mp4
FFmpeg allows us to transcode video as well as convert video to audio only. We are using FFmpeg from the command line.
ffmpeg -i input.mp4 -c:v libvpx -crf 10 -b:v 1M -c:a libvorbis output.webm
.mp4 to audio
ffmpeg -i my-video.mp4 my-video.mp3
ffmpeg -i animated.gif -movflags faststart -pix_fmt yuv420p -vf "scale=trunc(iw/2)*2:trunc(ih/2)*2" 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.
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
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
If you see several
.ts files search for a
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.
.ts file then use FFmpeg to join then all and transcode them to
ffmpeg copy /b segment1_0_av.ts+segment2_0_av.ts+segment3_0_av.ts full.ts
ffmpeg -i full.ts -acodec copy -vcodec copy full.mp4