Step 1. Add the JitPack repository to your build file
Add it in your root settings.gradle at the end of repositories:
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
mavenCentral()
maven { url 'https://jitpack.io' }
}
}
Add it in your settings.gradle.kts at the end of repositories:
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
mavenCentral()
maven { url = uri("https://jitpack.io") }
}
}
Add to pom.xml
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
Add it in your build.sbt at the end of resolvers:
resolvers += "jitpack" at "https://jitpack.io"
Add it in your project.clj at the end of repositories:
:repositories [["jitpack" "https://jitpack.io"]]
Step 2. Add the dependency
dependencies {
implementation 'com.github.amosyuen:FFmpegVideoRecorder:1.0.2'
}
dependencies {
implementation("com.github.amosyuen:FFmpegVideoRecorder:1.0.2")
}
<dependency>
<groupId>com.github.amosyuen</groupId>
<artifactId>FFmpegVideoRecorder</artifactId>
<version>1.0.2</version>
</dependency>
libraryDependencies += "com.github.amosyuen" % "FFmpegVideoRecorder" % "1.0.2"
:dependencies [[com.github.amosyuen/FFmpegVideoRecorder "1.0.2"]]
The library provides a way to record multiple videos using MediaRecorder and merge them together using FFmpeg Recorder from JavaCV. It is designed to allow maximum customization of the video encoding and recording.
It has built in activities for easy recording and previewing. But it also exposes basic components that can be used to customize your own UI and logic.
All components are designed to be extensible and usable without all the other components. Some of the main components:
Wrapper for camera library that abstracts away some of the complications about checking support for various features.
SurfaceView that scales and crops the view so that it matches the desired resolution.
Recorder that simplifies logic for recording multiple video clips.
Task that combines multiple video files and transforms the video frames into the desired resolution.
Activity that allows the user to record videos in a way similar to instagram and snapchat
Activity to preview the recorded video before selecting the video
There is a demo activity that allows you to try different settings for recording videos.
compile 'com.amosyuen.ffmpegvideorecorder:ffmpeg-video-recorder:2.0.3'
<dependency>
<groupId>com.amosyuen.ffmpegvideorecorder</groupId>
<artifactId>ffmpeg-video-recorder</artifactId>
<version>2.0.3</version>
<type>pom</type>
</dependency>
<dependency org='com.amosyuen.ffmpegvideorecorder' name='ffmpeg-video-recorder' rev='2.0.3'>
<artifact name='$AID' ext='pom'></artifact>
</dependency>
git clone git://github.com/amosyuen/FFmpegVideoRecorder.git
For a working implementation, please have a look at the demo project
For Android M and above, make sure you request the following permissions before starting the activity:
Send an intent to start the activity with the desired parameters. The parameters are required:
private static final int RECORD_VIDEO_REQUEST = 1000;
public void startActivity(File videoFile, File thumbnailFile) {
FFmpegRecorderActivityParams.Builder paramsBuilder =
FFmpegRecorderActivityParams.builder(getContext())
.setVideoOutputFileUri(videoFile)
.setVideoThumbnailOutputFileUri(thumbnailFile);
paramsBuilder.recorderParamsBuilder()
.setVideoSize(new ImageSize(640, 480))
.setVideoCodec(VideoCodec.H264)
.setVideoBitrate(100000)
.setVideoFrameRate(30)
.setVideoImageFit(ImageFit.FILL)
.setVideoImageScale(ImageScale.DOWNSCALE)
.setShouldCropVideo(true)
.setShouldPadVideo(true)
.setVideoCameraFacing(Facing.BACK)
.setAudioCodec(AudioCodec.AAC)
.setAudioSamplingRateHz(44100)
.setAudioBitrate(100000)
.setAudioChannelCount(2)
.setOutputFormat(OutputFormat.MP4);
Intent intent = new Intent(this, FFmpegRecorderActivity.class);
intent.putExtra(FFmpegRecorderActivity.REQUEST_PARAMS_KEY, paramsBuilder.build());
startActivityForResult(intent, RECORD_VIDEO_REQUEST);
}
Override onActivityResult
method and handle request code sent in step 3.
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == RECORD_VIDEO_REQUEST) {
switch (resultCode) {
case RESULT_OK:
Uri videoUri = data.getData();
Uri thumbnailUri =
data.getParcelableExtra(FFmpegRecorderActivity.THUMBNAIL_URI_KEY);
break;
case Activity.RESULT_CANCELED:
break;
case FFmpegRecorderActivity.RESULT_ERROR:
Exception error = (Exception)
data.getSerializableExtra(FFmpegRecorderActivity.ERROR_PATH_KEY);
break;
}
}
}
This error means that the native libraries were not installed properly. One common problem is that Android doesn't support loading both 64-bit and 32-bit native libraries at the same time. Unfortunately the FFmpeg library used in this library only has 32-bit binaries. So if your app includes another dependency that has 64-bit libraries, this will make the app unable to load the video recorder. The only workaround is to exclude 64-bit native libraries. This can be done by adding these lines in your app gradle build file:
android {
//...
packagingOptions {
exclude "lib/arm64-v8a/**"
exclude "lib/x86_64/**"
}
//...
}
This library is based off of these sources:
It uses https://github.com/bytedeco/javacv for combining and encoding videos.