Is there a way to add a "fade to black" effect to a video, from the command line?

9,841

Solution 1

The only thing I could find – based on the command line – is mmsuper8, a Linux tool.

Specifically, it features the mmsuper8fade tool, which seems like it could be useful for you.

Best would be if you edit this question and supply an example that worked for you!

Solution 2

A recent ffmpeg with the 'fade' video filter can do this. Unfortunately, the ffmpeg in the Ubuntu repos doesn't have filters enabled for some inane reason, so you'll have to get it from somewhere else (see here).

To fade in from black, starting at frame 0, over 50 frames (2 seconds @ 25fps):

ffmpeg -i input.mp4 -filter:v 'fade=in:0:50' \
-c:v libx264 -crf 22 -preset veryfast -c:a copy output.mp4

To fade out starting at frame 21000 (14 minutes @ 25fps), over 50 frames:

ffmpeg -i input.mp4 -filter:v 'fade=out:21000:50' \
-c:v libx264 -crf 22 -preset veryfast -c:a copy output.mp4

You can combine the two into a filterchain:

ffmpeg -i input.mp4 -filter:v 'fade=in:0:50,fade=out:21000:50' \
-c:v libx264 -crf 22 -preset veryfast -c:a copy output.mp4

As you can see from these examples, you have to transcode video when using a video filter. Since ffmpeg and mencoder share many libraries, it should be fairly easy to re-write your scripts to use ffmpeg instead of mencoder & avoid an unnecessary extra encode. I'm pretty sure that you can achieve the same thing in mencoder, but I don't know mencoder syntax.

Solution 3

I have this bash script to fade out the end of movies (audio + video) automatically :

#!/bin/bash

# Audio + vidéo fade out at the end of mp4 files

# 2015-09-09 19:07:17.0 +0200 / Gilles Quenot

# length of the fade out
fade_duration=2 # seconds

if [[ ! $2 ]]; then
    cat<<EOF
Usage:
    ${0##*/} <input mp4> <output mp4>
EOF
    exit 1
fi

for x in bc awk ffprobe ffmpeg; do
    if ! type &>/dev/null $x; then
        echo >&2 "$x should be installed"
        ((err++))
    fi
done

((err > 0)) && exit 1

duration=$(ffprobe -select_streams v -show_streams "$1" 2>/dev/null |
    awk -F= '$1 == "duration"{print $2}')
final_cut=$(bc -l <<< "$duration - $fade_duration")
ffmpeg -i "$1" \
    -filter:v "fade=out:st=$final_cut:d=$fade_duration" \
    -af "afade=t=out:st=$final_cut:d=$fade_duration" \
    -c:v libx264 -crf 22 -preset veryfast -strict -2 "$2"

Usage :

FadeOutMp4 <input mp4> <output mp4>
Share:
9,841

Related videos on Youtube

Manu
Author by

Manu

European of French nationality. Web developer &amp; UX enthusiast.

Updated on September 18, 2022

Comments

  • Manu
    Manu almost 2 years

    I have a script to encode videos (using mencoder), but is there a way to add a simple "fade out/in to black" from the command line, preferably free (as in open source).

    It could be either on Windows or Ubuntu Linux.

  • Foxinni
    Foxinni almost 4 years
    Changes the colors somewhat? Is that possible?
  • Danny Lo
    Danny Lo over 3 years
    Thanks Gilles, that script is useful for me. I just had to change line 29 to final_cut=$(echo "$duration - $fade_duration" | sed 's/\r//' | bc -l) since i'm on WSL