Implement features similar to Livephoto in iphone on Android phones
Source code Sharing
Github:https://github.com/amazingyyc/deepred
Code Description:
1. Change the resolution of the video: Modify the value of the min_preview_width in the com.deepcolor.deepred.shot.CameraInstance, the larger the min_preview_width the greater the video resolution.
2. Change the video bit rate: Modify the int encoder::get_bit_rate_by_height (int height) function under Jni/encoder.cpp
3. Change the video frame rate: Modify the Video_fps value in the Jni/content.h
3. Change the audio BIT rate: Modify the Audio_bit_rate value under Jni/content.h
4. Change the video length: Modify the Add_second_part_msg_delay_ in Time_duration and com.deepcolor.deepred.shot.ShotInstance under Jni/content.h The value of time
5. Change the video storage location: Modify the Getapppath () function under Com.deepcolor.deepred.util.FileUtil
Demo
When you click the "Shooting button" and press the mouse click on the stopwatch "start", you can see the recording of about 1.5s video at the same time
Interface:
1: Middle "yellow camera" icon: whether to turn on the Livephoto function
2: "Flash icon": Focus mode
3: Swipe left and right to toggle the camera
Principle:
1: Maintain 2 queues in the JNI layer, storing 1.5s of video and audio data respectively. The video data directly calls the system's own callback function Onpreviewframe (byte[] data, camera camera) to each frame of the video data.
audio New A thread loops from the Audiorecord read audio data. When the data exceeds 1.5s, the pre-team data is ejected and then added.
2: When you click the shooting button
A: Create a new thread to copy the video & audio data from the current queue to the thread and start using FFmpeg to encode the data into a MP4 video file
B: Simultaneously send a 1.5s "delay message" when the main thread receives a "deferred message", then copies the data from the current queue to the corresponding thread.
3: Until all the data is encoded in the file, end
Problem:
1: Slow! Slow! Slow! , very slow. Encode 3s 1080*760 video for a total of 45 frames, need about 16s!! 640*480 also needs about 5s, there is no way to do real-time shooting.
The reason is that using ffmpeg pure software encoding speed is naturally very slow, the solution can use the Android hardware encoding interface Mediacodec instead of ffmpeg video encoding, mobile phone support for Mediacodec there is no way to test.
2: The Source code button at the same time shooting a "small video", no picture shooting, call the system interface can be realized, too lazy do not want to do .... Place of modification in: com.deepcolor.deepred.sho.ShotInstance
The Takepicture () function.
3: Video playback of the Gaussian blur is not realized, to achieve Gaussian blur can refer to the switch effect of the front and rear camera, code in the Com.deepcolor.deepred.view.CameraView.
Implement Livephoto features & source code on Android