简体   繁体   中英

Concat video files with ffmpeg-android

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
        }
    }

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)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM