Search code examples
androidhttp-live-streamingvideo-thumbnails

how to generating thumbnail from HLS remote URL (*.m3u8) for android


I am looking to generate thumbnail for particular timeframe from HLS remote URL for ex : https://devstreaming-cdn.apple.com/videos/streaming/examples/img_bipbop_adv_example_ts/master.m3u8

I have tried MediaMetadataRetriever but it does not work for live streams (HLS)

any code snippet or library is appreciated, thanks


Solution

  • The FFmpeg library can extract a thumbnail from a particular video frame.

    Use the following function to generate a thumbnail:

    private fun generateThumbnailFromHls(url: String, timestamp: String, outputImage: String) {
            val command = arrayOf(
                "-i", url,
                "-ss", timestamp,
                "-vframes", "1",
                "-q:v", "2",
                "-y", outputImage
            )
    
            FFmpegKit.executeAsync(command.joinToString(" ")) { session ->
                val returnCode = session.returnCode
                if (returnCode.isValueSuccess) {
                    Log.d("FFmpeg", "Thumbnail saved to $outputImage")
                } else {
                    Log.e("FFmpeg", "Error occurred: ${session.failStackTrace}")
                }
            }
        }
    

    Don't forget to add FFmpeg in your app: build.gradle