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
-
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 :
-
Grafika is based on https://source.android.com/devices/graphics/architecture
-
Grafika is a dumping ground for Android graphics & media hacks.
-
It consists of pure JAVA classes which can be easily modified (open source)
-
No NDK , no extra setting up required.
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
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
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.