To include embedded cover art when converting an mp3 to mkv file, I use below command. In this case, the audio remains in the output file with the same settings as the input mp3.
See more details at this Stack Overflow thread at: How to add album art with ffmpeg?
ffmpeg -i "input.mp3" -map 0:0 -map 0:1 -c copy -id3v2_version 3 -metadata:s:v title="Album cover" -metadata:s:v comment="Cover (front)" "output.mkv"
If you would like to attach an image that is not embedded in the mp3 file then this is a way to do that:
ffmpeg -i "input_sample.mp3" -attach "image_cover.jpg" -map 0 -c:v copy -metadata:s:t mimetype="image/jpg" -metadata:s:t:0 filename="cover.jpg" "output_sample.mkv"
Output of ffprobe input_sample.mp3:
Stream #0:0: Audio: mp3, 44100 Hz, stereo, fltp, 320 kb/s
Stream #0:1: Video: mjpeg (Baseline), yuvj420p(pc, bt470bg/unknown/unknown), 1018x1023 [SAR 72:72 DAR 1018:1023], 90k tbr, 90k tbn (attached pic)
Metadata:
title : Album cover
comment : Cover (front)
Output of ffprobe output_sample.mkv:
Stream #0:0: Audio: mp3, 44100 Hz, stereo, fltp, 320 kb/s
Stream #0:1: Video: mjpeg (Baseline), yuvj420p(pc, bt470bg/unknown/unknown), 1018x1023 [SAR 72:72 DAR 1018:1023], 1k tbr, 1k tbn
Metadata:
title : Album cover
COMMENT : Cover (front)
Stream #0:2: Attachment: none
Metadata:
filename : cover.jpg
mimetype : image/jpg
In the output file, streams #0:0, #0:1 were kept and stream #0:2 was included, which is the cover art stream.
The audio on the output remains with the same settings as the input. If you wanted to change the audio in MKV format use -c:a libvorbis -q:a 9 ,VBR format, or c:a libvorbis -b:a 320, CBR format, but the file will be re-encoded. Note: q:a 9 is equivalent to 320 kbps VBR.
Equivalences when encoding with libvorbis to MKV format:
-q:a 4 ~= 128 kbps VBR
-q:a 5 ~= 160 kbps VBR
-q:a 6 ~= 192 kbps VBR
-q:a 7 ~= 224 kbps VBR
-q:a 8 ~= 256 kbps VBR
-q:a 9 ~= 320 kbps VBR
-q:a 10 ~= 500 kbps VBR
Note about the second command posted at top:
I used the second command that I posted here in some mkv files I have and, although they are attached without an error message, when I went to convert the files with the cover art into mp3, the system showed a codec error in the image and only converted the audio to mp3.
What happens is that the mjpeg codec, looks for information in the command so that it can encode the image correctly, this information is in the mimetype="image/jpg" field. Note that where is jpg, it has to be jpeg. As in the command I posted this, the field was with jpg, the codec, despite not giving an error, simply attached the image without any encoding, causing many problems at the time of conversion.
the correct command is:
ffmpeg -i "without_cover.mkv" -attach "art_cover.jpg" -map 0 -c copy -metadata:s:t mimetype="image/jpeg" -metadata:s:t:0 filename="cover.jpeg" "with_cover.mkv"
thanks