java - 使用 FFMPEG 连接两个视频文件时出现问题

标签 java android video ffmpeg media

我正在尝试连接从图库中获取的两个 mp4 文件。我遇到流程执行失败的问题。我已经添加了代码和错误日志。使用来自 Guardian 项目的 ffmpeg 库。

我在 Samsung Galaxy S3 设备上运行此程序。

错误是在该特定行中引发的。

ProcessBuilder pb = new ProcessBuilder(cmds);
pb.directory(fileExec);
Process process = pb.start();  

当我用此替换上面的最后一行时,

Process process = Runtime.getRuntime().exec("chmod 777 "+cmds.toArray(new String[cmds.size()]));

它的工作原理没有异常,如下所示。但输出似乎没有出现。

文件串联代码:

    File fileVideoOutput = new File(getApplicationContext()
            .getExternalFilesDir("test") + "hello.mp4");
    fileVideoOutput.delete();

    File fileTmp = getApplicationContext().getCacheDir();
    File fileAppRoot = new File(getApplicationContext()
            .getApplicationInfo().dataDir);

    try {
        FfmpegController fc = new FfmpegController(fileTmp, fileAppRoot);

        ArrayList<Clip> listVideos = new ArrayList<Clip>();
        Clip clip = new Clip();
        clip.path = video1;
        fc.getInfo(clip);
        clip.duration = clip.duration;
        System.out.println("Clip1 duration " + clip.duration);
        listVideos.add(clip);

        Clip clip2 = new Clip();
        clip2.path = video2;
        fc.getInfo(clip2);
        clip2.duration = clip2.duration;
        System.out.println("Clip2 duration " + clip2.duration);
        listVideos.add(clip2);

        Clip clipOut = new Clip();
        clipOut.path = fileVideoOutput.getCanonicalPath();

        fc.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);
                    }
                });
    } catch (Exception e1) {
        e1.printStackTrace();
    }

错误日志:

05-08 11:17:03.765: W/System.err(25209): java.io.IOException: Error running exec(). Command: [ffmpeg, -y, -i, /storage/emulated/0/DCIM/Camera/20140507_155713.mp4, -f, mpegts, -c, copy, -an, -bsf:v, h264_mp4toannexb, /data/data/com.yoyo.videoeditor/cache/0.ts] Working Directory: /data/data/com.yoyo.videoeditor/lib Environment: [VIBE_PIPE_PATH=/dev/pipes, ANDROID_ROOT=/system, EMULATED_STORAGE_SOURCE=/mnt/shell/emulated, LOOP_MOUNTPOINT=/mnt/obb, EMULATED_STORAGE_TARGET=/storage/emulated, ANDROID_BOOTLOGO=1, LD_LIBRARY_PATH=/vendor/lib:/system/lib, EXTERNAL_STORAGE=/storage/emulated/legacy, ANDROID_SOCKET_zygote=9, ANDROID_DATA=/data, PATH=/sbin:/vendor/bin:/system/sbin:/system/bin:/system/xbin, ANDROID_ASSETS=/system/app, ASEC_MOUNTPOINT=/mnt/asec, BOOTCLASSPATH=/system/framework/core.jar:/system/framework/core-junit.jar:/system/framework/bouncycastle.jar:/system/framework/ext.jar:/system/framework/framework.jar:/system/framework/framework2.jar:/system/framework/telephony-common.jar:/system/framework/voip-common.jar:/system/framework/mms-common.jar:/system/framework/android.policy.jar:/system/framework/services.jar:/system/framework/apache-xml.jar:/system/framework/sec_edm.jar:/system/framework/seccamera.jar:/system/framework/scrollpause.jar:/system/framework/stayrotation.jar:/system/framework/smartfaceservice.jar:/system/framework/sc.jar:/system/framework/secocsp.jar:/system/framework/commonimsinterface.jar, ANDROID_PROPERTY_WORKSPACE=8,66560, SECONDARY_STORAGE=/storage/extSdCard:/storage/UsbDriveA:/storage/UsbDriveB:/storage/UsbDriveC:/storage/UsbDriveD:/storage/UsbDriveE:/storage/UsbDriveF, ANDROID_STORAGE=/storage]
05-08 11:17:03.770: W/System.err(25209):    at java.lang.ProcessManager.exec(ProcessManager.java:211)
05-08 11:17:03.770: W/System.err(25209):    at java.lang.ProcessBuilder.start(ProcessBuilder.java:195)
05-08 11:17:03.770: W/System.err(25209):    at org.ffmpeg.android.FfmpegController.execProcess(FfmpegController.java:101)
05-08 11:17:03.770: W/System.err(25209):    at org.ffmpeg.android.FfmpegController.execFFMPEG(FfmpegController.java:71)
05-08 11:17:03.770: W/System.err(25209):    at org.ffmpeg.android.FfmpegController.execFFMPEG(FfmpegController.java:75)
05-08 11:17:03.775: W/System.err(25209):    at org.ffmpeg.android.FfmpegController.convertToMP4Stream(FfmpegController.java:657)
05-08 11:17:03.775: W/System.err(25209):    at org.ffmpeg.android.FfmpegController.concatAndTrimFilesMP4Stream(FfmpegController.java:1107)
05-08 11:17:03.775: W/System.err(25209):    at com.yoyo.videoeditor.EditorActivity.mergeVideosOld(EditorActivity.java:271)
05-08 11:17:03.775: W/System.err(25209):    at com.yoyo.videoeditor.EditorActivity.access$0(EditorActivity.java:243)
05-08 11:17:03.775: W/System.err(25209):    at com.yoyo.videoeditor.EditorActivity$3.onClick(EditorActivity.java:85)
05-08 11:17:03.775: W/System.err(25209):    at android.view.View.performClick(View.java:4475)
05-08 11:17:03.775: W/System.err(25209):    at android.view.View$PerformClick.run(View.java:18786)
05-08 11:17:03.780: W/System.err(25209):    at android.os.Handler.handleCallback(Handler.java:730)
05-08 11:17:03.780: W/System.err(25209):    at android.os.Handler.dispatchMessage(Handler.java:92)
05-08 11:17:03.780: W/System.err(25209):    at android.os.Looper.loop(Looper.java:176)
05-08 11:17:03.780: W/System.err(25209):    at android.app.ActivityThread.main(ActivityThread.java:5419)
05-08 11:17:03.780: W/System.err(25209):    at java.lang.reflect.Method.invokeNative(Native Method)
05-08 11:17:03.785: W/System.err(25209):    at java.lang.reflect.Method.invoke(Method.java:525)
05-08 11:17:03.785: W/System.err(25209):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
05-08 11:17:03.785: W/System.err(25209):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
05-08 11:17:03.785: W/System.err(25209):    at dalvik.system.NativeStart.main(Native Method)
05-08 11:17:03.785: W/System.err(25209): Caused by: java.io.IOException: Permission denied
05-08 11:17:03.785: W/System.err(25209):    at java.lang.ProcessManager.exec(Native Method)
05-08 11:17:03.790: W/System.err(25209):    at java.lang.ProcessManager.exec(ProcessManager.java:209)
05-08 11:17:03.790: W/System.err(25209):    ... 20 more

