FFmpeg Video Editor

FFmpeg is a tool that can be used to edit or convert videos and audios.It includes libavcodec – the audio/video codec library.
Some of the uses of FFmpeg are:

  • Video Compress
  • Audio Compress
  • Video Cut
  • Video Rotate
  • Video Crop
  • Extract Picture from Video
  • Extract Sound from Video
  • Change Video Resolution
  • Adding filters to videos
  • Creating fast and slow motion video
  • Reverse Video
  • Creating video from images
  • Convert video from one format into another
  • Merge an audio and video

For integrating FFmpeg in android we can use precompiled libraries like ffmpeg-android.
ffmpeg-android is a library by WritingMinds that simplifies your task of using ffmpeg in Android project.

Below tutorial covers integration of following features using FFmpeg-

  • Video Compress
  • Video Cut
  • Extract Picture from Video
  • Extract Audio from Video
  • Add Fade In Fade Out effect to Video
  • Creating Fast Motion Video
  • Creating Slow Motion Video
  • Reverse Video

I have used RangeSeekBar to allow user to select the time range for cutting video from original video.

Complete code for tutorial is uploaded on github at below link-

https://github.com/bhuvnesh123/FFmpeg-Video-Editor-Android

and the demo app is published on play store-

https://play.google.com/store/apps/details?id=videoeditor.bhuvnesh.com.ffmpegvideoeditor

Integrating FFmpeg library

1.Add FFmpeg dependency in app module gradle file and sync project. 

compile ‘com.writingminds:FFmpegAndroid:0.3.2’

2.Load FFMpeg using below 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);
}
}

loadBinary copies the ffmpeg binary to device according to device’s architecture. You just need to put this once in your code, whenever you are starting the application or using FFmpeg for the first time.

3.Create a method which will take ffmpeg command as an argument and pass it to execute 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) {

}
}

execute() method of FFmpeg class takes command as parameter and executes it.It has a ResponseHandler which return event on start,progress,finish,success and failure.

Now we will go through the various FFmpeg commands that we can use to perform different operations-

Compress a video

To compress a video we can use below command-

String[] command = {"-y", "-i", inputFileAbsolutePath, "-s", "160x120", "-r", "25", "-vcodec", "mpeg4", "-b:v", "150k", "-b:a", "48000", "-ac", "2", "-ar", "22050", outputFileAbsolutePath};

Here,

-y

Overwrite output files without asking.

-i

ffmpeg reads from an arbitrary number of input “files” specified by the -i option

-s

video output size

-r

Set frame rate

-vcodec

Set the video codec.

-b:v

Set the video bitrate

-b:a

Set the audio bitrate
-ac

Set the number of audio channels.

-ar

sets the sampling rate for audio streams if encoded

Cut a video

To cut a video we can use 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};

Here,

-ss
seeks to position(time from where we want to start cutting video)
-t
limit the duration of data read from the input file(duration from cutting start position time upto which we want to cut video)

Other options already defined in previous command

Extract images from video

To extract images from a video we can use below command

String[] complexCommand = {"-y", "-i", inputFileAbsolutePath, "-an", "-r", "1/2", "-ss", "" + startMs / 1000, "-t", "" + (endMs - startMs) / 1000, outputFileAbsolutePath};

Here,

-an
Disable audio recording.

Other options already defined in previous commands

Extract audio from video

To extract audio from a video we can use below command

String[] complexCommand = {"-y", "-i", inputFileAbsolutePath, "-vn", "-ar", "44100", "-ac", "2", "-b:a", "256k", "-f", "mp3", outputFileAbsolutePath};

-vn

Disable video recording

-f

format

Other options already defined in previous commands

Add Fade In Fade Out effect at start and end of video

To add Fade In and Fade Out effect to start and end of video respectively we can use 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

Set the audio codec

vf filtergraph (output)

Create the filtergraph specified by filtergraph and use it to filter the stream.

fade=t=in:st=0:d=5

Fade in first 5 seconds of video
fade=t=out:st=”+String.valueOf(duration-5)+”:d=5″

Fade out last 5 seconds of video

Other options already defined in previous commands

Fast Motion Video

To create fast motion video we can use below command-

String[] complexCommand = {"-y", "-i", inputFileAbsolutePath, "-filter_complex", "[0:v]setpts=0.5*PTS[v];[0:a]atempo=2.0[a]", "-map", "[v]", "-map", "[a]", "-b:v", "2097k", "-r", "60", "-vcodec", "mpeg4", outputFileAbsolutePath};

