(FFmpeg) How to Edit the Hue of a Video?

The hue filter allows for modification of the hue and saturation of a video. 

Figure 57.0: applying a hue and saturation

In the example below, the hue is set to 45 degrees with an increased saturation, as seen in figure 57.0:

$ ffmpeg -i input.mp4 -vf "hue=h=45:s=2" output.mp4
Figure 57.1: dynamic hues

The hue filter also has framing and time parameters that allow for great effects like animating through hue colors quickly. Expressions can also be programmed, as seen in figure 57.1:

 $ ffmpeg -i input.mp4 -vf "hue=H=2*PI*t:s=cos(2*PI*t)+10" output.mp4
hue
	Indicates the hue filter name

h
	Indicates the hue angle as a number of degrees (default 0)

s
	Indicates the saturation (10 to -10 with default 1)

H
	Indicates hue angle as a number of radians (default 0)

b
	Indicates the brightness (10 to -10 with default 0)


n
	Indicates frame count of the input frame (starts at 0)

t
	Indicates the timestamp (HH:MM:SS)

Love FFmpeg? Grab a copy of FFmpeg For Beginners on Kindle or Paperback to learn over 120 ways to master FFmpeg!

buy now on amazon

(FFmpeg) How to Add Color Balance to a Video?

The color balance filter allows the RGB shadows, midtones, highlights and preserving lightness can be adjusted. This filter can be used to apply very custom color grading or used to create trippy color changes. 

Figure 56.0: green/blue extreme color balance

In this example, the green shadows are increase and the blue highlights are maxed out, as seen in figure 56.0:

  $ ffmpeg -i input.mp4 -vf "colorbalance=gs=.5:bh=1" -pix_fmt yuv420p output.mp4 
colorbalance
	Indicates the color balance filter name

rs, gs, bs
	Indicates adjustments to red, green and blue shadows or darkest pixels (1.0 to -1.0 with default 0.0)

rm, gm, bm
	Indicates adjustments to red, green and blue midtones or medium pixels (1.0 to -1.0 with default 0.0)

rh, gh, bh
	Indicates adjustments to red, green and blue highlights or brightest pixels (1.0 to -1.0 with default 0.0)


pl
	Indicates preserve lightness when changing color balance (1.0 to -1.0 with default 0.0)

Tip: Negative values shift to complementary colors while positive values shift to primary colors.

Love FFmpeg? Grab a copy of FFmpeg For Beginners on Kindle or Paperback to learn over 120 ways to master FFmpeg!

buy now on amazon

(FFmpeg) How to Use Green Screen to Mask a Video Into Another Video?

The use of a green screen is the most common way to add a subject to a scene in video production. By setting the scene at the purest green (not commonly found in nature) objects can interact on the green screen while computer generation can replace a new image in the background.

With FFmpeg, this can easily be accomplished using the colorkey filter. The colorkey filter takes an input, looks for a specific color depending on similarity, and stores the information in a new stream. 

In this example, the use of greenscreen.mp4 is used as a mask to be applied to input.mp4, as seen in figure 1. 

If you have an image with green screen content like above but want to turn the image into a video try this code out:

$ ffmpeg -loop 1 -i input.png -I -c:v libx264 -pix_fmt yuv420p output.mp4

The result includes the black background but the train video in replace off the green words, as seen in figure 2:

 $ ffmpeg -i input.mp4 -i greenscreen.mp4 -filter_complex '[1:v]colorkey=color=00FF00:similarity=0.85:blend=0.0[ckout];[0:v][ckout]overlay[out]' -map '[out]' output.mp4 

Tip: #00FF00 is the pure green found in green screens. 

Love FFmpeg? Grab a copy of FFmpeg For Beginners on Kindle or Paperback to learn over 120 ways to master FFmpeg!

buy now on amazon

“FFmpeg For Beginners” is now on Amazon! (Paperback & Ebook)

Today I am extremely excited to announce that “FFmpeg For Beginners” is now on Amazon. Cover over 112 different concepts, this is the ultimate FFmpeg programmers guide for users at any level. Readers at the novice level can gracefully learn FFmpeg and become upper-intermediate in no time. Soon you’ll be able to edit audio and video like a pro! Over 112 Questions fully answered with examples, scripts, and images!

It’s available on Kindle and Paperback (English) in various countries:

United States

United Kingdom

Canada

Germany

France

Spain

