Skip to main content
added trimmed video input w/ audio crossfade example
Source Link
llogan
  • 60.6k
  • 17
  • 130
  • 152

Crossfade between two videos with audio

Select 5 second segment from each input and add a 1 second crossfade:

ffmpeg -i input0.mp4 -i input1.mp4 -filter_complex \
"[0:v]trim=start=5:end=10,setpts=PTS-STARTPTS[v0];
 [1:v]trim=start=12:end=17,setpts=PTS-STARTPTS+4/TB,format=yuva444p,fade=st=4:d=1:t=in:alpha=1[v1];
 [v0][v1]overlay,format=yuv420p[v];
 [0:a]atrim=start=5:end=10,asetpts=PTS-STARTPTS[a0];
 [1:a]atrim=start=12:end=17,asetpts=PTS-STARTPTS[a1];
 [a0][a1]acrossfade=d=1[a]" \
-map "[v]" -map "[a]" output.mp4

Crossfade between two videos with audio

Select 5 second segment from each input and add a 1 second crossfade:

ffmpeg -i input0.mp4 -i input1.mp4 -filter_complex \
"[0:v]trim=start=5:end=10,setpts=PTS-STARTPTS[v0];
 [1:v]trim=start=12:end=17,setpts=PTS-STARTPTS+4/TB,format=yuva444p,fade=st=4:d=1:t=in:alpha=1[v1];
 [v0][v1]overlay,format=yuv420p[v];
 [0:a]atrim=start=5:end=10,asetpts=PTS-STARTPTS[a0];
 [1:a]atrim=start=12:end=17,asetpts=PTS-STARTPTS[a1];
 [a0][a1]acrossfade=d=1[a]" \
-map "[v]" -map "[a]" output.mp4
added gif and crossfade
Source Link
llogan
  • 60.6k
  • 17
  • 130
  • 152

Dip/fade to black

fade exampleScroll down for crossfade method.

Basic example

fade example

Example where each image displayed for 5 seconds and each has a fade that lasts 1 second. Each image input has the same width, height, and sample aspect ratio. If they vary in size see example #3 below.

MP4 output

See the examples in Resizing videos to fit into static sized player if you want to crop (fill the screen) instead of pad (letterbox/pillarbox), or if you want to prevent upscaling.

GIF output

Adds the filters from How do I convert a video to GIF using ffmpeg, with reasonable quality?

ffmpeg \
-framerate 10 -loop 1 -t 5 -i input0.png \
-framerate 10 -loop 1 -t 5 -i input1.png \
-framerate 10 -loop 1 -t 5 -i input2.png \
-framerate 10 -loop 1 -t 5 -i input3.png \
-framerate 10 -loop 1 -t 5 -i input4.png \
-filter_complex \
"[0:v]fade=t=out:st=4:d=1[v0]; \
 [1:v]fade=t=in:st=0:d=1,fade=t=out:st=4:d=1[v1]; \
 [2:v]fade=t=in:st=0:d=1,fade=t=out:st=4:d=1[v2]; \
 [3:v]fade=t=in:st=0:d=1,fade=t=out:st=4:d=1[v3]; \
 [4:v]fade=t=in:st=0:d=1,fade=t=out:st=4:d=1[v4]; \
 [v0][v1][v2][v3][v4]concat=n=5:v=1:a=0,split[v0][v1]; \
 [v0]palettegen[p];[v1][p]paletteuse[v]" -map "[v]" out.gif

Use the -loop output option to control the number of times the GIF loops. Default is infinite loop if this option is not used. A value of -1 is no loop.

