How to set cover art to mp4 media file jcodec-android

394 Views Asked by At

I'm using jcodec.

I'm trying to set cover art to an mp4 file, but it's not working. I've done the following:

fun AppCompatActivity.setAlbumCover(filePath: String) {
    try {
        val file = File(filePath)
        val bitmap = ThumbnailUtils.createVideoThumbnail(file.absolutePath, MediaStore.Video.Thumbnails.MINI_KIND)
        if (bitmap != null) {
            val byteArrayOutputStream = ByteArrayOutputStream()
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream)
            val byteArray = byteArrayOutputStream.toByteArray()
            val base64Str = Base64.encodeToString(byteArray, Base64.DEFAULT)
            if (base64Str != null) {
                val mediaMeta = MetadataEditor.createFrom(file)
                if (mediaMeta != null) {
                    val meta = mediaMeta.keyedMeta
                    if (meta != null) {
                        meta["coverart"] = MetaValue.createString(base64Str)
                    }
                    mediaMeta.save(false)
                }
            }
        }
    } catch (e: Exception) {
        e.printStackTrace()
    }
}

After that I upload this video to server and when I load the thumbnail from video URL using MediaMetadataRetriever::getEmbededPicture. It should return byte[] not null.

How to achieve this?

1

There are 1 best solutions below

0
On

Use Itunes metaData and code 0x636F7672.

For example:

ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
coverBitmap.compress(Bitmap.CompressFormat.JPEG, 100,byteArrayOutputStream);
MetaValue coverMetaValue = MetaValue.createOther(MetaValue.TYPE_JPEG, byteArrayOutputStream.toByteArray());

MetadataEditor metadataEditor = MetadataEditor.createFrom(new File(url));
metadataEditor.getItunesMeta().put(0x636F7672 , coverMetaValue); 
metadataEditor.save(false);