Italy

Japan

Netherlands

Brazil

India

Australia

Mexico

I look forward to your feedback and as always, all of these scripts will be available free of charge on this website. Thanks for all the support helping me refine this book and making it perfect for those interesting in FFmpeg!

Full list of topics

Installation

  • 1How to check the installed FFMPEG version?
  • How to install extra libraries to FFMPEG
  • How to use FFMPEG in x Language?
  • How to install FFMPEG for PHP?
  • How to install FFMPEG on Windows?
  • How to install FFMPEG on Linux?
  • How to install FFMPEG on MacOS?

Basic FFMPEG knowledge

  • How to convert an entire directory?
  • How to use filter_complex without losing video quality?
  • How to check audio / video file information
  • How to chain multiple filters?
  • What is the difference between -vf vs -filter_complex?
  • How to copy codec from one video type to another?
  • What are all the formats FFMPEG supports?
  • What are all the FFMPEG supported codecs?
  • What is a ‘codec’? explained
  • Explaining -map
  • What is a pxl_fmt / yuv420p

— Audio

Basic audio conversions

  • How to convert wav to mp3?
  • How to convert flac to mp3?
  • How to convert ogg to mp3?
  • How to convert Video to Audio?

Audio editing

  • How to trim x seconds from start & end of an audio track?
  • How to trim x seconds from the end of an audio track?
  • How to trim x seconds from the start of an audio track?
  • How to merge multiple MP3s into one track?

Audio Filters

  • Add a high pass filter – audio
  • Add a low-pass filter – audio
  • How to replace audio on a video?
  • How to mix audio on a video?
  • How to remove audio on a MP4?
  • How to generate text to speech audio (flite)
  • How to generate an audio tone (sine)?
  • How to change pitch of audio (asetrate)?
  • How to change tempo of audio (atempo)?
  • How to add echo to MP3
  • How to normalize audio?
  • How to cross fade to two mp3s?
  • How to adjust volume of MP3?

Basic video conversions

  • How to compress mp4 & reduce file size?
  • How to convert a MP4 to GIF?
  • How to convert webm to mp4?
  • How to convert FLV to MP4?
  • How to convert AVI to MP4?
  • How to convert mkv to mp4?
  • How to convert mov to mp4?

Video timeline editing

  • How to concatenate multiple MP4 files
  • How to loop a section of video X times?
  • How to stitch spliced videos into segments?
  • How to splice up video into segments?
  • How to trim X seconds from the start & end of video?
  • How to trim X seconds from the end of video?
  • How to trim X seconds from the start of video?

Video filters

  • How to apply a color palette to a video (paletteuse)?
  • How to generate a color palette from video (palettegen)?
  • How to remove all colors except one from video (colorhold)? NEEDS correction
  • How to convert video to black and white (hue)?
  • How to apply a vignette to a video (vignette)?
  • How to RGB shift a video (rgbashift)?
  • How to invert video (negate)?
  • How to edit saturation of a video (vibrance)?
  • How to edit the hue/saturation of a video (hue)?
  • How to color balance a video?
  • How to use visual normalization on a video?
  • How to use blend on two MP4s?
  • How to adjust volume of MP4?

Sharpening / Blur filters

  • How to add a pixelate effect to a video?
  • How to apply a box blur a video (boxblur)?
  • How to apply Gaussian blur to video (gblur)?
  • How to apply a smartblur to a video (smartblur)?
  • How to apply a blur to a video (unsharp)?
  • How to sharpen a video (unsharp)?

Video playback filters

  • How to remove duplicate frames?
  • How to change video quantization (qp)?
  • How to change video resolution?
  • How to change video frame rate?
  • How to crop a video (crop)?
  • How to reverse a video (reverse)?
  • How to speed up a video?
  • How to slow down a video?

Transform filters

  • How to rotate a video?
  • How to horizontal flip a video (hflip)?
  • How to vertically flip a video (vflip)?
  • How to stack multiple videos vertically (vstack)?
  • How to stack multiple videos horizontally (hstack)?

Text editing

  • How to overlay custom text to video (drawtext)?
  • How to burn subtitles into a video?
  • How to add subtitles to video?
  • How to extract subtitles from video?

