Android FFMPEG java concat多个文件

时间:2015-07-11 17:01:20

标签: java android cordova video ffmpeg

我正在使用cordova-plugin-video-editor库的分支:

https://github.com/jbavari/cordova-plugin-video-editor/pull/13

这里使用android-ffmpeg-java库:

https://github.com/guardianproject/android-ffmpeg-java

开箱即用的cordova插件示例适用于使用以下代码对单个视频进行编码:

File tempFile = File.createTempFile("ffmpeg", null, appContext.getCacheDir());
FfmpegController ffmpegController = new FfmpegController(appContext, tempFile);

TranscodeCallback tcCallback = new TranscodeCallback();

Clip clipIn = new Clip(videoSrcPath);
Clip clipOut = new Clip(outputFilePath);
clipOut.videoCodec = "libx264";
clipOut.videoFps = "24"; // tailor this to your needs
clipOut.videoBitrate = 512; // 512 kbps - tailor this to your needs
clipOut.audioChannels = 1;
clipOut.width = outputWidth;
clipOut.height = outputHeight;
clipOut.duration = videoDuration;
ffmpegController.processVideo(clipIn, clipOut, true, tcCallback);

这里调用android-ffmpeg-java代码:

https://github.com/guardianproject/android-ffmpeg-java/blob/master/src/org/ffmpeg/android/FfmpegController.java#L225

他们在这里有一个多文件concat测试示例:

https://github.com/guardianproject/android-ffmpeg-java/blob/master/src/org/ffmpeg/android/test/ConcatTest.java#L16

所以我修改了cordova插件代码以匹配示例:

ArrayList<Clip> listVideos = new ArrayList<Clip>();

Clip clip = new Clip();
clip.path = new File(videoSrcPath).getCanonicalPath();
ffmpegController.getInfo(clip);
clip.duration = 5;
listVideos.add(clip);

Clip clip2 = new Clip();
clip2.path = new File(videoSrcPath2).getCanonicalPath();
ffmpegController.getInfo(clip2);
clip2.duration = 5;
listVideos.add(clip2);

Clip clipOut = new Clip();
clipOut.path = new File(outputFilePath).getCanonicalPath();
ffmpegController.concatAndTrimFilesMP4Stream(listVideos, clipOut, false, false, new ShellUtils.ShellCallback() {
    @Override
    public void shellOut(String shellLine) {
        System.out.println("fc>" + shellLine);
    }
    @Override
    public void processComplete(int exitValue) {
        if (exitValue < 0)
            System.err.println("concat non-zero exit: " + exitValue);
    }
});

但是在运行时我收到错误:

