Optimise camera recordings on the fly without FFMPEG or other libraries

Posted By : Ajit Jati | 27-Jun-2017

In today’s modern graphics world if you are related to mobile application development, the odds are great that you are being asked to optimise video/media before sharing to social sites or live streaming with friends. But keeping below points in mind :

 

  • The video should be recorded from camera in HD

  • The optimise tasks should be completed in no time (on-the-fly)

  • The optimise tasks include encoding,decoding, video resizing, setting bitrates, framerates, key-frames(i-frames) etc

 
To optimise (encode,decode etc) videos in android there are few third party yet open source libraries available on the web. FFMPEG library with JNI wrapper is one of the top choices for android developers.But it comes with few pullbacks as well :
 
  • FFMPEG library isn’t an independent program on its own. It depends on many low level libraries. Setting up FFMPEG is heck of a task and sometimes developers end up using old and existing compiled versions of FFMPEG available on the web.

  • FFMPEG can’t process camera videos on the fly. It takes min 5 sec to optimise a video of length 10 sec, even if setting the ‘preset’ option to ‘ultrafast’.

  • The size of APK increases drastically.

 

In the rescue, here comes Google’s open source Grafika project (https://github.com/google/grafika).

Grafika is :

 

I have created a sample project (https://github.com/ajitjati/simple-grafika) using Grafika which does following :

  • Attempts to record at 720p from the default camera, displaying the preview and recording it simultaneously.(CameraCaptureActivity.java)
  • Recording continues until stopped. If you back out and return, recording will start again, with a real-time gap. If you try to play the movie while it's recording, you will see an incomplete file (and probably cause the play movie activity to crash).

  • The output is stored at home directory of your SD card ("grafika-2372377731.mp4").

  • It can optimise your media/video on-the-fly from camera while recording

  • You can change video key-frames, bitrates,framerates,size and encodings(VideoEncoderCore.java)

  • The best part is everything will happen on the fly so when your recording stops, the video should be already prepared :)

 

How it works :

  • It captures frames(camera prefiew) from camera

  • Render the frame to the SurfaceView, on GLSurfaceView's renderer thread

  • Render the frame to the mediacodec's input surface, on the encoder thread, if recording is enabled

  • Video optimisation values can be updated on VideoEncoderCore.java

 

Note : You can use camera/front and back switching buttons, preview players to the app as well. Create pull requests if you want to contribute.

Cheers!

Reference - https://github.com/google/grafika

Reference - https://github.com/ajitjati/simple-grafika

 

 

About Author

Author Image
Ajit Jati

Ajit is an proficient Project Manager specializing in Mobile technologies. He possesses extensive development experience in Titanium, Android, Kotlin, Roku, and PHP. Ajit has successfully delivered various projects in the OTT, AR/VR, and Travel industries. His skill set includes developing and maintaining project plans, schedules, and budgets, ensuring timely delivery while staying within the allocated budget. He excels in collaborating closely with clients to define project scope and requirements, establish project timelines and milestones, and effectively manage expectations. He conducts regular project status meetings, ensuring effective communication and providing updates to clients and stakeholders on project progress, risks, and issues. Furthermore, Ajit takes on the role of a coach and mentor for team,offering guidance on project management best practices and assisting in their skill development.

Request for Proposal

Name is required

Comment is required

Sending message..