-filter_complex filtergraph (global)

Define a complex filtergraph, i.e. one with arbitrary number of inputs and/or outputs.

[0:v]setpts=0.5*PTS[v];[0:a]atempo=2.0[a]

The filter works by changing the presentation timestamp (PTS) of each video frame. For example, if there are two succesive frames shown at timestamps 1 and 2, and you want to speed up the video, those timestamps need to become 0.5 and 1, respectively. Thus, we have to multiply them by 0.5.

You can speed up or slow down audio with the atemto audio filter.The atempo filter is limited to using values between 0.5 and 2.0 (so it can slow it down to no less than half the original speed, and speed up to no more than double the input).To speed up the audio to double of its speed we have to use atempo value 2.0 .

-map

Output link labels are referred to with -map. Unlabeled outputs are added to the first output file.

Other options already defined in previous commands

Slow Motion Video

To create a slow motion video we can use the below command-

String[] complexCommand = {"-y", "-i", inputFileAbsolutePath, "-filter_complex", "[0:v]setpts=2.0*PTS[v];[0:a]atempo=0.5[a]", "-map", "[v]", "-map", "[a]", "-b:v", "2097k", "-r", "60", "-vcodec", "mpeg4", outputFileAbsolutePath};

The filter works by changing the presentation timestamp (PTS) of each video frame.To slow down your video, you have to use a multiplier greater than 1. For example, if there are two succesive frames shown at timestamps 1 and 2, and you want to slow down the video, those timestamps need to become 2 and 4, respectively.Thus, we have to multiply them by 2.0.

You can speed up or slow down audio with the atemto audio filter.The atempo filter is limited to using values between 0.5 and 2.0 (so it can slow it down to no less than half the original speed, and speed up to no more than double the input).To slow down the audio to half of its speed we have to use atempo value 0.5 .

Other options already defined in previous commands.

Reverse Video

For reversing video,first we need to divide video into segments with duration of 10 seconds or less because reverse video command for ffmpeg will not work for long duration videos unless your device has 32 GB of RAM.

Hence,to reverse a video-

1.Divide video into segments with duration of 10 seconds or less.
2.Reverse the segmented videos
3.Concatenate reversed segmented videos in reverse order.

For dividing video into segments with duration of 6 seconds we can use the below command-

String[] complexCommand = {"-i", inputFileAbsolutePath, "-c:v", "libx264", "-crf", "22", "-map", "0", "-segment_time", "6", "-g", "9", "-sc_threshold", "0", "-force_key_frames", "expr:gte(t,n_forced*6)", "-f", "segment", outputFileAbsolutePath};

-c:v libx264

encodes all video streams with libx264

-crf

Set the quality for constant quality mode.

-segment_time

time for each segment of video

-g

GOP size

-sc_threshold

set scene change threshold.

-force_key_frames expr:gte(t,n_forced*n)

Forcing a keyframe every n seconds

 

After segmenting video,we need to reverse the segmented videos.For that we need to run a loop where each segmented video file will be reversed.

To reverse a video with audio(without removing its audio) we can use the below command-

String command[] = {"-i", inputFileAbsolutePath, "-vf", "reverse", "-af", "areverse", outputFileAbsolutePath};

To reverse a video with audio removing its audio we can use the below command-

String command[] = {"-i", inputFileAbsolutePath, "-an", "-vf", "reverse", outputFileAbsolutePath};

To reverse a video without audio we can use the below command-

String command[] = {"-i",inputFileAbsolutePath, "-vf", "reverse", outputFileAbsolutePath};

 

After reversing segmented videos,we need to concatenate reversed segmented videos in reverse order.For that we sort videos on the basis of last modified file using Arrays.sort(files, LastModifiedFileComparator.LASTMODIFIED_REVERSE).

Then, to concatenate  reversed segmented videos(with audio) we can use the below command-

