【Android FFMPEG 开发】Android 中执行 FFMPEG 指令

举报
韩曙亮 发表于 2022/01/10 22:49:15 2022/01/10
【摘要】 文章目录 一、推荐开源项目二、Android 中执行 FFMPEG 指令1、导入依赖2、Java 代码编写3、使用时的代码示例 三、博客资源 一、推荐开源项目 ...





一、推荐开源项目



最近需要在 Android 中进行音视频数据转码 , 音频混音 , 音频编辑边裁 等操作 , 如果能在 Android 系统中执行 FFMPEG 指令 , 基本就可以晚上需求 ;


推荐一个 GitHub 上的项目 : https://github.com/WritingMinds/ffmpeg-android-java


该项目中 FFmpegAndroid 是 Android Library 核心依赖库 , 在自己的项目中 , 引入该依赖库即可进行 FFMPEG 命令执行 ;

app Module 仅仅是一个示例项目 , 展示 FFmpegAndroid 依赖库如何使用 ;

在 FFmpegAndroid 项目中的 ffmpeg-android-java-0.3.2\FFmpegAndroid\assets\armeabi-v7a\ffmpeg 是 FFMPEG 可执行文件 , 可以在 ARM 架构的 Android 系统中执行 ;

ffmpeg-android-java-0.3.2\FFmpegAndroid\assets\x86\ffmpeg 是可以在 x86 架构的 Android 系统中可执行的文件 ;

这个 ffmpeg 可执行文件是该应用的核心 ;

在这里插入图片描述


基于最后一个可运行版本进行调试 ,

这个项目在 2016 2016 2016 年停止维护了 , 运行后一堆报错 , 引用了远古版本的 ButterKnife 和 Dagger 依赖库 , 更新了最新的 com.github.dcendents:android-maven-gradle-plugin 插件 , 然后添加了 google() 库支持 , 项目运行起来了 ;

参考 :

在这里插入图片描述

运行该项目 , 执行

-version

  
 
  • 1

命令 , 打印出该 FFMPEG 的版本 , 3.0.1 的版本 , 有点老 ;

在这里插入图片描述





二、Android 中执行 FFMPEG 指令



参考 http://writingminds.github.io/ffmpeg-android-java/ 博客中的使用介绍 ;



1、导入依赖


直接引用项目 :

repositories {
    flatDir {
        dirs 'libs'
    }
}

