javacv icon indicating copy to clipboard operation
javacv copied to clipboard

Get MP4 cover image

Open Tengyyy opened this issue 1 year ago • 6 comments

Is there a way to get cover image of mp4 file? It doesnt show up in any of the metadata fields, like it does in jcodec library.

Tengyyy avatar Aug 25 '22 00:08 Tengyyy

It seems like FFmpeg exposes it as a separate video stream. Try to call FFmpegFrameGrabber.setVideoStream(1) followed by start() and grabImage().

saudet avatar Aug 25 '22 10:08 saudet

Thanks. Most of my mp4 files contained 1 audio stream and 2 video streams, the second of which was the image. So setting frameGrabber's video stream to 2 did the trick. However one file had 3 video streams and the cover image was inside the third one. Is there a way to get the number of video streams in the file to then search for the cover image inside the last video stream?

Tengyyy avatar Aug 25 '22 12:08 Tengyyy

The FrameGrabber interface doesn't provide a method to do that right now, but I don't think we can rely on that to identify a stream anyway. I assume we can get some information that tells us what the stream is with getVideoMetadata(). What do you get?

saudet avatar Aug 26 '22 00:08 saudet

This is the output from the getVideoMetadata() call on the default video stream: {creation_time=2011-10-13T16:24:14.000000Z, vendor_id=[0][0][0][0], language=und}

For the stream containing the cover image the getVideoMetadata() call returns an empty map.

The video metadata doesn't seem to return anything that can be used to determine if it contains the cover image.

Tengyyy avatar Aug 26 '22 11:08 Tengyyy

The frameGrabber's console output shows which stream has the attached picture, but getVideoMetadata() doesn't show the same information

Console output: Stream #0:00x1: Video: h264 (Main) (avc1 / 0x31637661), yuv420p(tv, bt709, progressive), 480x360 [SAR 1:1 DAR 4:3], 354 kb/s, 25 fps, 25 tbr, 12800 tbn (default) Metadata: handler_name : ISO Media file produced by Google Inc. vendor_id : [0][0][0][0] Stream #0:10x2: Audio: aac (LC) (mp4a / 0x6134706D), 44100 Hz, stereo, fltp, 96 kb/s (default) Metadata: handler_name : ISO Media file produced by Google Inc. vendor_id : [0][0][0][0] Stream #0:2[0x0]: Video: mjpeg (Baseline), yuvj420p(pc, bt470bg/unknown/unknown), 1259x1264, 90k tbr, 90k tbn (attached pic)

So the last stream here obviously contains the attached image

Tengyyy avatar Aug 26 '22 11:08 Tengyyy

It looks like that information is found in AVStream.disposition: https://github.com/FFmpeg/FFmpeg/blob/n5.1/libavformat/dump.c#L607 It should be pretty easy to update FFmpegFrameGrabber to return that kind of information. Let me know if you encounter any problems doing so though. I'll be here to help!

saudet avatar Aug 26 '22 12:08 saudet