23:15:08.498    3218-3293/com.example.hello D/VideoEditor﹕ execute method starting
07-10 23:15:08.498    3218-3293/com.example.hello D/VideoEditor﹕ transcodeVideo firing
07-10 23:15:08.499    3218-3293/com.example.hello D/VideoEditor﹕ options: {"fileUri":"content:\/\/com.android.providers.media.documents\/document\/video%3A23389","fileUri2":"content:\/\/com.android.providers.media.documents\/document\/video%3A23390","outputFileName":"1436584506888","quality":2,"outputFileType":1,"optimizeForNetworkUse":1,"duration":2}
07-10 23:15:08.615    3218-3293/com.example.hello D/VideoEditor﹕ videoSrcPath: /storage/emulated/0/Movies/-a.mp4
07-10 23:15:08.615    3218-3293/com.example.hello D/VideoEditor﹕ videoSrcPath2: /storage/emulated/0/Movies/-b.mp4
07-10 23:15:08.618    3218-3293/com.example.hello V/VideoEditor﹕ outputFilePath: /storage/emulated/0/Movies/HelloWorld/VID_1436584506888.mp4
07-10 23:15:08.618    3218-3293/com.example.hello W/PluginManager﹕ THREAD WARNING: exec() call to VideoEditor.transcodeVideo blocked the main thread for 121ms. Plugin should use CordovaInterface.getThreadPool().
07-10 23:15:09.126    3742-3742/? W/linker﹕ /data/data/com.example.hello/app_bin/ffmpeg has text relocations. This is wasting memory and prevents security hardening. Please fix.
07-10 23:15:09.506    3750-3750/? W/linker﹕ /data/data/com.example.hello/app_bin/ffmpeg has text relocations. This is wasting memory and prevents security hardening. Please fix.
07-10 23:15:09.836    3218-3264/com.example.hello I/System.out﹕ fc>/data/data/com.example.hello/app_bin/ffmpeg -y -t 0 0 : 0 0 : 5.000000 -i /storage/emulated/0/Movies/-a.mp4 -f mpegts -c copy -an -bsf:v h264_mp4toannexb /storage/emulated/0/Android/data/com.example.hello/cache/ffmpeg-246029513.tmp/0.ts
07-10 23:15:09.864    3758-3758/? W/linker﹕ /data/data/com.example.hello/app_bin/ffmpeg has text relocations. This is wasting memory and prevents security hardening. Please fix.
07-10 23:15:09.865    3218-3759/com.example.hello I/System.out﹕ fc>WARNING: linker: /data/data/com.example.hello/app_bin/ffmpeg has text relocations. This is wasting memory and prevents security hardening. Please fix.
07-10 23:15:09.869    3218-3759/com.example.hello I/System.out﹕ fc>ffmpeg version 0.11.1 Copyright (c) 2000-2012 the FFmpeg developers
07-10 23:15:09.870    3218-3759/com.example.hello I/System.out﹕ fc>  built on Dec 22 2014 12:52:34 with gcc 4.6 20120106 (prerelease)
07-10 23:15:09.870    3218-3759/com.example.hello I/System.out﹕ fc>  configuration: --arch=arm --cpu=cortex-a8 --target-os=linux --enable-runtime-cpudetect --prefix=/data/data/info.guardianproject.ffmpeg/app_opt --enable-pic --disable-shared --enable-static --cross-prefix=/home/n8fr8/dev/android/ndk/toolchains/arm-linux-androideabi-4.6/prebuilt/linux-x86_64/bin/arm-linux-androideabi- --sysroot=/home/n8fr8/dev/android/ndk/platforms/android-16/arch-arm --extra-cflags='-I../x264 -mfloat-abi=softfp -mfpu=neon -fPIE -pie' --extra-ldflags='-L../x264 -fPIE -pie' --enable-version3 --enable-gpl --disable-doc --enable-yasm --enable-decoders --enable-encoders --enable-muxers --enable-demuxers --enable-parsers --enable-protocols --enable-filters --enable-avresample --enable-libfreetype --disable-indevs --enable-indev=lavfi --disable-outdevs --enable-hwaccels --enable-ffmpeg --disable-ffplay --disable-ffprobe --disable-ffserver --disable-network --enable-libx264 --enable-zlib
07-10 23:15:09.870    3218-3759/com.example.hello I/System.out﹕ fc>  libavutil      51. 54.100 / 51. 54.100
07-10 23:15:09.870    3218-3759/com.example.hello I/System.out﹕ fc>  libavcodec     54. 23.100 / 54. 23.100
07-10 23:15:09.870    3218-3759/com.example.hello I/System.out﹕ fc>  libavformat    54.  6.100 / 54.  6.100
07-10 23:15:09.870    3218-3759/com.example.hello I/System.out﹕ fc>  libavdevice    54.  0.100 / 54.  0.100
07-10 23:15:09.870    3218-3759/com.example.hello I/System.out﹕ fc>  libavfilter     2. 77.100 /  2. 77.100
07-10 23:15:09.870    3218-3759/com.example.hello I/System.out﹕ fc>  libswscale      2.  1.100 /  2.  1.100
07-10 23:15:09.870    3218-3759/com.example.hello I/System.out﹕ fc>  libswresample   0. 15.100 /  0. 15.100
07-10 23:15:09.870    3218-3759/com.example.hello I/System.out﹕ fc>  libpostproc    52.  0.100 / 52.  0.100
07-10 23:15:09.870    3218-3759/com.example.hello I/System.out﹕ fc>[NULL @ 0xb6421100] Unable to find a suitable output format for '0'
07-10 23:15:09.870    3218-3759/com.example.hello I/System.out﹕ fc>0: Invalid argument
07-10 23:15:09.891    3218-3264/com.example.hello I/System.out﹕ fc>/data/data/com.example.hello/app_bin/ffmpeg -y -t 0 0 : 0 0 : 5.000000 -i /storage/emulated/0/Movies/-b.mp4 -f mpegts -c copy -an -bsf:v h264_mp4toannexb /storage/emulated/0/Android/data/com.example.hello/cache/ffmpeg-246029513.tmp/1.ts
07-10 23:15:09.912    3762-3762/? W/linker﹕ /data/data/com.example.hello/app_bin/ffmpeg has text relocations. This is wasting memory and prevents security hardening. Please fix.
07-10 23:15:09.913    3218-3763/com.example.hello I/System.out﹕ fc>WARNING: linker: /data/data/com.example.hello/app_bin/ffmpeg has text relocations. This is wasting memory and prevents security hardening. Please fix.
07-10 23:15:09.917    3218-3763/com.example.hello I/System.out﹕ fc>ffmpeg version 0.11.1 Copyright (c) 2000-2012 the FFmpeg developers
07-10 23:15:09.917    3218-3763/com.example.hello I/System.out﹕ fc>  built on Dec 22 2014 12:52:34 with gcc 4.6 20120106 (prerelease)
07-10 23:15:09.918    3218-3763/com.example.hello I/System.out﹕ fc>  configuration: --arch=arm --cpu=cortex-a8 --target-os=linux --enable-runtime-cpudetect --prefix=/data/data/info.guardianproject.ffmpeg/app_opt --enable-pic --disable-shared --enable-static --cross-prefix=/home/n8fr8/dev/android/ndk/toolchains/arm-linux-androideabi-4.6/prebuilt/linux-x86_64/bin/arm-linux-androideabi- --sysroot=/home/n8fr8/dev/android/ndk/platforms/android-16/arch-arm --extra-cflags='-I../x264 -mfloat-abi=softfp -mfpu=neon -fPIE -pie' --extra-ldflags='-L../x264 -fPIE -pie' --enable-version3 --enable-gpl --disable-doc --enable-yasm --enable-decoders --enable-encoders --enable-muxers --enable-demuxers --enable-parsers --enable-protocols --enable-filters --enable-avresample --enable-libfreetype --disable-indevs --enable-indev=lavfi --disable-outdevs --enable-hwaccels --enable-ffmpeg --disable-ffplay --disable-ffprobe --disable-ffserver --disable-network --enable-libx264 --enable-zlib
07-10 23:15:09.918    3218-3763/com.example.hello I/System.out﹕ fc>  libavutil      51. 54.100 / 51. 54.100
07-10 23:15:09.918    3218-3763/com.example.hello I/System.out﹕ fc>  libavcodec     54. 23.100 / 54. 23.100
07-10 23:15:09.918    3218-3763/com.example.hello I/System.out﹕ fc>  libavformat    54.  6.100 / 54.  6.100
07-10 23:15:09.918    3218-3763/com.example.hello I/System.out﹕ fc>  libavdevice    54.  0.100 / 54.  0.100
07-10 23:15:09.918    3218-3763/com.example.hello I/System.out﹕ fc>  libavfilter     2. 77.100 /  2. 77.100
07-10 23:15:09.918    3218-3763/com.example.hello I/System.out﹕ fc>  libswscale      2.  1.100 /  2.  1.100
07-10 23:15:09.918    3218-3763/com.example.hello I/System.out﹕ fc>  libswresample   0. 15.100 /  0. 15.100
07-10 23:15:09.918    3218-3763/com.example.hello I/System.out﹕ fc>  libpostproc    52.  0.100 / 52.  0.100
07-10 23:15:09.918    3218-3763/com.example.hello I/System.out﹕ fc>[NULL @ 0xb6321100] Unable to find a suitable output format for '0'
07-10 23:15:09.918    3218-3763/com.example.hello I/System.out﹕ fc>0: Invalid argument
07-10 23:15:09.940    3218-3264/com.example.hello I/System.out﹕ fc>/data/data/com.example.hello/app_bin/ffmpeg -y -i concat:/storage/emulated/0/Android/data/com.example.hello/cache/ffmpeg-246029513.tmp/0.ts|/storage/emulated/0/Android/data/com.example.hello/cache/ffmpeg-246029513.tmp/1.ts -c copy -an /storage/emulated/0/Movies/HelloWorld/VID_1436584506888.mp4
07-10 23:15:09.963    3766-3766/? W/linker﹕ /data/data/com.example.hello/app_bin/ffmpeg has text relocations. This is wasting memory and prevents security hardening. Please fix.
07-10 23:15:09.964    3218-3767/com.example.hello I/System.out﹕ fc>WARNING: linker: /data/data/com.example.hello/app_bin/ffmpeg has text relocations. This is wasting memory and prevents security hardening. Please fix.
07-10 23:15:09.971    3218-3767/com.example.hello I/System.out﹕ fc>ffmpeg version 0.11.1 Copyright (c) 2000-2012 the FFmpeg developers
07-10 23:15:09.972    3218-3767/com.example.hello I/System.out﹕ fc>  built on Dec 22 2014 12:52:34 with gcc 4.6 20120106 (prerelease)
07-10 23:15:09.972    3218-3767/com.example.hello I/System.out﹕ fc>  configuration: --arch=arm --cpu=cortex-a8 --target-os=linux --enable-runtime-cpudetect --prefix=/data/data/info.guardianproject.ffmpeg/app_opt --enable-pic --disable-shared --enable-static --cross-prefix=/home/n8fr8/dev/android/ndk/toolchains/arm-linux-androideabi-4.6/prebuilt/linux-x86_64/bin/arm-linux-androideabi- --sysroot=/home/n8fr8/dev/android/ndk/platforms/android-16/arch-arm --extra-cflags='-I../x264 -mfloat-abi=softfp -mfpu=neon -fPIE -pie' --extra-ldflags='-L../x264 -fPIE -pie' --enable-version3 --enable-gpl --disable-doc --enable-yasm --enable-decoders --enable-encoders --enable-muxers --enable-demuxers --enable-parsers --enable-protocols --enable-filters --enable-avresample --enable-libfreetype --disable-indevs --enable-indev=lavfi --disable-outdevs --enable-hwaccels --enable-ffmpeg --disable-ffplay --disable-ffprobe --disable-ffserver --disable-network --enable-libx264 --enable-zlib
07-10 23:15:09.973    3218-3767/com.example.hello I/System.out﹕ fc>  libavutil      51. 54.100 / 51. 54.100
07-10 23:15:09.973    3218-3767/com.example.hello I/System.out﹕ fc>  libavcodec     54. 23.100 / 54. 23.100
07-10 23:15:09.974    3218-3767/com.example.hello I/System.out﹕ fc>  libavformat    54.  6.100 / 54.  6.100
07-10 23:15:09.974    3218-3767/com.example.hello I/System.out﹕ fc>  libavdevice    54.  0.100 / 54.  0.100
07-10 23:15:09.974    3218-3767/com.example.hello I/System.out﹕ fc>  libavfilter     2. 77.100 /  2. 77.100
07-10 23:15:09.975    3218-3767/com.example.hello I/System.out﹕ fc>  libswscale      2.  1.100 /  2.  1.100
07-10 23:15:09.976    3218-3767/com.example.hello I/System.out﹕ fc>  libswresample   0. 15.100 /  0. 15.100
07-10 23:15:09.976    3218-3767/com.example.hello I/System.out﹕ fc>  libpostproc    52.  0.100 / 52.  0.100
07-10 23:15:09.976    3218-3767/com.example.hello I/System.out﹕ fc>concat:/storage/emulated/0/Android/data/com.example.hello/cache/ffmpeg-246029513.tmp/0.ts|/storage/emulated/0/Android/data/com.example.hello/cache/ffmpeg-246029513.tmp/1.ts: Not a directory
07-10 23:15:09.981    3218-3264/com.example.hello D/VideoEditor﹕ transcode exception
    java.lang.Exception: There was a problem rendering the video: /storage/emulated/0/Movies/HelloWorld/VID_1436584506888.mp4
            at org.ffmpeg.android.FfmpegController.concatAndTrimFilesMP4Stream(FfmpegController.java:1272)
            at org.apache.cordova.videoeditor.VideoEditor$1.run(VideoEditor.java:257)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
            at java.lang.Thread.run(Thread.java:818)