dependencies {
    compile(name:'FFmpegAndroid', ext:'aar')
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

添加 Gradle 依赖库 :

compile 'com.writingminds:FFmpegAndroid:0.3.2'

  
 
  • 1

Maven 依赖库 :

<dependency>
  <groupId>com.writingminds</groupId>
  <artifactId>FFmpegAndroid</artifactId>
  <version>0.3.2</version>
</dependency>

  
 
  • 1
  • 2
  • 3
  • 4
  • 5


2、Java 代码编写


首先 , 初始化 FFMPEG 实例 ;

FFmpeg ffmpeg = FFmpeg.getInstance(context);

  
 
  • 1

然后 , 加载 ffmpeg 可执行文件 , 该操作是将可执行文件从 assets 目录中拷贝到 Android 应用的内置存储空间 ;

try {
  ffmpeg.loadBinary(new LoadBinaryResponseHandler() {

    @Override
    public void onStart() {}

    @Override
    public void onFailure() {}

    @Override
    public void onSuccess() {}

    @Override
    public void onFinish() {}
  });
} catch (FFmpegNotSupportedException e) {
  // Handle if FFmpeg is not supported by device
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

最后 , 执行 FFMPEG 命令 ;

try {
  // to execute "ffmpeg -version" command you just need to pass "-version"
  ffmpeg.execute(cmd, new ExecuteBinaryResponseHandler() {

    @Override
    public void onStart() {}

    @Override
    public void onProgress(String message) {}

    @Override
    public void onFailure(String message) {}

    @Override
    public void onSuccess(String message) {}

    @Override
    public void onFinish() {}
  });
} catch (FFmpegCommandAlreadyRunningException e) {
  // Handle if FFmpeg is already running
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22


3、使用时的代码示例


ffmpeg-android-java 项目中 app 的主界面代码 , 有上述 3 3 3 个完整的使用步骤 ;

package com.github.hiteshsondhi88.sampleffmpeg;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.github.hiteshsondhi88.libffmpeg.ExecuteBinaryResponseHandler;
import com.github.hiteshsondhi88.libffmpeg.FFmpeg;
import com.github.hiteshsondhi88.libffmpeg.LoadBinaryResponseHandler;
import com.github.hiteshsondhi88.libffmpeg.exceptions.FFmpegCommandAlreadyRunningException;
import com.github.hiteshsondhi88.libffmpeg.exceptions.FFmpegNotSupportedException;

public class Home extends Activity implements View.OnClickListener {

    private static final String TAG = Home.class.getSimpleName();

    FFmpeg ffmpeg;

    EditText commandEditText;

    LinearLayout outputLayout;

    Button runButton;

    private ProgressDialog progressDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);

        // 1. 获取 FFMPEG 实例
        ffmpeg = FFmpeg.getInstance(this);

        commandEditText = (EditText) findViewById(R.id.command);
        outputLayout = (LinearLayout) findViewById(R.id.command_output);
        runButton = (Button) findViewById(R.id.run_command);

        loadFFMpegBinary();

        initUI();
    }

    private void initUI() {
        runButton.setOnClickListener(this);

        progressDialog = new ProgressDialog(this);
        progressDialog.setTitle(null);
    }

    // 2. 加载 ffmpeg 可执行文件
    private void loadFFMpegBinary() {
        try {
            ffmpeg.loadBinary(new LoadBinaryResponseHandler() {
                @Override
                public void onFailure() {
                    showUnsupportedExceptionDialog();
                }
            });
        } catch (FFmpegNotSupportedException e) {
            showUnsupportedExceptionDialog();
        }
    }

    // 3. 执行命令
    private void execFFmpegBinary(final String[] command) {
        try {
            ffmpeg.execute(command, new ExecuteBinaryResponseHandler() {
                @Override
                public void onFailure(String s) {
                    addTextViewToLayout("FAILED with output : "+s);
                }

                @Override
                public void onSuccess(String s) {
                    addTextViewToLayout("SUCCESS with output : "+s);
                }

                @Override
                public void onProgress(String s) {
                    Log.d(TAG, "Started command : ffmpeg "+command);
                    addTextViewToLayout("progress : "+s);
                    progressDialog.setMessage("Processing\n"+s);
                }

                @Override
                public void onStart() {
                    outputLayout.removeAllViews();

                    Log.d(TAG, "Started command : ffmpeg " + command);
                    progressDialog.setMessage("Processing...");
                    progressDialog.show();
                }

                @Override
                public void onFinish() {
                    Log.d(TAG, "Finished command : ffmpeg "+command);
                    progressDialog.dismiss();
                }
            });
        } catch (FFmpegCommandAlreadyRunningException e) {
            // do nothing for now
        }
    }

    private void addTextViewToLayout(String text) {
        TextView textView = new TextView(Home.this);
        textView.setText(text);
        outputLayout.addView(textView);
    }

    private void showUnsupportedExceptionDialog() {
        new AlertDialog.Builder(Home.this)
                .setIcon(android.R.drawable.ic_dialog_alert)
                .setTitle(getString(R.string.device_not_supported))
                .setMessage(getString(R.string.device_not_supported_message))
                .setCancelable(false)
                .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Home.this.finish();
                    }
                })
                .create()
                .show();

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.run_command:
                String cmd = commandEditText.getText().toString();
                String[] command = cmd.split(" ");
                if (command.length != 0) {
                    execFFmpegBinary(command);
                } else {
                    Toast.makeText(Home.this, getString(R.string.empty_command_toast), Toast.LENGTH_LONG).show();
                }
                break;
        }
    }
}


  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153




三、博客资源



调试通过的源码下载地址 : https://download.csdn.net/download/han1202012/19156661

资源内容 : 源码 , FFMPEG 中文文档 ;

在这里插入图片描述

文章来源: hanshuliang.blog.csdn.net,作者:韩曙亮,版权归原作者所有,如需转载,请联系作者。

原文链接:hanshuliang.blog.csdn.net/article/details/117373640

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。