未来社会中,文盲并非不识字的人,而是不能再学习的人。铁人赛就是强迫自己学习的好机会。
大家好今天我要来介绍如何使用FFmpeg来切割影片,他有很多其他的功能但我今天只会来介绍切影片的功能,大家如果有想要尝试其他功能的话可以去网路上查询。废话不多说那我们就开始示范吧!
一开始先在gradle加入:
implementation 'com.arthenica:mobile-ffmpeg-full:4.3.2'
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/mButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="点击我"
android:textSize="35sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
当使用者按下按钮时,app会跳到选取影片的画面,在点选影片後就会开始分割影片了。
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import com.arthenica.mobileffmpeg.Config;
import com.arthenica.mobileffmpeg.FFmpeg;
import com.arthenica.mobileffmpeg.FFprobe;
import java.io.File;
import java.util.ArrayList;
import static com.arthenica.mobileffmpeg.Config.RETURN_CODE_CANCEL;
import static com.arthenica.mobileffmpeg.Config.RETURN_CODE_SUCCESS;
public class MainActivity extends AppCompatActivity {
Button mButton;
private static final int PICK_VIDEO_FROM_GALLERY_REQUEST_CODE = 400;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mButton = findViewById(R.id.mButton);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
pickImageFromGallery();
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_VIDEO_FROM_GALLERY_REQUEST_CODE && resultCode == RESULT_OK) {
} if (Build.VERSION.SDK_INT >= 16 && data.getClipData() == null) {
ArrayList<String> uriList = new ArrayList<String>();
uriList.clear();
String path = FileUtil.getFileAbsolutePath(this, data.getData());
String name = FileUtil.fileName(path);
uriList.add(path);
ArrayList<String> VideoList = new ArrayList<>();
VideoList.add(path);
Log.e("Listsize", VideoList.size() + ""+path);
Log.e("List", VideoList.get(0));
segVideo(path);
}
}
private void pickImageFromGallery() {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("video/*");
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
String[] mimeTypes = {"video/mp4", "video/mov"};
intent.putExtra(Intent.EXTRA_MIME_TYPES, mimeTypes);
startActivityForResult(intent, PICK_VIDEO_FROM_GALLERY_REQUEST_CODE);
}
public void segVideo(String path){
ArrayList<String> segVideoList = new ArrayList<>();
File video = new File(path);
int videoSize = (int) video.length();
int sizeLimit = 50 * 1024 * 1024;
Log.e("size", video.length() + "");
int duration = (int) (FFprobe.getMediaInformation(path).getDuration() / 1000); //影片秒数
int startTime = 0;
int segTime = 0; //分割秒数
int loop = 0;
int sss = videoSize / sizeLimit;
int aaa = videoSize % sizeLimit;
if (videoSize > sizeLimit) { //如果video大小超过设定的sizelimit大小
//计算要分割的秒数
if (aaa != 0) {
segTime = duration / (sss + 1);
}
//回圈次数
if (duration % segTime != 0) {
loop = duration / segTime + 1;
} else {
loop = duration / segTime;
}
//开始分割
for (int i = 0; i < loop; i++) {
int rc = FFmpeg.execute("-ss " + startTime + " -i " + path + " -t " + segTime + " -acodec copy -vcodec copy " + Environment.getExternalStorageDirectory().toString() + "/Seg_Video" + "/" + video.getName().replace(".mp4", "") + "_seg_" + i + ".mp4");
if (rc == RETURN_CODE_SUCCESS) {
segVideoList.add(Environment.getExternalStorageDirectory().toString() + "/Seg_Video" + "/" + video.getName().replace(".mp4", "") + "_seg_" + i + ".mp4");
Log.i(Config.TAG, "Command execution completed successfully.");
} else if (rc == RETURN_CODE_CANCEL) {
Log.i(Config.TAG, "Command execution cancelled by user.");
} else {
Log.i(Config.TAG, String.format("Command execution failed with rc=%d and the output below.", rc));
Config.printLastCommandOutput(Log.INFO);
}
startTime += segTime;
}
} else {
//没超过
segVideoList.add(path);
}
}
}
我来解释一下在MainActivity的的程序码里面最重要的有影片切割功能的是下面的程序码:
int rc = FFmpeg.execute("-ss " + startTime + " -i " + path + " -t " + segTime + " -acodec copy -vcodec copy " + Environment.getExternalStorageDirectory().toString() + "/Seg_Video" + "/" + video.getName().replace(".mp4", "") + "_seg_" + i + ".mp4");
在这段程序码里面
AndroidManifest 里面加入
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
喔对了如果各位的Android系统是10以上的话记得在AndroidManifest的application里面加入:
android:requestLegacyExternalStorage="true"
否则Logcat会报错
那今天的介绍就到这了,谢谢大家的观看。
成绩单上有国,英,数,物理四科 分数分别70、89、72、93 这时候我们就可以用阵列将他们绑再一起...
完赛结语 今天是我们团队首次参加30天铁人赛的完赛日,老套路了,首先要感谢每个对本系列文章订阅与观看...
根据机密性,应将要求国家安全信息的第12356号行政命令分类为“最高机密”,“机密”或“机密”只是对...
这次开发一个之後活动可能会用到的功能叫抽签 只要输入/draw就随机抽一位公会在线上的成员 希望有了...
鬼牌,在此借用的意思是「可以成为任何一张牌」 Dynamic Components 可以当鬼牌 Dy...