Android - 连接两个视频

标签 android video android-ndk ffmpeg java-native-interface

我正在尝试在 Android 上连接两个视频。我已经在使用 ffmpeg 来满足其他需求,但我正在使用 halfninja's one ,只有 0.9。 0.9 不允许使用以下方法:

// filter_complex isn't recognized
vk.run(new String[] {
        "ffmpeg",
        "-i",
        inputFile1,
        "-i",
        inputFile2,
        "-filter_complex",
        "'[0:1] [0:0] [1:1] [1:0] concat=n=2:v=1:a=1 [v] [a]'",
        "-map",
        "'[v]'",
        "-map",
        "'[a]'",
        outputFile
});

// Or, after converting the two videos to ts, trying to merge them: concat:file1.ts|file2.ts: No such file or directory
vk.run(new String[] {
        "ffmpeg",
        "-i",
        "'concat:" + ts1 + "|" + ts2 + "'",
        "-vcodec",
        "copy",
        "-acodec",
        "copy",
        "-absf",
        "aac_adtstoasc",
        output
});

我尝试的第三件事是使用 concat demuxer 解释 here , ffmpeg 0.9 也无法识别。

有什么方法可以将 Android 上的两个视频与 ffmpeg 0.9(或其他库)连接起来吗?

最佳答案

好吧,找到的唯一解决方案是使用 ffmpeg ≥1.1。我编译了2.1,它工作得很好。这是我现在使用的:

/**
 * Concatenates two videos
 * @param inputFile1 First video file path
 * @param inputFile2 Second video file path
 * @param outputFile Output file path
 */
public static void concatenate(String inputFile1, String inputFile2, String outputFile) {
    Log.d(TAG, "Concatenating " + inputFile1 + " and " + inputFile2 + " to " + outputFile);
    String list = generateList(new String[] {inputFile1, inputFile2});
    Videokit vk = Videokit.getInstance();
    vk.run(new String[] {
            "ffmpeg",
            "-f",
            "concat",
            "-i",
            list,
            "-c",
            "copy",
            outputFile
    });
}

/**
 * Generate an ffmpeg file list
 * @param inputs Input files for ffmpeg
 * @return File path
 */
private static String generateList(String[] inputs) {
    File list;
    Writer writer = null;
    try {
        list = File.createTempFile("ffmpeg-list", ".txt");
        writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(list)));
        for (String input: inputs) {
            writer.write("file '" + input + "'\n");
            Log.d(TAG, "Writing to list file: file '" + input + "'");
        }
    } catch (IOException e) {
        e.printStackTrace();
        return "/";
    } finally {
        try {
            if (writer != null)
                writer.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    Log.d(TAG, "Wrote list file to " + list.getAbsolutePath());
    return list.getAbsolutePath();
}

关于Android - 连接两个视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33996797/

相关文章:

android - 使用自定义 UI 播放 Youtube 视频

android - GridView.setOnItemClickListener 不工作

javascript - ios-deploy 安装失败

php - 直接在浏览器中使用 jquery/flash 从网络摄像头录制视频?

ios - 在 Facebook 视频 IOS 中标记 friend

macos - OS X 的视频播放器 (QTKit, ffmpeg)

android - Dagger 2.10/2.11 注入(inject) Activity 失败

android ndk : are -fPIC and -pie mututally exclusive?

android - 是否可以使用 google cardboard API 来创建原生 Android 应用程序?

android - 如何在Windows XP上挂载Android文件系统