我不确定为什么会说“不是目录”?当然它是有效的,因为文件是在上一步创建的?

1 个答案:

答案 0 :(得分:0)

好的我有一个使用以下代码的工作版本:

VideoEditor.java第10行

import java.util.ArrayList;

VideoEditor.java第22行

import org.ffmpeg.android.ShellUtils;

VideoEditor.java第99行

// intro
final File inFile = this.resolveLocalFileSystemURI(options.getString("fileUri"));
if (!inFile.exists()) {
    Log.d(TAG, "input file does not exist");
    callback.error("input video does not exist.");
    return;
}

// recording
final File inFile2 = this.resolveLocalFileSystemURI(options.getString("fileUri2"));
if (!inFile2.exists()) {
    Log.d(TAG, "input file2 does not exist");
    callback.error("input video2 does not exist.");
    return;
}

// outro
final File inFile3 = this.resolveLocalFileSystemURI(options.getString("fileUri3"));
if (!inFile3.exists()) {
    Log.d(TAG, "input file3 does not exist");
    callback.error("input video3 does not exist.");
    return;
}

// overlay
final File inFile4 = this.resolveLocalFileSystemURI(options.getString("fileUri4"));
if (!inFile4.exists()) {
    Log.d(TAG, "input file4 does not exist");
    callback.error("input video4 does not exist.");
    return;
}

final String videoSrcPath = inFile.getAbsolutePath();
final String videoSrcPath2 = inFile2.getAbsolutePath();
final String videoSrcPath3 = inFile3.getAbsolutePath();
final String videoSrcPath4 = inFile4.getAbsolutePath();

VideoEditor.java第250行

FfmpegController ffmpegController = new FfmpegController(appContext, appContext.getCacheDir());
TranscodeCallback tcCallback = new TranscodeCallback();

ArrayList<Clip> listVideos = new ArrayList<Clip>();

Clip clip = new Clip();
clip.path = new File(videoSrcPath).getCanonicalPath();
ffmpegController.getInfo(clip);
clip.duration = 5;
listVideos.add(clip);

Clip clip2 = new Clip();
clip2.path = new File(videoSrcPath2).getCanonicalPath();
ffmpegController.getInfo(clip2);
clip2.duration = 5;
listVideos.add(clip2);

Clip clip3 = new Clip();
clip3.path = new File(videoSrcPath3).getCanonicalPath();
ffmpegController.getInfo(clip3);
clip3.duration = 5;
listVideos.add(clip3);

Clip clipOut = new Clip();
clipOut.path = new File(outputFilePath).getCanonicalPath();

ffmpegController.concatAndTrimFilesMP4Stream(listVideos, clipOut, false, false, new ShellUtils.ShellCallback() {
    @Override
    public void shellOut(String shellLine) {
        System.out.println("fc>" + shellLine);
    }
    @Override
    public void processComplete(int exitValue) {
        if (exitValue < 0)
            System.err.println("concat non-zero exit: " + exitValue);
    }
});

FfmpegController.java第664行

/*
double dValue = mediaIn.duration;
int hours = (int)(dValue/3600f);
dValue -= (hours*3600);

cmd.add("0");
cmd.add(String.format(Locale.US,"%s",hours));
cmd.add(":");

int min = (int)(dValue/60f);
dValue -= (min*60);

cmd.add("0");
cmd.add(String.format(Locale.US,"%s",min));
cmd.add(":");

cmd.add(String.format(Locale.US,"%f",dValue));
*/
cmd.add(Double.toString(mediaIn.duration));