Create playable videostream from bytes/frames Java/Android

Viewed 595

I have org.webrtc.VideoFrame frames stream.

The frames are coming one by one. Is there any lib or tool to convert frames to stream on a fly?

I could successfully take convert that VideoFrames to array of bytes. (similar as this question that uses it for image) Android org.webrtc.VideoRenderer.I420Frame arrays to PreviewCallback.onPreviewFrame byte[]

I would like to create playable stream video but when I try to play the one create with FileOutputStream or any other stream that can be passed to FFMPEG for example it is not playable, so seems it needs muxer to create it?

private fun addMetaDataToVideo() {
   val file = File(context.getExternalFilesDir(Environment.DIRECTORY_DCIM).toString() + "/" 
   +"KYR", "${videoNamePrefix}}.mp4")
   val out = FileOutputStream(file)
   listOfFrames.forEach { out.write(it) }
   out.close()
   addMetaDataToVideo(file)
}


   private fun addMetaDataToVideo(videoFile: File) {
       val values = ContentValues(3)
       values.put(MediaStore.Video.Media.MIME_TYPE, "video/mp4")
      // values.put(MediaStore.Video.Media.DURATION, getVideoDuration(videoFile))
       values.put(MediaStore.Video.Media.DATA, videoFile.absolutePath)
       context.contentResolver.insert(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, values)
   }

1 Answers

Using ffmpeg is the most common way to do it. Can you put your frames into some sort of image aray? BufferedImage or something like it? Once you have your images you can use the following line. Like explained here: https://hamelot.io/visualization/using-ffmpeg-to-convert-a-set-of-images-into-a-video/

Try to write your frames into a BufferedImage,

ffmpeg -r 60 -f image2 -s 1920x1080 -i pic%04d.png -vcodec libx264 -crf 25  -pix_fmt yuv420p test.mp4

Related