2

I wish to include embedded cover art when converting an mp3 file to mkv (vorbis).

ffmpeg -i input.mp3 output.mkv

will render a file that does play (in mpv) showing an image but the moment I try to seek strange things start to happen.

ffprobe input.mp3

tells me

...
Stream #0:0: Audio: mp3, 44100 Hz, stereo, fltp, 160 kb/s
Stream #0:1: Video: mjpeg (Progressive), yuvj444p(pc, bt470bg/unknown/unknown), 600x595 [SAR 1:1 DAR 120:119], 90k tbr, 90k tbn, 90k tbc (attached pic)
Metadata:
  title           : cover.jpg
  comment         : Cover (front)

while

ffprobe output.mkv

tells me

...
Stream #0:0: Video: h264, none(pc, progressive), 600x595, SAR 1:1 DAR 120:119, 1k tbr, 1k tbn, 2k tbc (default)
...
Stream #0:1: Audio: vorbis, 44100 Hz, stereo, fltp (default)

Apparently ffmpeg converts the image to h264 which is not what I want, I want an embedded picture.

How can I tell ffmpeg to just copy the embedded picture instead of converting it to h264?

fuumind
  • 403
  • 2
  • 7
  • 15
  • did you take a look to https://superuser.com/questions/1041816/combine-one-image-one-audio-file-to-make-one-video-using-ffmpeg – djdomi Dec 26 '21 at 15:56
  • 3
    Does this answer your question? [Combine one image + one audio file to make one video using FFmpeg](https://superuser.com/questions/1041816/combine-one-image-one-audio-file-to-make-one-video-using-ffmpeg) – Giacomo1968 Dec 26 '21 at 16:30

1 Answers1

3

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

fuumind
  • 403
  • 2
  • 7
  • 15
Clamarc
  • 593
  • 1
  • 5
  • 21
  • How can I include two or more embedded image files? – fuumind May 03 '22 at 15:10
  • @fuumind I haven't tested more than one image, when I have time I will test with more than one image, have you already tested it? but I think it would be enough to add one more `-map 0:2` and also the metadata `metadata:s:v:2` or, if you are going to attach the image, one more metadata `metadata:s:t:1`. – Clamarc May 04 '22 at 14:14
  • Did some experimenting. Adding `-map 0:2` will include the second embedded cover art image in the output file. Using `-metadata:s:v:1` will let you set metadata for that image. – fuumind May 06 '22 at 14:34
  • @fuumind Ok, thanks for testing! – Clamarc May 06 '22 at 23:28