Search code examples
javaffmpegandroid-ffmpeg

Android Java (ffmpeg-kit). Assistance(opinion) with combining 4 ffmpeg commands together


I have the following 4 ffmpeg commands. 1. adds a png frame(border) over the entire length of the video. 2. creates a boomerang effect. 3. adds an outro jpeg to the last 2 seconds of the video. 4. Adds an intro jpeg to the first 2 seconds of the video.(these commands work individually)

My aim is to combine all of these individual commands into one command to create a complete edited video. The final video needs all these elements in one final edited video.

Your assistance is greatly appreciated.

/**
 *

 * (Middle overlay filter) String exe = "-i " + input_video_uri + " -framerate 60 -i " + frame + " -filter_complex [0]pad="+mVideoWidth+":"+mVideoHeight+":576:0[vid];[vid][1]overlay -c:a copy -vcodec mpeg4 -crf 0 -preset ultrafast -qscale 0 " + file2.getAbsolutePath();

 * (Boomerang effect) String exe = "-y -i " + input_video_uri + " -filter_complex [0]reverse[r];[0][r][0]concat=n=3,setpts=0.5*PTS " + file2.getAbsolutePath();

 * (Put image at end of video) String exe = "-i "+ input_video_uri +" -i "+ frame +" -filter_complex \"[0:v][1:v] overlay=0:0:enable='between(t,"+ (msec - 2 ) + ","+ msec+")'\" -pix_fmt yuv420p -c:a copy " + file2.getAbsolutePath();

 * (Put image at start of video) String exe = "-i "+ input_video_uri +" -i "+ frame +" -filter_complex \"[0:v][1:v] overlay=0:0:enable='between(t,0,2)'\" -pix_fmt yuv420p -c:a copy " + file2.getAbsolutePath();

 * */

Being new to ffmpeg, I am limited in knowledge. However, I have tried '&&' which produced an unrecognized error from the ffmpeg library.


Solution

  • The main problem in your case is to create the proper filtergraph. I'll assist you with this task. To adapt these commands for java application is on your own.

    Assumed you have four input sources:

    • 0 - source video;
    • 1 - middle frame image;
    • 2 - title frame image;
    • 3 - end frame image.

    You can apply all your transformations in one filter_complex (for testing purposes I chose final video resolution 640x480 and source video duration 5s):

    -filter_complex \
      "[0:v]pad=640:480:576:0[vpad]; \
       [vpad][1]overlay[vframed]; \
       [vframed]split=3[vfr1][vfr2][vfr3]; \
       [vfr1]reverse[vrev]; \
       [vfr2][vrev][vfr3]concat=n=3,setpts=0.5*PTS[vboom];
       [vboom][2]overlay=enable='lte(t,2)'[vpreout];
       [vpreout][3]overlay=enable='gte(t,5*3*0.5-2)'[vout]" \
    

    Since you applied fast motion effect to "boomerang", the last line has some arithmetic to calculate duration of output video: <5s of source video> * <3 boomerang's chunks> * <speed coefficient>.

    The result will be like this: