簡體   English   中英

Android FFMPEG java concat多個文件

[英]Android FFMPEG java concat multiple files

我正在使用cordova-plugin-video-editor庫的fork:

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)

我不確定為什么會顯示“不是目錄”? 由於文件是在上一步中創建的,因此確定有效嗎?

好的,我有一個使用以下代碼的工作版本:

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));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM