Probing Video and Audio Metadata on Android Using FFmpeg Part 1

code

After four months of the development of the Infinet app, I learnt an important lesson: The Android MediaMetadataRetriever is out of date and needs the focus it deserves from the Google team.

The Android MediaMetadataRetriever (Available as from API 10) class is often used to extract basic metadata about videos and audio files. An example use of this class is as shown below:

fun parseMP3File(path: String?): Music? {
        val mmr = MediaMetadataRetriever()
        var name: String? = null
        var album: String? = null
        // More code
        try {
            mmr.setDataSource(path)
            name = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_TITLE)
            album = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ALBUM)
            // More code
            
        } catch (e: Exception) {
            e.printStackTrace()
            return null
        } finally {
            mmr.release()
        }
        return Music(name, singer, album, duration, path)
    }

This works fine but has its limits. It provides many of the available metadata types, which are described in the article at Extract Meta-data from Media file: Android; MediaMetaDataRetriever but what if you wanted to extract other metadata like comments? It is worth noting that there is no such attribute like MediaMetadataRetriever.METADATA_KEY_COMMENT, and therefore, one cannot use:

comment = mmr.extractMetadata(MediaMetadataRetriever.METADATA_KEY_COMMENT)

In addition, this class somehow becomes somewhat unreliable and often fails as from API 15. Please see the discussions on StackOverflow such as:

So what are the alternatives?

From a little research, I stumbled upon two third-party libraries:

FFmpegMediaMetadataRetriever is like MediaMetadataRetriever but in addition, provides the missing metadata keys e.g comments data can be extracted like below:

comment = mmr.extractMetadata(FFmpegMediaMetadataRetriever.METADATA_KEY_COMMENT)

The similarity is very calming, however, it is short-lived once you start using it. I experienced, lots of memory leaks and app crashes, which I spent hours trying to fix, but this required re-compiling the JNI modules (not very friendly). Searching through the GitHub issues page, one can see that lots of similar issues have been filed in the past years. Please see:

I did try to raise a few support tickets myself but never received an answer to date, so it is a largely unmaintained repository because:

  • the last release was in 2016
  • issues are left unanswered
  • no promise of any future release as the branches don’t contain any commits

This leaves us with Mobile-ffmpeg. Mobile-ffmpeg is:

  • Newer
  • Multi-platform
  • Actively maintained (releases every few months and daily commits)
  • offers more functionality
  • Active response time on the issues page
  • Has donation options

For these reasons, I think Mobile-ffmpeg is the best alternative, without a doubt. I will be sharing a couple of lessons on how you can integrate it into Android and read all the video and audio metadata that you need. In addition to that, I will show you how you can use it for some video and audio processing in your Android apps.

Stayed tuned for other parts of the series.

Probing Video and Audio Metadata on Android Using FFmpeg Part 1 Probing Video and Audio Metadata on Android Using FFmpeg Part 1 Reviewed by Oti Rowland on March 01, 2020 Rating: 5

No comments:

Powered by Blogger.