String command[] = {"-i",inputFile1AbsolutePath,"-i",inputFile2AbsolutePath .....,"-i",inputFileNAbsolutePath,"-filter_complex","[0:v0] [0:a0] [1:v1] [1:a1]...[N:vN] concat=n=N:v=1:a=1 [v] [a],"-map","[v]","-map","[a]", outputFileAbsolutePath};

To concatenate  reversed segmented videos(without audio) we can use the below command-

String command[] = {"-i",inputFile1AbsolutePath,"-i",inputFile2AbsolutePath .....,"-i",inputFileNAbsolutePath,"-filter_complex","[0:0] [1:0] [2:0]...[N:0] concat=n=N:v=1:a=0",outputFileAbsolutePath};

 

-filter_complex [0:v0] [0:a0] [1:v1] [1:a1]…[N:vN] tells ffmpeg what streams to send to the concat filter.In the above case, video stream 0 [0:v0] and audio stream 0 [0:a0] from input 0,video stream 1 [1:v1] and audio stream 1 [1:v1] from input 1 and so on.

concat filter is used to concatenate audio and video streams, joining them together one after the other.The filter accepts the following options:

n

Set the number of segments. Default is 2.
v

Set the number of output video streams, that is also the number of video streams in each segment. Default is 1.
a

Set the number of output audio streams, that is also the number of audio streams in each segment. Default is 0.

 

 

Please feel free to post any queries,doubts or suggestions in comments section or contact me and I will get back to you as soon as possible!

If you like my post please rate it as it will mean a lot to me!

Advertisements

26 thoughts on “FFmpeg Video Editor

    • Hi Omi,

      For merging audio and video where input video file contains audio ,you can use below command-

      String command[] = {"-i",videoFileAbsolutePath,"-i",audioFileAbsolutePath, "-c:v", "copy", "-c:a", "aac","-map", "0:v:0", "-map", "1:a:0","-shortest", destinationAbsolutePath};

      For merging audio and video where input video file does not contain any audio stream ,you can use below command-

      String command[] = {"-i",videoFileAbsolutePath,"-i",audioFileAbsolutePath, "-c:v", "copy", "-c:a", "aac","-shortest", destinationAbsolutePath};

      Like

      • how can we merge video and audio , when audio is shorter , put it in a loop, and change volume of both audio and video file

        Like

      • 1.Get the duration of audio file and video file using MediaMetadataRetriever.
        2.When audio is shorter,calculate how many times we need to concatenate audio so that its duration is equal or more than video.For example if audio is 30 seconds and video is of 2 minutes we need to concatenate audio 4 times.
        3.Run concatenate audio ffmpeg command.
        4.Merge the concatenated audio with video.

        Like

      • I am actually looking for a complex command to both merge and loop

        I found this
        String[] complexCommand = {“-i”,videopath,”-i”,audioPath,”-filter_complex”,”[0:a]aformat=sample_fmts=fltp:sample_rates=44100:channel_layouts=stereo,volume=0.5[a1];[1:a]aformat=sample_fmts=fltp:sample_rates=44100:channel_layouts=stereo,volume=0.8[a2];[a1][a2]amerge,pan=stereo:c0<c0+c2:c1<c1+c3[out]","-map","1:v","-map","[out]","-c:v","copy","-c:a","-vcodec","-shortest",finalPath};

        but it gives error Unknown encoder '-vcodec'

        And for loop i looked at this
        https://video.stackexchange.com/a/16933

        can we have a single command for this

        Like

    • You can use below command to create a slow motion video removing audio from original video-

      String[] complexCommand = {"-y", "-i", inputFileAbsolutePath,"-an", "-filter:v", "setpts=2.0*PTS", "-b:v", "2097k", "-r", "60", "-vcodec", "mpeg4", outputFileAbsolutePath}

      Here -an removes audio

      Like

  1. Hi, How can i combine two commands to run one after the other before getting the output file. I have a working code for cutting a video and a separate code for decreasing speed, but i want them to run together so that when the command is invoked, a selected section of the video is cut and then its speed reduced before saving the final output.

    Also is there a code to convert video to gif format using ffmpeg?

    Thanks for your response in advance.

    Like

  2. Hello!
    What is the best way to compress the video? What parameters should I use?
    I tried to do it your way and I can’t open the file when I search it in my phone. I get Cannot play video…..Unsupported file type

    Code:
    String[] command = {“-y”, “-i”, filePath, “-s”, “640×480”, “-r”, “25”, “-vcodec”,
    “mpeg4”, “-b:v”, “150k”, “-b:a”, “48000”, “-ac”, “2”, “-ar”, “22050”, compressedFilePath};
    executeFFmpegBinary(command);

    Like

      • Yeah i figured it out…!! also used image overlay.. everything work so great… but my app size increased from 5mb to 23mb.. 🙂

        Like

    • You cant do anything about that.. i searched about that… But you can add in string this “-preset”, “ultrafast”
      this will make conversion a lot faster!!

      Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s