【问题标题】:FFmpeg executable Chmod permissionFFmpeg 可执行 Chmod 权限
【发布时间】:2013-12-10 15:37:30
【问题描述】:

尝试使用存储在 Raw 文件夹 eclipse 中的可执行文件运行 FFmpeg 命令以用于 android 应用程序。我收到权限被拒绝错误,无法调整视频大小。如何从我的 java 文件中授予正确的权限。

【问题讨论】:

    标签: android permissions ffmpeg command


    【解决方案1】:

    将此代码放在静态类中或任何您想要的地方:

    public static void installBinaryFromRaw(Context context, int resId, File file) {
        final InputStream rawStream = context.getResources().openRawResource(resId);
        final OutputStream binStream = getFileOutputStream(file);
    
        if (rawStream != null && binStream != null) {
            pipeStreams(rawStream, binStream);
    
            try {
                rawStream.close();
                binStream.close();
            } catch (IOException e) {
                Log.e(TAG, "Failed to close streams!", e);
            }       
    
            doChmod(file, 777);
        }       
    }
    public static OutputStream getFileOutputStream(File file) {
        try {
            return new FileOutputStream(file);
        } catch (FileNotFoundException e) {
            Log.e(TAG, "File not found attempting to stream file.", e);
        }
        return null;
    }
    
    public static void pipeStreams(InputStream is, OutputStream os) {
        byte[] buffer = new byte[IO_BUFFER_SIZE];
        int count;
        try {
            while ((count = is.read(buffer)) > 0) {
                os.write(buffer, 0, count);
            }
        } catch (IOException e) {
            Log.e(TAG, "Error writing stream.", e);
        }
    }
    public static void doChmod(File file, int chmodValue) {
        final StringBuilder sb = new StringBuilder();
        sb.append("chmod");
        sb.append(' ');
        sb.append(chmodValue);
        sb.append(' ');
        sb.append(file.getAbsolutePath());
    
        try {
            Runtime.getRuntime().exec(sb.toString());
        } catch (IOException e) {
            Log.e(TAG, "Error performing chmod", e);
        }
    }
    

    并使用以下代码调用它:

    private void installFfmpeg() {
        File ffmpegFile = new File(getCacheDir(), "ffmpeg");
        mFfmpegInstallPath = ffmpegFile.toString();
        Log.d(TAG, "ffmpeg install path: " + mFfmpegInstallPath);
    
        if (!ffmpegFile.exists()) {
            try {
                ffmpegFile.createNewFile();
            } catch (IOException e) {
                Log.e(TAG, "Failed to create new file!", e);
            }
            Utils.installBinaryFromRaw(this, R.raw.ffmpeg, ffmpegFile);
        }else{
            Log.d(TAG, "It was already installed");
        }
    
        ffmpegFile.setExecutable(true);
        Log.d(TAG, String.valueOf(ffmpegFile.canExecute()));
    }
    

    希望有用!!

    【讨论】:

    • 非常感谢。我完全使用了代码,它说 ffmpeg 安装完美。现在这是我运行 FFMpeg 命令所做的 - 但它没有创建任何输出视频。我有一个我的输入 mp4 文件的外部目录路径,我在外部目录上添加了输出 mp4 文件的字符串路径。从我的 oncreate() 我调用 installFFmpeg(); String inputPath = "外部目录路径" String outputPath = "外部目录路径" Process process = Runtime.getRuntime().exec("ffmpeg -i "+ inputPath+ " -s 480x320 "+ outputPath); processComplete = process.waitFor();我做错了什么?
    • 好的,尝试将这样的终端模拟器 (play.google.com/store/apps/…) 下载到您的手机中,然后先尝试那里的命令。您应该访问您的 ffmpeg 可执行文件类型:“cd /data/data/YOUR_PACKAGE/cache”“./ffmpeg -i inputFile -y outputFile”。首先尝试一个非常简单的命令,看看文件是否被复制。文件名不能有空格。试试看,告诉我输出是什么。
    • 我试过了,得到了所有乱码数据。 link
    • 好吧..您需要输入每个文件的扩展名(.mp4 或其他),以及您使用的是哪个可执行文件?你确定它为android正确编译了吗?你想试试我的 ffmpeg 文件吗??
    • 是的...您的 ffmpeg 版本仅适用于 Mac。试试这个:drive.google.com/file/d/0B9xbR4X7h9CidjNqaWgtaFMwdmM/…
    【解决方案2】:

    您的 Android 应用必须具有读取和写入存储空间的权限,位于 AndroidManifest.xml

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />.
    

    另一个需要注意的是路径,/storage/emulated/0/ 并非在所有设备上都可用,您应该使用Environment.getExternalStorageDirectory() 来查找实际路径。

    最后,有一个简单的解决方法,而不是从原始文件夹中提取 ffmpeg,将 ffmpeg 重命名为 lib...ffmpeg...so 并将其放入项目中的目录 libs/armeabi 中。

    当然,你稍后会运行Runtime.getRuntime().exec(getContext().getApplicationInfo().nativeLibraryDir + "/lib...ffmpeg...so" +whatever)

    系统安装程序会自动为您解压可执行文件到/data/data/your.package.full.name/lib

    【讨论】:

    • 谢谢你,Alex - 我今天会试一试,告诉你进展如何。
    猜你喜欢
    • 2011-12-27
    • 1970-01-01
    • 1970-01-01
    • 2011-03-25
    • 2012-07-20
    • 2011-04-15
    • 1970-01-01
    • 1970-01-01
    • 2012-11-24
    相关资源
    最近更新 更多