这是 ffmpeg 教程页面中给出的我的 ffmpeg 命令。

ffmpeg -i input1.mp4 -c copy -bsf:v h264_mp4toannexb -f mpegts intermediate1.ts
ffmpeg -i input2.mp4 -c copy -bsf:v h264_mp4toannexb -f mpegts intermediate2.ts
ffmpeg -i "concat:intermediate1.ts|intermediate2.ts" -c copy -bsf:a aac_adtstoasc output.mp4

FFMPEG 库似乎没有按预期工作。当我添加命令并使用“touch”创建输出文件时,正在创建 shell 命令文件。我仍然无法按预期看到 ffmpeg 的输出。

最佳答案

我看到您有一个java.io.IOException:权限被拒绝,您是否有权在应用程序 list 中读取/写入文件? 您可以显示您的 AndroidManifest.xml 文件吗?

最好是在应用程序的临时目录中执行所有操作,然后将它们复制到外部 SD 卡。

为了复制文件,您必须使用自定义类,这是我的:

package com.five_doors.xposedtranslate.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.channels.FileChannel;

/**
 * A small util file class to handle things Java 6 Should have handled himself
 * @author Hugo
 *
 */
public class FileUtil {
    public static void copyFile(File sourceFile, File destFile) throws IOException {
        if(!destFile.exists()) {
            destFile.createNewFile();
        }

        FileChannel source = null;
        FileChannel destination = null;
        try {
            source = new FileInputStream(sourceFile).getChannel();
            destination = new FileOutputStream(destFile).getChannel();

            // previous code: destination.transferFrom(source, 0, source.size());
            // to avoid infinite loops, should be:
            long count = 0;
            long size = source.size();              
            while((count += destination.transferFrom(source, count, size-count))<size);
        }
        finally {
            if(source != null) {
                source.close();
            }
            if(destination != null) {
                destination.close();
            }
        }
    }

    public static void deleteRecursive(File fileOrDirectory) {
        if (fileOrDirectory.isDirectory())
            for (File child : fileOrDirectory.listFiles())
                deleteRecursive(child);

        fileOrDirectory.delete();
    }

    public static String convertStreamToString(InputStream is) throws Exception {
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
          sb.append(line).append("\n");
        }
        reader.close();
        return sb.toString();
    }

    public static String getStringFromFile (String filePath) throws Exception {
        File fl = new File(filePath);
        FileInputStream fin = new FileInputStream(fl);
        String ret = convertStreamToString(fin);
        //Make sure you close all streams.
        fin.close();        
        return ret;
    }
}

关于java - 使用 FFMPEG 连接两个视频文件时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23533794/

相关文章:

java - isEmpty() 在幕后如何工作

android - android中的层应用

java - Runnable 的时间确定性执行

java - 屏幕锁定后相机预览卡住

android - 为什么 android InputMethodManager.showSoftInput() 返回 false?

android - Play 商店说我的手机不支持我自己的应用程序?

javascript - 从文件中单独检索 HTML5 视频持续时间

ios - 从 iPhone 和 iPad 捕获实时屏幕视频,包括音频并发布到 YouTube、Facebook 等

video - 自定义 DirectShow 源过滤器 - 动态分辨率更改

java - 异步网络服务 SOAP