Image Video Filters

  • How to generate a solid color video?
  • How to generate a picture waveform from video?
  • How to convert video into a tile image (tile)?
  • How to make a video from a single image & mp3?
  • How to create a video from images?
  • How to extract all frames (jpg) from a video?
  • How to extract an image (jpeg) frame from video at x seconds?
  • How to create a video slideshow from images (slideshow)?
  • How to add a transparent watermark to a video?

Advance video filters

  • How to use frei0r filters?
  • How to use green screen to mask a video into another video?
  • How to randomize frames in video (random)?
  • How to apply static to a video (get)?
  • How to add noise to video (noise)?
  • How to add various fades to a video (xfade)?
  • How to datamosh glitch a video?

Steaming

  • How to apply filters to a steam
  • How to connect to YouTube

Common Errors

* Previously published as The FFmpeg Bible

(FFmpeg) How to Add Color Normalization to a Video?

Normalization is a color correction filter that standardizes the RGB values in each frame of a video. With the use of histogram and contrast stretching, FFmpeg can quickly fix issues like rapid brightness changes and flickering. Below is an example of normalization at the highest strength:

  $ ffmpeg -i input.mp4 -vf "normalize=strength=1" output.mp4   

Tip: If this filter is applied to an MP4, you may need to add a pixel format, -pix_fmt yuv420p, for the previewer to work on macOS.

normalize
	Indicates the normalization filter name

strength
	Indicates the strength of the normalization, large the value higher the strength (1.0 to 0.0 with default 1.0)

smoothing
	Indicates setting a temporal smoothing based on the previous frame (1 or 0 with a default 0 - no smoothing)

Love FFmpeg? Grab a copy of FFmpeg For Beginners on Kindle or Paperback to learn over 120 ways to master FFmpeg!

buy now on amazon

(FFmpeg) How to Blend Two Videos Together?

Blending has an interesting effect when two inputs are used. The blend filter has the potential to create outstanding artistic results, so let’s try it out. 

Tip: In order to blend two files, they must contain the same resolution.


blending two videos

In this example, using the difference filter applied to both video inputs gives an interesting inverted look as seen above: 

$ ffmpeg -i input1.mp4 -i input2.mp4 -filter_complex "blend=difference" output.mp4 

The available blending modes are listed below:

addition
grainmerge
and
average
burn
darken
difference
grainextract
divide
dodge
freeze
exclusion
extremity
glow
hardlight
hardmix
heat
lighten
linearlight
multiply
multiply128
negation
normal
or
overlay
phoenix
pinlight
reflect
screen
softlight
subtract
vividlight
xor


blending with a color

A lot of these filters work better when color is applied instead of another video. For example, blending a solid color video (red.mp4) with a lighten blending, as seen above:

  $ ffmpeg -i input1.mp4 -i red.mp4 -filter_complex "blend=lighten" output.mp4  

Using colors or gradients and multiple blends, custom Instagram-like filters can easily be created. 

Love FFmpeg? Grab a copy of FFmpeg For Beginners on Kindle or Paperback to learn over 120 ways to master FFmpeg!

buy now on amazon

(FFmpeg) How to Concatenate Multiple Videos?

Using concat with a txt file has one catch, all the MP4s must have the exact codec, framerate, resolution, etc. An example text file is shown below (file.txt): 

file 'input0.mp4'
file 'input1.mp4'
file 'input2.mp4'

Then run the following command to load the txt file to create a single file from the list:

  $ ffmpeg -f concat -i file.txt -c copy -fflags +genpts full.mp4   

But how can you join multiple MP4 files that are not exactly the same resolution, codec or framerates? A different method of concat is required, as seen in the example below:

 $ ffmpeg -i input0.mp4 -i input1.mp4 -i input2.mp4 -filter_complex "[0:v] [0:a] [1:v] [1:a] [2:v] [2:a] concat=n=3:v=1:a=1 [v] [a]" -map "[v]" -map "[a]" output.mp4
concat
	Indicates the concat filter name

n
	Indicates the number of segments or input files

v
	Indicates the number of output video streams

a
	Indicates the number of output audio streams

(FFmpeg) How to Loop a Section of Video Multiple Times?

Looping a section of a video can be useful for presentations or advanced video editing. In this example the input.mp4 is a 5 minute long video with the requirement that after the 1 minute mark, the video will loop 10 times.

  $ ffmpeg -i input.mp4 -filter_complex "loop=loop=10:size=1500:start=1500" -pix_fmt yuv420p output.mp4  

