如何使用 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"}

祝你好運..!

相關內容