Note that .mkv files only allows .jpg and .png image files. FFmpeg also only accepts .jpg and .png files when embedding images in .mp4 containers.

Add an embedded thumbnail to a .mp4 container (as a mjpeg video stream)

ffmpeg -iINPUT_VIDEO.mp4 -iINPUT_COVER.jpg -map 0 -map 1 -c copy-disposition:v:1 attached_picOUTPUT.mp4

Each input file in FFmpeg is specified with the option -i

The option -map is used to map where each input (and optionally which stream inside that input) will be located as a stream inside the output file. In particular, the first appearance of the option -map (...) means that it is referring to INPUT #0 while the number that follows means (in case of -map 0 it's 0) that it will be the first output stream. Note that if a video file has both a video and an audio track, FFmpeg will automatically select both. Mapping can also be more selective eg. -map 0:v:0 would mean only pick the first (0) v(ideo) stream of INPUT #0.

The second occurrence is -map 1 which means that the second input (the cover image) will be considered as the second stream in output. The .jpg image will be converted by FFmpeg into an mjpeg video stream that serves as an image.

Finally -disposition:v:1 attached_pic tells FFmpeg that the second video stream (v:1) has to be used as a thumbnail image.

If the input file only has 2 tracks (one video and one audio) it would look like this:

Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'INPUT_VIDEO.mp4':
  Metadata:
    ...
  Duration: ...
    Stream #0:0(und): Video: h264 ... (default)
    Stream #0:1(und): Audio: aac ... (default)

After embedding the image:

Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'COVER_VIDEO.mp4':
  Metadata:
    ...
  Duration: ...
    Stream #0:0(und): Video: h264 ... (default)
    Stream #0:1(und): Audio: aac ... (default)
    Stream #0:2: Video: mjpeg ... (attached pic)

Add embedded thumbnails to .mkv container

In .mkv files the name of the image to use as a thumbnail specifies which roles it serves: up to 4 cover images can be added in total. See: https://www.matroska.org/technical/attachments.html

Untitled

The role of the image is specified by the filename=... option in FFmpeg. If we want to add 2 covers, one for the thumbnail and one as a poster the command will be:

ffmpeg -i INPUT_VIDEO.mkv -attach INPUT_NORMAL_COVER.jpg -attach INPUT_SMALL_COVER.jpg -c copy -metadata:s:t mimetype=image/jpg -metadata:s:t:0 filename=cover.jpg -metadata:s:t:1 filename=small_cover.jpg out.mkv