我如何使用 Android ffmpeg 库中的代码

我如何使用 Android ffmpeg 库中的代码

我的代码是:

private void startTransformVideo() {

    String cmd = new String("-y -i {filepath}{filenameRaw}.mp4 -vf crop={width}:{height}:0:0 -acodec copy -threads 5 {filepath}{filenameCropped}.mp4");

    int relWidth = ActivityCamera.videoSize.height;
    int relHeight = relWidth;
    cmd = cmd.replace("{width}", String.valueOf(relHeight));
    cmd = cmd.replace("{height}", String.valueOf(relWidth));
    cmd = cmd.replace("{filepath}", filePath);   //sdcard/
    cmd = cmd.replace("{filenameRaw1}", fileNameNormal);   //fileNameExample
    cmd = cmd.replace("{filenameCropped}", fileNameCropped);   //fileName after Crop

    executeFFmpegCmd(cmd);
    mTransformStatus = 1;
}

private void executeFFmpegCmd(String cmd) {
    try {
        ffmpeg.execute(cmd, new ExecuteBinaryResponseHandler() {

            @Override
            public void onStart() {
                System.out.println("onStart");
            }

            @Override
            public void onProgress(String message) {
                System.out.println("onProgress : " + message);
            }

            @Override
            public void onFailure(String message) {
                System.out.println("onFailure : " + message);
            }

            @Override
            public void onSuccess(String message) {
                System.out.println("onSuccess : " + message);
            }

            @Override
            public void onFinish() {
                System.out.println("onFinish");
            }
        });
    } catch (FFmpegCommandAlreadyRunningException e) {
        e.printStackTrace();
    }
}

此代码接收并裁剪视频文件

并做得很好

现在我的问题是:

这段代码如何:

String cmd = new String("-y -i {filepath}{filenameRaw}.mp4 -vf crop={width}:{height}:0:0 -acodec copy -threads 5 {filepath}{filenameCropped}.mp4");

替换为:

ffmpeg -i input0 -i input1 -i input2 -i input3 -filter_complex \
"[0:v][1:v]hstack[top]; \
[2:v][3:v]hstack[bottom]; \
[top][bottom]vstack" \
output

我尝试了以下方法:

String cmd = new String("-i {filepath}{filenameRaw}.mp4 -i {filepath}{filenameRaw}.mp4 -i {filepath}{filenameRaw}.mp4 -i {filepath}{filenameRaw}.mp4 -filter_complex [0:v][1:v]hstack[top]; [2:v][3:v]hstack[bottom]; [top][bottom]vstack {filenameCropped}.mp4");

这段代码没有回答,也不知道谁应该是分号

没有分号:

String cmd = new String("-i {filepath}{filenameRaw}.mp4 -i {filepath}{filenameRaw}.mp4 -i {filepath}{filenameRaw}.mp4 -i {filepath}{filenameRaw}.mp4 -filter_complex [0:v][1:v]hstack[top] [2:v][3:v]hstack[bottom] [top][bottom]vstack {filenameCropped}.mp4");

而且我也尝试了很多方法,都没有用。

编辑

代码@Ouroborus:

String cmd = new String("-i {filepath}{filenameRaw}.mp4 -i {filepath}{filenameRaw}.mp4 -i {filepath}{filenameRaw}.mp4 -i {filepath}{filenameRaw}.mp4 -filter_complex \"[0:v][1:v]hstack[top]; [2:v][3:v]hstack[bottom]; [top][bottom]vstack\" {filenameCropped}.mp4");

错误:

On FailureWARNING: linker: /data/data/com.parksangha.videorecorder/files/ffmpeg has text relocations. This is wasting memory and prevents security hardening. Please fix.
                                                               ffmpeg version n2.4.2 Copyright (c) 2000-2014 the FFmpeg developers
                                                                 built on Oct  7 2014 15:08:46 with gcc 4.8 (GCC)
                                                                 configuration: --target-os=linux --cross-prefix=/home/sb/Source-Code/ffmpeg-android/toolchain-android/bin/arm-linux-androideabi- --arch=arm --cpu=cortex-a8 --enable-runtime-cpudetect --sysroot=/home/sb/Source-Code/ffmpeg-android/toolchain-android/sysroot --enable-pic --enable-libx264 --enable-libass --enable-libfreetype --enable-libfribidi --enable-fontconfig --enable-pthreads --disable-debug --disable-ffserver --enable-version3 --enable-hardcoded-tables --disable-ffplay --disable-ffprobe --enable-gpl --enable-yasm --disable-doc --disable-shared --enable-static --pkg-config=/home/sb/Source-Code/ffmpeg-android/ffmpeg-pkg-config --prefix=/home/sb/Source-Code/ffmpeg-android/build/armeabi-v7a-neon --extra-cflags='-I/home/sb/Source-Code/ffmpeg-android/toolchain-android/include -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=2 -fno-strict-overflow -fstack-protector-all -mfpu=neon' --extra-ldflags='-L/home/sb/Source-Code/ffmpeg-android/toolchain-android/lib -Wl,-z,relro -Wl,-z,now -pie' --extra-libs='-lpng -lexpat -lm' --extra-cxxflags=
                                                                 libavutil      54.  7.100 / 54.  7.100
                                                                 libavcodec     56.  1.100 / 56.  1.100
                                                                 libavformat    56.  4.101 / 56.  4.101
                                                                 libavdevice    56.  0.100 / 56.  0.100
                                                                 libavfilter     5.  1.100 /  5.  1.100
                                                                 libswscale      3.  0.100 /  3.  0.100
                                                                 libswresample   1.  1.100 /  1.  1.100
                                                                 libpostproc    53.  0.100 / 53.  0.100
                                                               Input #0, mov,mp4,m4a,3gp,3g2,mj2, from '/sdcard/testffmpeg/1465937355161.mp4':
                                                                 Metadata:
                                                                   major_brand     : mp42
                                                                   minor_version   : 0
                                                                   compatible_brands: isommp42
                                                                   creation_time   : 2016-06-14 20:49:17
                                                                 Duration: 00:00:00.96, start: 0.000000, bitrate: 2710 kb/s
                                                                   Stream #0:0(eng): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 1920x1080, 2706 kb/s, 30.02 fps, 30 tbr, 90k tbn, 180k tbc (default)
                                                                   Metadata:
                                                                     rotate          : 90
                                                                     creation_time   : 2016-06-14 20:49:17
                                                                     handler_name    : VideoHandle
                                                                   Side data:
                                                                     displaymatrix: rotation of -90.00 degrees
                                                                   Stream #0:1(eng): Audio: aac (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 150 kb/s (default)
                                                                   Metadata:
                                                                     creation_time   : 2016-06-14 20:49:17
                                                                     handler_name    : SoundHandle
                                                               Input #1, mov,mp4,m4a,3gp,3g2,mj2, from '/sdcard/testffmpeg/1465937359014.mp4':
                                                                 Metadata:
                                                                   major_brand     : mp42
                                                                   minor_version   : 0
                                                                   compatible_brands: isommp42
                                                                   creation_time   : 2016-06-14 20:49:21
                                                                 Duration: 00:00:01.11, start: 0.000000, bitrate: 2463 kb/s
                                                                   Stream #1:0(eng): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 1920x1080, 2615 kb/s, 29.83 fps, 30 tbr, 90k tbn, 180k tbc (default)
                                                                   Metadata:
                                                                     rotate          : 90
                                                                     creation_time   : 2016-06-14 20:49:21
                                                                     handler_name    : VideoHandle
                                                                   Side data:
                                                                     displaymatrix: rotation of -90.00 degrees
                                                                   Stream #1:1(eng): Audio: aac (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 159 kb/s (default)
                                                                   Metadata:
                                                                     creation_time   : 2016-06-14 20:49:21
                                                                     handler_name    : SoundHandle
                                                               Input #2, mov,mp4,m4a,3gp,3g2,mj2, from '/sdcard/testffmpeg/1465937355161.mp4':
                                                                 Metadata:
                                                                   major_brand     : mp42
                                                                   minor_version   : 0
                                                                   compatible_brands: isommp42
                                                                   creation_time   : 2016-06-14 20:49:17
                                                                 Duration: 00:00:00.96, start: 0.000000, bitrate: 2710 kb/s
                                                                   Stream #2:0(eng): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 1920x1080, 2706 kb/s, 30.02 fps, 30 tbr, 90k tbn, 180k tbc (default)
                                                                   Metadata:
                                                                     rotate          : 90
                                                                     creation_time   : 2016-06-14 20:49:17
                                                                     handler_name    : VideoHandle
                                                                   Side data:
                                                                     displaymatrix: rotation of -90.00 degrees
                                                                   Stream #2:1(eng): Audio: aac (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 150 kb/s (default)
                                                                   Metadata:
                                                                     creation_time   : 2016-06-14 20:49:17
                                                                     handler_name    : SoundHandle
                                                               Input #3, mov,mp4,m4a,3gp,3g2,mj2, fro

答案1

在您的代码中,您没有引用复杂过滤器,尽管它在原始命令中被引用。它应该是这样的:

String cmd = new String("-i {filepath}{filenameRaw}.mp4 -i {filepath}{filenameRaw}.mp4 -i {filepath}{filenameRaw}.mp4 -i {filepath}{filenameRaw}.mp4 -filter_complex \"[0:v][1:v]hstack[top]; [2:v][3:v]hstack[bottom]; [top][bottom]vstack\" {filenameCropped}.mp4");

请注意转义的引号。

答案2

如果您想在 Android 应用中运行命令,请按如下方式转换您的代码:

Command line:
ffmpeg -i input0 -i input1 -i input2 -i input3 -filter_complex \
"[0:v][1:v]hstack[top]; \
[2:v][3:v]hstack[bottom]; \
[top][bottom]vstack" \
output

Android: 

String command[] = {"-i","input0","-i","input1","-i","input2","-i","input3","-filter_complex","[0:v][1:v]hstack[top];[2:v][3:v]hstack[bottom];[top][bottom]vstack","output"}

祝你好运..!

相关内容