Android - 连接两个视频 [英] Android - concatenate two videos

查看:27
本文介绍了Android - 连接两个视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 Android 上连接两个视频.我已经将 ffmpeg 用于其他需求,但我正在使用 halfninja 的一个,它只有 0.9.0.9 版本不允许使用以下方法:

I'm trying to concatenate two videos on Android. I'm already using ffmpeg for other needs, but I'm using halfninja's one, which is only 0.9. The 0.9 one doesn't allow the following ways to do it:

// 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 解释 此处,ffmpeg 0.9 也无法识别.

The third thing I tried is to use the concat demuxer explained here, which isn't recognized with ffmpeg 0.9 either.

有什么办法可以在 Android 上使用 ffmpeg 0.9(或其他库)连接两个视频?

Is there any way to concatenate two videos on Android with ffmpeg 0.9 (or another library)?

推荐答案

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

Well, the only solution found was to use ffmpeg ≥1.1. I compiled the 2.1, it's working just fine. Here's what I use now:

/**
 * 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 + "'
");
            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 - 连接两个视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