我正试图了解更多关于JavaCV android studio中的ffmpeg使用情况,并且对于我正在尝试使用ProcessBuilder的任务。我试着写一个简单的程序来调试pb.start();
,虽然,我没有得到响应。我所做的是启动一个默认/空活动,并粘贴以下程序:
package com.example.myapplication;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import org.bytedeco.javacpp.Loader;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
public class MainActivity extends AppCompatActivity {
static final int cols = 192;
static final int rows = 108;
static final String ffmpeg = Loader.load(org.bytedeco.ffmpeg.ffmpeg.class);
static final String rtmp_url = "test.flv";
static final String[] command = {ffmpeg,
"-y",
"-f", "rawvideo",
"-vcodec", "rawvideo",
"-pix_fmt", "bgr24",
"-s", (Integer.toString(cols) + "x" + Integer.toString(rows)),
"-r", "10",
"-i", "pipe:",
"-c:v", "libx264",
"-pix_fmt", "yuv420p",
"-preset", "ultrafast",
"-f", "flv",
rtmp_url};
@RequiresApi(api = Build.VERSION_CODES.O)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new Thread(t1).start();
}
private static Runnable t1 = () -> {
Log.e("TAG", "void OnCreate called successfully!");
ProcessBuilder pb = new ProcessBuilder(command).redirectErrorStream(true);
pb.redirectErrorStream(true);
try {
Process process = pb.start();
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
OutputStream writer = process.getOutputStream();
Log.e("TAG", "Something good happened here");
} catch (IOException e) {
e.printStackTrace();
Log.e("TAG", "Nothing good happened here");
}
};
}
我目前的问题是,我似乎无法通过pb.start();
启动processBuilder进程我从logcat面板中获得以下日志:
2022-06-14 17:24:46.328 13371-13371/com.example.myapplication E/TAG: void OnCreate called successfully!
2022-06-14 17:24:46.333 13371-13371/com.example.myapplication E/TAG: Nothing good happened here
我想了解为什么它跳过try/catch块而不启动该进程?
编辑:我按照@g00se的建议做了一些更改,我从上面的代码中得到了以下堆栈跟踪:
2022-06-15 00:32:26.700 29787-29787/? E/USNET: USNET: appName: com.example.myapplication
2022-06-15 00:32:29.328 29787-29828/com.example.myapplication E/TAG: void OnCreate called successfully!
2022-06-15 00:32:29.330 29787-29828/com.example.myapplication E/AndroidRuntime: FATAL EXCEPTION: Thread-4
Process: com.example.myapplication, PID: 29787
java.lang.NullPointerException
at java.lang.ProcessBuilder.start(ProcessBuilder.java:1012)
at com.example.myapplication.MainActivity.lambda$static$0(MainActivity.java:48)
at com.example.myapplication.MainActivity$$ExternalSyntheticLambda0.run(Unknown Source:0)
at java.lang.Thread.run(Thread.java:920)
不需要线程,也不需要读取输出来启动进程,在本例中,errCode = process.waitFor();
完成了任务。
package com.example.myapplication;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import org.bytedeco.javacpp.Loader;
import org.bytedeco.opencv.opencv_core.Point;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.Scalar;
import org.opencv.imgproc.Imgproc;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
public class MainActivity extends AppCompatActivity {
static final int cols = 192;
static final int rows = 108;
static final String ffmpeg = Loader.load(org.bytedeco.ffmpeg.ffmpeg.class);
static final String rtmp_url = "test.flv";
static final String[] command = {ffmpeg,
"-y",
"-f", "rawvideo",
"-vcodec", "rawvideo",
"-pix_fmt", "bgr24",
"-s", (Integer.toString(cols) + "x" + Integer.toString(rows)),
"-r", "10",
"-i", "pipe:",
"-c:v", "libx264",
"-pix_fmt", "yuv420p",
"-preset", "ultrafast",
"-f", "flv",
rtmp_url};
@RequiresApi(api = Build.VERSION_CODES.O)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//new Thread(t1).start();
ProcessBuilder pb = new ProcessBuilder("echo", "Hello World!");
pb.redirectErrorStream(true);
System.out.println("Run echo command");
Process process = null;
try {
process = pb.start();
} catch (IOException e) {
e.printStackTrace();
}
int errCode = 0;
try {
errCode = process.waitFor();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Echo command executed, any errors? " + (errCode == 0 ? "No" : "Yes"));
try {
System.out.println("Echo Output:n" + output(process.getInputStream()));
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
OutputStream writer = process.getOutputStream();
} catch (IOException e) {
e.printStackTrace();
}
Log.e("TAG", "void OnCreate called successfully!");
}
private static String output(InputStream inputStream) throws IOException {
StringBuilder sb = new StringBuilder();
BufferedReader br = null;
try {
br = new BufferedReader(new InputStreamReader(inputStream));
String line = null;
while ((line = br.readLine()) != null) {
sb.append(line + System.getProperty("line.separator"));
}
} finally {
br.close();
}
return sb.toString();
}
}