FFMPEG for android
Posted By : Piyush Choudhary | 23-Jun-2017
This tutorial teaches you about how to use integrate and use ffmpeg library in your android applicaation which is helpful tool that can be used to edit or convert videos and audios.It includes libavcodec – the audio/video codec library.
There are many uses of ffmpeg given below :-
- Audio Compression
- Video Compression
- Video Rotation
- Video Crop
- Video Cut
- Extract sound from video
- Extarct picture from the video
- Reverse the video
- Change video resolution
- Adding filters to the video
- Create video from images
- Create fast or slow motion videos
- Merging an audio and video
- Converting a video from one format to another .
1) To integrate ffmpeg in your android appliation you can use precompiled libraries like ffmpeg-android (credit - Writing-Minds) , which is easy to integrate by adding FFmpeg dependency in app module gradle file and sync project.:-
compile ‘com.writingminds:FFmpegAndroid:0.3.2’
2) To load ffmpeg use the following code :-
FFmpeg ffmpeg;
private void loadFFMpegBinary() {
try {
if (ffmpeg == null) {
Log.d(TAG, “ffmpeg : null”);
ffmpeg = FFmpeg.getInstance(this);
}
ffmpeg.loadBinary(new LoadBinaryResponseHandler() {
@Override public void onFailure() {
showUnsupportedExceptionDialog();
}
@Override public void onSuccess() {
Log.d(TAG, “ffmpeg : correct Loaded”);
}
});
} catch (FFmpegNotSupportedException e) {
showUnsupportedExceptionDialog();
} catch (Exception e) {
Log.d(TAG, “EXception not supported : ” + e); }
}
Just put this once in your code, whenever you are starting your application or you are using FFmpeg for the first time to load the binary copies of ffmpeg binary to device according to device’s architecture.
3 ) Create a method like this which will take the ffmpeg command as an argument and pass it to execute the method of FFMpeg class :-
private void execFFmpegBinary(final String[] command) {
try {
ffmpeg.execute(command, new ExecuteBinaryResponseHandler() {
@Override public void onFailure(String s) {
Log.d(TAG, “FAILED with output : ” + s);
}
@Override public void onSuccess(String s) {
Log.d(TAG, “SUCCESS with output : ” + s); //Perform action on success
}
}
@Override public void onProgress(String s) {
Log.d(TAG, “progress : ” + s);
}
@Override public void onStart() {
Log.d(TAG, “Started command : ffmpeg ” + command);
}
@Override public void onFinish() {
Log.d(TAG, “Finished command : ffmpeg ” + command);
}
});
} catch (FFmpegCommandAlreadyRunningException e) { }
}
By calling this method the execute method of ffmpeg class the command as a paramter and starts its task . It has is response handler called ExecuteBinaryResponseHandler()
which will return events on start,progress,finish,success and failure.
To do the tasks according to your requirement you can use any of below ffmpeg commands :-
- Compress a video
If you want to compress a video you can use the below ffmef command
String[] command = {"-y", "-i", inputFileAbsolutePath, "-s", "160x120", "-r", "25", "-vcodec", "mpeg4", "-b:v", "150k", "-b:a", "48000", "-ac", "2", "-ar", "22050", outputFileAbsolutePath};
the arguments used here are
-y
to overwrite output files without asking .
-i
reads from input files specified by option -i
-s
video output size(video resolution)
-r
set frame rate
-vcodec
sets the codec of the video
-b:v
sets the video bitrate
-b:a
sets the audio bitrate
-ac
sets the number of audio channels
-ar
sets the sampling rate of the audio streams if encoded .
- Cut a Video
to cut a video you can use the below command
String[] complexCommand = {"-ss", "" + startMs / 1000, "-y", "-i", inputFileAbsolutePath, "-t", "" + (endMs - startMs) / 1000, "-s", "320x240", "-r", "15", "-vcodec", "mpeg4", "-b:v", "2097152", "-b:a", "48000", "-ac", "2", "-ar", "22050", outputFileAbsolutePath};
-ss
it seeks to position from where you want to to cut the video .
-t
limit the duration of data read from input files
Rest options are aready defined in above command examples .
Adding Fade in and Fade out effect at start and end of the video
To add the fade in and fade out effect during start and end of the video file , use the below command
String[] complexCommand = {"-y", "-i", inputFileAbsolutePath, "-acodec", "copy", "-vf", "fade=t=in:st=0:d=5,fade=t=out:st=" + String.valueOf(duration - 5) + ":d=5", outputFileAbsolutePath};
-acodec
To set the audio codec
–vf filtergraph (output)
For creating the filtergraph specified by filtergraph and it is used to filter the stream.
fade=t=in:st=0:d=10
Fade in first 10 seconds of video
fade=t=out:st=”+String.valueOf(duration-10)+”:d=10″
Fade out last 10 seconds of video
Rest options are aready defined in above command examples .
Extract audio from video
For extracting the audio from a video file we can use the below command
String[] complexCommand = {"-y", "-i", inputFileAbsolutePath, "-vn", "-ar", "44100", "-ac", "2", "-b:a", "256k", "-f", "mp3", outputFileAbsolutePath};
-vn
To disable the video recording
-f
format
Rest options are aready defined in above command examples .
Extracting images from the video
To extract the images from a video file we can use the command below
String[] complexCommand = {"-y", "-i", inputFileAbsolutePath, "-an", "-r", "1/2", "-ss", "" + startMs / 1000, "-t", "" + (endMs - startMs) / 1000, outputFileAbsolutePath};
Options used here ,
-an
To disable the audio recording.
Rest options are aready defined in above command examples .
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
Piyush Choudhary
Piyush is a bright mobile app developer, he has expertise in building Native Android Applications and Core Java.His hobbies are reading tehnical blogs, playing snooker and console gaming.