In the requirements for this example, it was stated after the 1 minute mark the video will loop yet the start number is 1500. This is because start requires a frame value not time.  To calculate the frame value one must know the framerate of the input. In this example, input.mp4 has a 30 frames per second framerate (FPS). 30 FPS * 1 minute (60 seconds) = 1500 frames.

In addition to the start parameter, a size parameter must be set as well. size is the number of frames that are repeated. So from frames 0 to 1500 the loop will play 10 times after the 1500th frame has been played.

loop 
	Indicates the name of the loop filter

loop
	Indicates the number of loops (default is 0 with -1 being infinite)

size
	Indicates the number of frames (from 0 to value) that are repeated

start
	Indicates first or starting frame of the loop (default is 0)

The loop filter works great with video but does not apply to audio output. Thus another method is required. Using concat multiple times on the same file could be used instead:

 $ ffmpeg -i input.mp4 -i input.mp4 -i input.mp4 -filter_complex "concat=n=3:v=1:a=1 [v] [a]" -map "[v]" -map "[a]" output.mp4

Then with the use of seeking, -ss, the section to be repeated can be specified. 

Challenge: Try looping a specific section of a video using concat.

Below is the command to identify the frame rate of an input file, this one has a frame rate of 29.97:

$ ffmpeg -i input.mp4 
ffmpeg version 4.2.2 Copyright (c) 2000-2019 the FFmpeg developers
  built with Apple clang version 11.0.0 (clang-1100.0.33.17)
  Configuration: <removed for clarity>
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'input.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 512
    compatible_brands: isomiso2avc1mp41
    encoder         : Lavf58.29.100
  Duration: 00:00:20.05, start: 0.000000, bitrate: 1802 kb/s
    Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 720x480 [SAR 40:33 DAR 20:11], 1667 kb/s, 29.97 fps, 29.97 tbr, 30k tbn, 59.94 tbc (default)
    Metadata:
      handler_name    : Core Media Video
      timecode        : 01:00:00:00
    Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 128 kb/s (default)
    Metadata:
      handler_name    : Core Media Audio
    Stream #0:2(eng): Data: none (tmcd / 0x64636D74), 0 kb/s
    Metadata:
      handler_name    : Core Media Video
      timecode        : 01:00:00:00

(FFmpeg) How to add a motion blur to a video?

I saw this question on Reddit and started messing with the effects recommended and totally liked the effect seen in the video below:

Here’s the full script using the tmix filter:

$ ffmpeg -i input.mp4 -vf tmix=frames=8:weights="1 1 1 1 1 1 1 1" output.mp4

What you’ll notice here is the more frames you set the more motion effect is applied. The weights must equal the same number of frames. there are eight “ones” for 8 frames. “of weights is smaller than [the] number of frames last specified weight will be used for all remaining unset weights.”

Mix successive video frames.

A description of the accepted options follows.

frames
The number of successive frames to mix. If unspecified, it defaults to 3.

weights
Specify weight of each input video frame. Each weight is separated by space. If number of weights is smaller than number of frames last specified weight will be used for all remaining unset weights.

scale
Specify scale, if it is set it will be multiplied with sum of each weight multiplied with pixel values to give final destination pixel value. By default scale is auto scaled to sum of weights.

Love FFmpeg? Grab a copy of FFmpeg For Beginners on Kindle or Paperback to learn over 120 ways to master FFmpeg!

buy now on amazon

(FFmpeg) How to Stitch Segments to One Video?

Stitching videos generated by the segment filter is accomplished using the concat filter. See Question, “How to Splice a Video into Segments?”, for more details on segmenting video output.

Imagine you’ve generated a bunch of segments for a stream and now you need to combine them back together as the packages arrive at the client. First a list of videos to stitch together must be created. The file (file.txt) might look like the following:

file 'output_00.mp4' 
file 'output_01.mp4' 
file 'output_02.mp4' 
file 'output_03.mp4' 
file 'output_04.mp4'

Then run the following command to create a single file again:

  $ ffmpeg -f concat -i file.txt -c copy -fflags +genpts output.mp4   

Streaming with concat is not covered in this book but more information can be found by visiting: https://ffmpeg.org/ffmpeg-formats.html#concat-1

fflags
	Indicates the specific flag that is used

+genpts
	Indicates the need to generate missing presentation timestamps (PTS) if needed. A common requirement when a video is segmented by duration instead of timestamp

Challenge: How would you do this with an audio file instead?