我使用 FFmpeg 将我的网络摄像头和语音传输到另一台计算机。我遇到了同样的问题,所以我使用了一个线程来执行 FFmpeg,并使用了一个执行 kill cmd 的类来停止 FFmpeg。
PS:文件已从给定 URL 中删除,因此我编辑此帖子以放置我的代码
package com.pfe.config;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
public class FFmpeg {
private String ip="";
private int port=0;
private String videoDevice="";
private String audioDevice="";
private List<String>devices=null;
public FFmpeg() {
}
public FFmpeg(String ip, int port) {
super();
this.ip = ip;
this.port = port;
}
public FFmpeg(String ip, int port, String videoDevice, String audioDevice) {
this.ip = ip;
this.port = port;
this.videoDevice = videoDevice;
this.audioDevice = audioDevice;
}
public void executeFFmpeg() throws Exception {
String commande = "ffmpeg -f dshow -i video=^\""+videoDevice+"^\":audio=^\""+audioDevice+"^\" -vcodec libx264 -preset ultrafast -tune zerolatency -r 30 -async 1 -acodec libmp3lame -ab 24k -ar 22050 -bsf:v h264_mp4toannexb -maxrate 750k -bufsize 3000k -f mpegts udp://"+ip+":"+port;
System.out.println(commande);
ProcessBuilder builder = new ProcessBuilder("cmd.exe", "/c", commande.toString());
builder.redirectErrorStream(true);
Process p = builder.start();
BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
while (true) {
line = r.readLine();
if (line == null) {
break;
}
System.out.println(line);
}
}
public void executeFFmpeg(String ip, int port, String videoDevice, String audioDevice) throws Exception {
String commande = "ffmpeg -f dshow -i video=^\""+videoDevice+"^\":audio=^\""+audioDevice+"^\" -vcodec libx264 -preset ultrafast -tune zerolatency -r 30 -async 1 -acodec libmp3lame -ab 24k -ar 22050 -bsf:v h264_mp4toannexb -maxrate 750k -bufsize 3000k -f mpegts udp://"+ip+":"+port;
System.out.println(commande);
ProcessBuilder builder = new ProcessBuilder("cmd.exe", "/c", commande.toString());
builder.redirectErrorStream(true);
Process p = builder.start();
BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
while (true) {
line = r.readLine();
if (line == null) {
break;
}
System.out.println(line);
}
}
public List<String> listDiveces() throws IOException{
String commande = "ffmpeg -list_devices true -f dshow -i dummy";
ProcessBuilder builder = new ProcessBuilder("cmd.exe", "/c", commande.toString());
builder.redirectErrorStream(true);
Process p = builder.start();
BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
List<String> ds = new ArrayList<String>();
List<String> ds1 = new ArrayList<String>();
while (true) {
line = r.readLine();
if (line == null) {
break;
}
if(line.contains("dshow")){
ds.add(line);
}
}
if(!ds.isEmpty()){
StringTokenizer st = new StringTokenizer("");
for(int i=0;i<ds.size();i++){
st = new StringTokenizer(ds.get(i) ,"]");
while (st.hasMoreTokens()){
ds1.add(st.nextToken());
}
}
ds.clear();
for(int i=0;i<ds1.size();i++){
if(!ds1.get(i).contains("[")&&ds1.get(i).contains("\"")){
ds.add(ds1.get(i));
}
}
for(int i=0;i<ds.size();i++){
System.out.println(ds.get(i));
ds.set( i, ds.get(i).replaceAll(" ", ""));
ds.set(i,ds.get(i).replaceAll("\"", ""));
System.out.println(ds.get(i));
}
}
return ds;
}
public void executeFFplay(String ip,int port) throws IOException {
String commande = "ffplay udp://"+ip+":"+port;
ProcessBuilder builder = new ProcessBuilder(
"cmd.exe", "/c", commande.toString());
builder.redirectErrorStream(true);
Process p = builder.start();
BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
while (true) {
line = r.readLine();
if (line == null) { break; }
System.out.println(line);
}
}
public void executeFFplay() throws IOException {
String commande = "ffplay udp://"+ip+":"+port;
ProcessBuilder builder = new ProcessBuilder(
"cmd.exe", "/c", commande.toString());
builder.redirectErrorStream(true);
Process p = builder.start();
BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
while (true) {
line = r.readLine();
if (line == null) { break; }
System.out.println(line);
}
}
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
public String getVideoDevice() {
return videoDevice;
}
public void setVideoDevice(String videoDevice) {
this.videoDevice = videoDevice;
}
public String getAudioDevice() {
return audioDevice;
}
public void setAudioDevice(String audioDevice) {
this.audioDevice = audioDevice;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
public List<String> getDevices() {
return devices;
}
public void setDevices(List<String> devices) {
this.devices = devices;
} }
*************************************************************
package com.pfe.config;
public class FFmpegThread implements Runnable {
private FFmpeg fFmpeg;
private String ip;
private int port;
private String videoDevice;
private String audioDevice;
public FFmpegThread(FFmpeg fFmpeg) {
this.fFmpeg = fFmpeg;
}
public FFmpegThread( String ip, int port, String videoDevice, String audioDevice) {
this.ip = ip;
this.port = port;
this.videoDevice = videoDevice;
this.audioDevice = audioDevice;
this.fFmpeg = new FFmpeg(ip, port, videoDevice, audioDevice);
}
@Override
public void run() {
try {
fFmpeg.executeFFmpeg();
}
catch (Exception e) {
e.printStackTrace();
}
}
public FFmpeg getfFmpeg() {
return fFmpeg;
}
public void setfFmpeg(FFmpeg fFmpeg) {
this.fFmpeg = fFmpeg;
}
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
public String getVideoDevice() {
return videoDevice;
}
public void setVideoDevice(String videoDevice) {
this.videoDevice = videoDevice;
}
public String getAudioDevice() {
return audioDevice;
}
public void setAudioDevice(String audioDevice) {
this.audioDevice = audioDevice;
}}
package com.pfe.config;
import java.io.IOException;
public class FFPlayThread implements Runnable {
private String ip;
private int port;
private FFmpeg fFmpeg;
public FFPlayThread(FFmpeg fFmpeg) {
this.fFmpeg = fFmpeg;
}
public FFPlayThread(String ip, int port) {
this.ip = ip;
this.port = port;
this.fFmpeg = new FFmpeg(ip, port);
}
@Override
public void run() {
try {
fFmpeg.executeFFplay();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
public FFmpeg getfFmpeg() {
return fFmpeg;
}
public void setfFmpeg(FFmpeg fFmpeg) {
this.fFmpeg = fFmpeg;
}}
package com.pfe.config;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Kill {
private static final String TASKLIST = "tasklist";
private static final String KILL = "taskkill /F /IM ";
public static boolean isProcessRunging(String serviceName) throws Exception {
Process p = Runtime.getRuntime().exec(TASKLIST);
BufferedReader reader = new BufferedReader(new InputStreamReader(
p.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
//System.out.println(line);
if (line.contains(serviceName)) {
return true;
}
}
return false;
}
public static void killProcess(String serviceName) throws Exception {
Runtime.getRuntime().exec(KILL + serviceName);
}}
import java.util.List;
import com.pfe.config.FFPlayThread;
import com.pfe.config.FFmpeg;
import com.pfe.config.FFmpegThread;
//import com.pfe.config.Kill;
import com.pfe.config.Kill;
public class test {
public static void main(String[] args) throws Exception {
String ip="127.0.0.1";
int port=12000;
FFmpeg fFmpeg = new FFmpeg();
List <String> ds = fFmpeg.listDiveces();
if(!ds.isEmpty()){
Thread thread = new Thread(new FFmpegThread(ip, port, ds.get(0), ds.get(1)));
thread.start();
}
Thread thread = new Thread(new FFPlayThread(ip, port));
thread.start();
/*int i=0;
while(i<10000){
i++;
System.out.println(i);
}
if(Kill.isProcessRunging("ffmpeg.exe"))
Kill.killProcess("ffmpeg.exe");
if(Kill.isProcessRunging("ffplay.exe"))
Kill.killProcess("ffplay.exe");*/
}}