/
CommandLineActivity.java
130 lines (119 loc) · 4.81 KB
/
CommandLineActivity.java
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
package com.lyman.ffmpeg_cmake_single;
import android.content.Context;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.ProgressBar;
import com.lyman.ffmpeg_cmake_single.utils.FFmpegKit;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
public class CommandLineActivity extends AppCompatActivity {
private static final String TAG = "CommandLineActivity";
private ProgressBar mProgressBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_command_line);
mProgressBar = findViewById(R.id.progressBar2);
if (!getMoveFile("input_video.mp4").exists()
|| !getMoveFile("input_audio.acc").exists()) {
new Thread(new Runnable() {
@Override
public void run() {
copyFilesFromRaw(CommandLineActivity.this, R.raw.input_video,
"input_video.mp4",
getExternalFilesDir(Environment.DIRECTORY_MOVIES).getAbsolutePath());
copyFilesFromRaw(CommandLineActivity.this, R.raw.input_audio,
"input_audio.acc",
getExternalFilesDir(Environment.DIRECTORY_MOVIES).getAbsolutePath());
}
}).start();
}
}
public void onClickStartCommand(View view) {
mProgressBar.setVisibility(View.VISIBLE);
new Thread(new Runnable() {
@Override
public void run() {
String base = getMoveFile("input_video.mp4").getParent();
Log.e("PATH", base);
String[] commands = new String[9];
commands[0] = "ffmpeg";
commands[1] = "-i";
commands[2] = base + "/input_video.mp4";
commands[3] = "-i";
commands[4] = base + "/input_audio.acc";
commands[5] = "-strict";
commands[6] = "-2";
commands[7] = "-y";
commands[8] = base + "/merge.mp4";
int result = FFmpegKit.run(commands);
Log.e("RESULT", result + "**********************");
runOnUiThread(new Runnable() {
@Override
public void run() {
mProgressBar.setVisibility(View.GONE);
}
});
}
}).start();
}
private File getMoveFile(String fileName) {
File rootFile = getExternalFilesDir(Environment.DIRECTORY_MOVIES);
// Create the storage directory if it does not exist
if (!rootFile.exists()) {
if (!rootFile.mkdirs()) {
Log.d(TAG, "failed to create directory");
return null;
}
}
String path = rootFile.getAbsolutePath() + File.separator + fileName;
return new File(path);
}
private void copyFilesFromRaw(Context context, int id, String fileName, String storagePath) {
Log.e(TAG, "copyFilesFromRaw: " + fileName + "start");
InputStream inputStream = context.getResources().openRawResource(id);
File file = new File(storagePath);
if (!file.exists()) {//如果文件夹不存在,则创建新的文件夹
file.mkdirs();
}
readInputStream(storagePath + File.separator + fileName, inputStream);
}
/**
* 读取输入流中的数据写入输出流
*
* @param storagePath 目标文件路径
* @param inputStream 输入流
*/
private void readInputStream(String storagePath, InputStream inputStream) {
File file = new File(storagePath);
try {
if (!file.exists()) {
// 1.建立通道对象
FileOutputStream fos = new FileOutputStream(file);
// 2.定义存储空间
byte[] buffer = new byte[inputStream.available()];
// 3.开始读文件
int lenght = 0;
while ((lenght = inputStream.read(buffer)) != -1) {// 循环从输入流读取buffer字节
// 将Buffer中的数据写到outputStream对象中
fos.write(buffer, 0, lenght);
}
fos.flush();// 刷新缓冲区
// 4.关闭流
fos.close();
inputStream.close();
Log.e(TAG, "readInputStream: " + storagePath);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}