Options and filters used:

  • -t to set duration in seconds of each input.

  • -loop 1 loops the image otherwise it would have a duration of 1 frame.

  • -framerate to set input image frame rate (default when undeclared is 25). Useful for making GIF.

  • scalescale with pad to fit the input images into a specific, uniform size (used in example #3).

  • fadefade to fade in and out. d is the duration of the fade. st is when it starts.

  • concatconcat to concatenate (or "join") each image.

  • formatformat to output a chroma subsampling scheme that is compatible with non-FFmpeg based players if outputting MP4 and encoding with libx264 (the default encoder for MP4 output if it is supported by your build).

  • split to make copies of a filter output. Needed by the palette* filters to do everything in one command.

  • palettegen and paletteuse for making nice looking GIF.

 

Using melt from the MLT Multimedia Framework may be easierExample where each image displayed for this task:5 seconds and each has a crossfade that lasts 1 second. Each image input has the same width, height, and sample aspect ratio. If they vary in size then adapt example #3 above.

MP4 output

meltffmpeg \
-verboseloop 1 -t 5 -i 1.png \
-profileloop atsc_720p_251 -t 5 -i 2.png \
001-loop 1 -t 5 -i 3.png out=50\
-loop 1 -t 5 -i 4.png \
002-loop 1 -t 5 -i 5.png out=75\
-filter_complex \
"[1]format=yuva444p,fade=d=1:t=in:alpha=1,setpts=PTS-mixSTARTPTS+4/TB[f0]; 25\
 [2]format=yuva444p,fade=d=1:t=in:alpha=1,setpts=PTS-mixerSTARTPTS+8/TB[f1]; luma\
 [3]format=yuva444p,fade=d=1:t=in:alpha=1,setpts=PTS-STARTPTS+12/TB[f2]; \
003 [4]format=yuva444p,fade=d=1:t=in:alpha=1,setpts=PTS-STARTPTS+16/TB[f3]; \
 [0][f0]overlay[bg1];[bg1][f1]overlay[bg2];[bg2][f2]overlay[bg3]; \
 [bg3][f3]overlay,format=yuv420p[v]" -map "[v]" -movflags +faststart out.mp4

With audio

ffmpeg \
-loop 1 -t 5 -i 1.png out=75\
-loop 1 -mixt 255 -mixeri luma2.png \
004-loop 1 -t 5 -i 3.png out=75\
-loop 1 -mixt 255 -mixeri luma4.png \
005-loop 1 -t 5 -i 5.png out=75\
-i music.mp3 \
-mixfilter_complex 25\
"[1]format=yuva444p,fade=d=1:t=in:alpha=1,setpts=PTS-STARTPTS+4/TB[f0]; \
 [2]format=yuva444p,fade=d=1:t=in:alpha=1,setpts=PTS-mixerSTARTPTS+8/TB[f1]; luma\
 [3]format=yuva444p,fade=d=1:t=in:alpha=1,setpts=PTS-STARTPTS+12/TB[f2]; \
 [4]format=yuva444p,fade=d=1:t=in:alpha=1,setpts=PTS-consumerSTARTPTS+16/TB[f3]; avformat\
 [0][f0]overlay[bg1];[bg1][f1]overlay[bg2];[bg2][f2]overlay[bg3]; \
 [bg3][f3]overlay,format=yuv420p[v]" -map "[v]" -map 5:outputa -shortest -movflags +faststart out.mp4

GIF output

ffmpeg vcodec=libx264\
-framerate an=110 -loop 1 -t 5 -i 1.png \
-framerate 10 -loop 1 -t 5 -i 2.png \
-framerate 10 -loop 1 -t 5 -i 3.png \
-framerate 10 -loop 1 -t 5 -i 4.png \
-framerate 10 -loop 1 -t 5 -i 5.png \
-filter_complex \
"[1]format=rgba,fade=d=1:t=in:alpha=1,setpts=PTS-STARTPTS+4/TB[f0]; \
 [2]format=rgba,fade=d=1:t=in:alpha=1,setpts=PTS-STARTPTS+8/TB[f1]; \
 [3]format=rgba,fade=d=1:t=in:alpha=1,setpts=PTS-STARTPTS+12/TB[f2]; \
 [4]format=rgba,fade=d=1:t=in:alpha=1,setpts=PTS-STARTPTS+16/TB[f3]; \
 [0][f0]overlay[bg1];[bg1][f1]overlay[bg2];[bg2][f2]overlay[bg3];[bg3][f3]overlay,split[v0][v1]; \
 [v0]palettegen[p];[v1][p]paletteuse[v]" -map "[v]" out.gif
  • Use a profile that suits your inputs files. You can list profiles with melt -query profiles and get info about a particular profile, such as melt -query "profile=atsc_720p_25".

  • out sets the duration of each image. The value is in frames. This example is using a profile that sets a frame rate of 25, so 25 frames equals 1 second.

  • mix sets the duration of the fade. The value is in frames.

  • mixer sets the type of mix.

  • See MLT Documentation and Melt Documentation for more info.

Use the -loop output option to control the number of times the GIF loops. Default is infinite loop if this option is not used. A value of -1 is no loop.

Dip to black

fade example

Basic example

Example where each image displayed for 5 seconds and each has a fade that lasts 1 second. Each image input has the same width, height, and sample aspect ratio. If they vary in size see example #3.

Options and filters used:

  • -t to set duration in seconds of each input.

  • scale with pad to fit the input images into a specific, uniform size (used in example #3).

  • fade to fade in and out. d is the duration of the fade. st is when it starts.

  • concat to concatenate (or "join") each image.

  • format to output a chroma subsampling scheme that is compatible with non-FFmpeg based players if encoding with libx264 (the default encoder for MP4 output if it is supported by your build).

Using melt from the MLT Multimedia Framework may be easier for this task:

melt -verbose \
-profile atsc_720p_25 \
001.png out=50 \
002.png out=75 -mix 25 -mixer luma \
003.png out=75 -mix 25 -mixer luma \
004.png out=75 -mix 25 -mixer luma \
005.png out=75 -mix 25 -mixer luma \
-consumer avformat:output.mp4 vcodec=libx264 an=1
  • Use a profile that suits your inputs files. You can list profiles with melt -query profiles and get info about a particular profile, such as melt -query "profile=atsc_720p_25".

  • out sets the duration of each image. The value is in frames. This example is using a profile that sets a frame rate of 25, so 25 frames equals 1 second.

  • mix sets the duration of the fade. The value is in frames.

  • mixer sets the type of mix.

  • See MLT Documentation and Melt Documentation for more info.

Dip/fade to black

Scroll down for crossfade method.

fade example

Example where each image displayed for 5 seconds and each has a fade that lasts 1 second. Each image input has the same width, height, and sample aspect ratio. If they vary in size see example #3 below.

MP4 output

See the examples in Resizing videos to fit into static sized player if you want to crop (fill the screen) instead of pad (letterbox/pillarbox), or if you want to prevent upscaling.

GIF output

Adds the filters from How do I convert a video to GIF using ffmpeg, with reasonable quality?

ffmpeg \
-framerate 10 -loop 1 -t 5 -i input0.png \
-framerate 10 -loop 1 -t 5 -i input1.png \
-framerate 10 -loop 1 -t 5 -i input2.png \
-framerate 10 -loop 1 -t 5 -i input3.png \
-framerate 10 -loop 1 -t 5 -i input4.png \
-filter_complex \
"[0:v]fade=t=out:st=4:d=1[v0]; \
 [1:v]fade=t=in:st=0:d=1,fade=t=out:st=4:d=1[v1]; \
 [2:v]fade=t=in:st=0:d=1,fade=t=out:st=4:d=1[v2]; \
 [3:v]fade=t=in:st=0:d=1,fade=t=out:st=4:d=1[v3]; \
 [4:v]fade=t=in:st=0:d=1,fade=t=out:st=4:d=1[v4]; \
 [v0][v1][v2][v3][v4]concat=n=5:v=1:a=0,split[v0][v1]; \
 [v0]palettegen[p];[v1][p]paletteuse[v]" -map "[v]" out.gif

Use the -loop output option to control the number of times the GIF loops. Default is infinite loop if this option is not used. A value of -1 is no loop.

Options and filters used:

  • -t to set duration in seconds of each input.

  • -loop 1 loops the image otherwise it would have a duration of 1 frame.

  • -framerate to set input image frame rate (default when undeclared is 25). Useful for making GIF.

  • scale with pad to fit the input images into a specific, uniform size (used in example #3).

  • fade to fade in and out. d is the duration of the fade. st is when it starts.

  • concat to concatenate (or "join") each image.

  • format to output a chroma subsampling scheme that is compatible with non-FFmpeg based players if outputting MP4 and encoding with libx264 (the default encoder for MP4 output if it is supported by your build).

  • split to make copies of a filter output. Needed by the palette* filters to do everything in one command.

  • palettegen and paletteuse for making nice looking GIF.

 

Example where each image displayed for 5 seconds and each has a crossfade that lasts 1 second. Each image input has the same width, height, and sample aspect ratio. If they vary in size then adapt example #3 above.

MP4 output

ffmpeg \
-loop 1 -t 5 -i 1.png \
-loop 1 -t 5 -i 2.png \
-loop 1 -t 5 -i 3.png \
-loop 1 -t 5 -i 4.png \
-loop 1 -t 5 -i 5.png \
-filter_complex \
"[1]format=yuva444p,fade=d=1:t=in:alpha=1,setpts=PTS-STARTPTS+4/TB[f0]; \
 [2]format=yuva444p,fade=d=1:t=in:alpha=1,setpts=PTS-STARTPTS+8/TB[f1]; \
 [3]format=yuva444p,fade=d=1:t=in:alpha=1,setpts=PTS-STARTPTS+12/TB[f2]; \
 [4]format=yuva444p,fade=d=1:t=in:alpha=1,setpts=PTS-STARTPTS+16/TB[f3]; \
 [0][f0]overlay[bg1];[bg1][f1]overlay[bg2];[bg2][f2]overlay[bg3]; \
 [bg3][f3]overlay,format=yuv420p[v]" -map "[v]" -movflags +faststart out.mp4

With audio

ffmpeg \
-loop 1 -t 5 -i 1.png \
-loop 1 -t 5 -i 2.png \
-loop 1 -t 5 -i 3.png \
-loop 1 -t 5 -i 4.png \
-loop 1 -t 5 -i 5.png \
-i music.mp3 \
-filter_complex \
"[1]format=yuva444p,fade=d=1:t=in:alpha=1,setpts=PTS-STARTPTS+4/TB[f0]; \
 [2]format=yuva444p,fade=d=1:t=in:alpha=1,setpts=PTS-STARTPTS+8/TB[f1]; \
 [3]format=yuva444p,fade=d=1:t=in:alpha=1,setpts=PTS-STARTPTS+12/TB[f2]; \
 [4]format=yuva444p,fade=d=1:t=in:alpha=1,setpts=PTS-STARTPTS+16/TB[f3]; \
 [0][f0]overlay[bg1];[bg1][f1]overlay[bg2];[bg2][f2]overlay[bg3]; \
 [bg3][f3]overlay,format=yuv420p[v]" -map "[v]" -map 5:a -shortest -movflags +faststart out.mp4

GIF output

ffmpeg \
-framerate 10 -loop 1 -t 5 -i 1.png \
-framerate 10 -loop 1 -t 5 -i 2.png \
-framerate 10 -loop 1 -t 5 -i 3.png \
-framerate 10 -loop 1 -t 5 -i 4.png \
-framerate 10 -loop 1 -t 5 -i 5.png \
-filter_complex \
"[1]format=rgba,fade=d=1:t=in:alpha=1,setpts=PTS-STARTPTS+4/TB[f0]; \
 [2]format=rgba,fade=d=1:t=in:alpha=1,setpts=PTS-STARTPTS+8/TB[f1]; \
 [3]format=rgba,fade=d=1:t=in:alpha=1,setpts=PTS-STARTPTS+12/TB[f2]; \
 [4]format=rgba,fade=d=1:t=in:alpha=1,setpts=PTS-STARTPTS+16/TB[f3]; \
 [0][f0]overlay[bg1];[bg1][f1]overlay[bg2];[bg2][f2]overlay[bg3];[bg3][f3]overlay,split[v0][v1]; \
 [v0]palettegen[p];[v1][p]paletteuse[v]" -map "[v]" out.gif

Use the -loop output option to control the number of times the GIF loops. Default is infinite loop if this option is not used. A value of -1 is no loop.

added requested example for arbitrary input sizes
Source Link
llogan
  • 60.6k
  • 17
  • 130
  • 152

Basic example

This is not as trivial as you would probably expect, but it is definitely possible. Example where each image displayed for 5 seconds and each has a fade that lasts 1 second. Each image input has the same width, height, and sample aspect ratio. If they vary in size see example #3.

With audio

For input images with varying or arbitrary sizes

Options and filters usedLike the first example, but with input images that vary in width x height. They will be padded to fit within a 1280x720 box:

ffmpeg \
-loop 1 -t 5 -i input0.png \
-loop 1 -t 5 -i input1.png \
-loop 1 -t 5 -i input2.png \
-loop 1 -t 5 -i input3.png \
-loop 1 -t 5 -i input4.png \
-filter_complex \
"[0:v]scale=1280:720:force_original_aspect_ratio=decrease,pad=1280:720:(ow-iw)/2:(oh-ih)/2,setsar=1,fade=t=out:st=4:d=1[v0]; \
 [1:v]scale=1280:720:force_original_aspect_ratio=decrease,pad=1280:720:(ow-iw)/2:(oh-ih)/2,setsar=1,fade=t=in:st=0:d=1,fade=t=out:st=4:d=1[v1]; \
 [2:v]scale=1280:720:force_original_aspect_ratio=decrease,pad=1280:720:(ow-iw)/2:(oh-ih)/2,setsar=1,fade=t=in:st=0:d=1,fade=t=out:st=4:d=1[v2]; \
 [3:v]scale=1280:720:force_original_aspect_ratio=decrease,pad=1280:720:(ow-iw)/2:(oh-ih)/2,setsar=1,fade=t=in:st=0:d=1,fade=t=out:st=4:d=1[v3]; \
 [4:v]scale=1280:720:force_original_aspect_ratio=decrease,pad=1280:720:(ow-iw)/2:(oh-ih)/2,setsar=1,fade=t=in:st=0:d=1,fade=t=out:st=4:d=1[v4]; \
 [v0][v1][v2][v3][v4]concat=n=5:v=1:a=0,format=yuv420p[v]" -map "[v]" out.mp4

Options and filters used:

  • -t to set duration in seconds of each input.

  • fadescale with pad to fit the input images into a specific, uniform size (used in example #3).

  • fade to fade in and out. d is the duration of the fade. st is when it starts.

  • concatconcat to concatenate (or "join") each image.

  • formatformat to output a chroma subsampling scheme that is compatible with non-FFmpeg based players if encoding with libx264 (defaultthe default encoder for MP4 output if it is supported by your build).

This is not as trivial as you would probably expect, but it is definitely possible. Example where each image displayed for 5 seconds and each has a fade that lasts 1 second.

Options and filters used:

  • -t to set duration in seconds of each input.

  • fade to fade in and out. d is the duration of the fade. st is when it starts.

  • concat to concatenate (or "join") each image.

  • format to output a chroma subsampling scheme that is compatible with non-FFmpeg based players if encoding with libx264 (default for MP4 output if it is supported by your build).

Basic example

Example where each image displayed for 5 seconds and each has a fade that lasts 1 second. Each image input has the same width, height, and sample aspect ratio. If they vary in size see example #3.

With audio

For input images with varying or arbitrary sizes

Like the first example, but with input images that vary in width x height. They will be padded to fit within a 1280x720 box:

ffmpeg \
-loop 1 -t 5 -i input0.png \
-loop 1 -t 5 -i input1.png \
-loop 1 -t 5 -i input2.png \
-loop 1 -t 5 -i input3.png \
-loop 1 -t 5 -i input4.png \
-filter_complex \
"[0:v]scale=1280:720:force_original_aspect_ratio=decrease,pad=1280:720:(ow-iw)/2:(oh-ih)/2,setsar=1,fade=t=out:st=4:d=1[v0]; \
 [1:v]scale=1280:720:force_original_aspect_ratio=decrease,pad=1280:720:(ow-iw)/2:(oh-ih)/2,setsar=1,fade=t=in:st=0:d=1,fade=t=out:st=4:d=1[v1]; \
 [2:v]scale=1280:720:force_original_aspect_ratio=decrease,pad=1280:720:(ow-iw)/2:(oh-ih)/2,setsar=1,fade=t=in:st=0:d=1,fade=t=out:st=4:d=1[v2]; \
 [3:v]scale=1280:720:force_original_aspect_ratio=decrease,pad=1280:720:(ow-iw)/2:(oh-ih)/2,setsar=1,fade=t=in:st=0:d=1,fade=t=out:st=4:d=1[v3]; \
 [4:v]scale=1280:720:force_original_aspect_ratio=decrease,pad=1280:720:(ow-iw)/2:(oh-ih)/2,setsar=1,fade=t=in:st=0:d=1,fade=t=out:st=4:d=1[v4]; \
 [v0][v1][v2][v3][v4]concat=n=5:v=1:a=0,format=yuv420p[v]" -map "[v]" out.mp4

Options and filters used:

  • -t to set duration in seconds of each input.

  • scale with pad to fit the input images into a specific, uniform size (used in example #3).

  • fade to fade in and out. d is the duration of the fade. st is when it starts.

  • concat to concatenate (or "join") each image.

  • format to output a chroma subsampling scheme that is compatible with non-FFmpeg based players if encoding with libx264 (the default encoder for MP4 output if it is supported by your build).

added requested example adding audio
Source Link
llogan
  • 60.6k
  • 17
  • 130
  • 152
Loading
removed ugly ffmpeg crossfade example. will add a better one later.
Source Link
llogan
  • 60.6k
  • 17
  • 130
  • 152
Loading
simplify and add melt example
Source Link
llogan
  • 60.6k
  • 17
  • 130
  • 152
Loading
add crossfade
Source Link
llogan
  • 60.6k
  • 17
  • 130
  • 152
Loading
Source Link
llogan
  • 60.6k
  • 17
  • 130
  • 152
Loading