2

I am using ffmpeg-android to concat two video files,bur it takes too long to concat and I guess it because of using "-filter_complex" but that was the only command I found on internet,can please someone simplify the below command? I just want to concat two video files captured by camera2 api without any modifications.

 String command[] = new String[]{
                        "-y",
                        "-i", firstPath,
                        "-i", secondPath,
                        "-strict",
                        "experimental",
                        "-filter_complex",
                        "[0:v]scale=iw*min(1920/iw\\,1080/ih):ih*min(1920/iw\\,1080/ih)," +
                                "pad=1920:1080:(1920-iw*min(1920/iw\\,1080/ih))/2:(1080-ih*min(1920/iw\\,1080/ih))/2,setsar=1:1[v0];[1:v] scale=iw*min(1920/iw\\,1080/ih):ih*min(1920/iw\\,1080/ih)," +
                                "pad=1920:1080:(1920-iw*min(1920/iw\\,1080/ih))/2:(1080-ih*min(1920/iw\\,1080/ih))/2,setsar=1:1[v1];[v0][0:a][v1][1:a] concat=n=2:v=1:a=1",
                        "-ab", "48000", "-ac", "2", "-ar", "22050", "-s", "1920x1080", "-vcodec", "libx264", "-crf", "27", "-q", "4", "-preset", "ultrafast", getVideoFilePath(getActivity())};
                commandFFMPEG(command);


 private void commandFFMPEG(String command[]) {
        FFmpeg ffmpeg = FFmpeg.getInstance(getActivity());
        try {
            ffmpeg.execute(command, new ExecuteBinaryResponseHandler() {

                @Override
                public void onStart() {
                    startTime = System.currentTimeMillis();
                }

                @Override
                public void onProgress(String message) {

                }

                @Override
                public void onFailure(String message) {

                }

                @Override
                public void onSuccess(String message) {
                    long endTime = System.currentTimeMillis();
                    long result = endTime - startTime;
                    Toast.makeText(getActivity(), "Videos are merged", Toast.LENGTH_SHORT).show();
                }

                @Override
                public void onFinish() {

                }
            });
        } catch (FFmpegCommandAlreadyRunningException e) {
            // Handle if FFmpeg is already running
        }
    }
Katy Colins
  • 533
  • 2
  • 8
  • 24

2 Answers2

1

Idk will it help you, but when I wanted to concat videos with ffmpeg, I used this command:

StringBuilder builder = new StringBuilder();
builder.append("-f ");
builder.append("concat ");
builder.append("-safe 0 ");
builder.append("-i ");
final String tempFile = getTextFile().getAbsolutePath();//it is text file with video files paths
builder.append(tempFile);
builder.append(" ");
builder.append("-c ");
builder.append("copy ");
builder.append(saveFile);// saveFile - it is your output file

Anyway, concatenation videos with total duration 20 sec took ~20 sec for processing. You can take a look here, I wrote it for concatenation several videos, but code quality is terrible, I don't know, will you understand something there. Hope it will help you)

H.Taras
  • 753
  • 4
  • 15
  • Thx for the answer,but I am getting `[concat @ 0xf4627000] Impossible to open '/storage/emulated/0/Android/data/com.example.user.camerademo/files/null' /storage/emulated/0/Android/data/com.example.user.camerademo/files/inputFiles.txt: No such file or directory` in onFailure – Katy Colins Jul 27 '18 at 12:41
  • Did you see how I did getTextFile() in git repo and did the same ? Or try the same thing, as you did before, without any text file, but with -i firstFile -i secondFile – H.Taras Jul 27 '18 at 12:43
  • Yes I copy the same method and gave my filepaths array,e.g. /storage/emulated/0/Android/data/com.example.user.camerademo/files/1532695230219.mp4 – Katy Colins Jul 27 '18 at 12:47
  • Then try your first approach with 2 -i, I did it in text file, because I could have different number of videos – H.Taras Jul 27 '18 at 12:50
  • Okay can you please tell me what should I append after ` "-i", secondPath,` – Katy Colins Jul 27 '18 at 12:56
  • -i pathToYourVideoFile (orAbsolutePath don't remember) - it will say ffmpeg to get this file as input. You used it `"-i", firstPath, "-i", secondPath,` in your question – H.Taras Jul 27 '18 at 13:01
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/176877/discussion-between-katy-colins-and-h-taras). – Katy Colins Jul 27 '18 at 13:02
0

String cmd[] = new String[]{ "-y","-i", "vid.mp4", "i", "vid2.mp4", "-preset", "ultrafast", "-filter_complex", "[0:v] [0:a] [1:v] [1:a] concat=n=2:v=1:a=1 [v] [a]","-map","[v]","-map","[a]","/output.mp4"};

  • This worked for me !
  • used media format : mp4
  • both videos has same size - 320x240 (required)

Used ffmpeg lib : com.writingminds:FFmpegAndroid:0.3.2

DILSHAD AHMAD
  • 215
  • 2
  • 4