import com.sun.jna.Library;
import com.sun.jna.Native;

public interface Kernel32 extends Library {

	@SuppressWarnings("deprecation")
	public static Kernel32 INSTANCE = (Kernel32) Native.loadLibrary("kernel32", Kernel32.class);

	public long GetProcessId(Long hProcess);
	
}


<dependency>
   <groupId>net.java.dev.jna</groupId>
	<artifactId>jna</artifactId>
	<version>5.11.0</version>
</dependency>

<dependency>
	<groupId>cn.hutool</groupId>
	<artifactId>hutool-system</artifactId>
	<version>5.7.22</version>
</dependency>

 

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.Field;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.reformer.video.ffmpeg.FfmpegUtil;

import cn.hutool.core.util.RuntimeUtil;

public class ProcessUtil {

	private static final Logger LOGGER = LoggerFactory.getLogger(ProcessUtil.class);

	public static void main(String[] args) throws IOException, InterruptedException {
		//String order = "ffmpeg -i rtsp://admin:123456@192.168.101.233:554/h264/ch1/main/av_stream -qscale 1 -vcodec h264 -acodec aac -bf 0 -tune zerolatency -g 5 -max_delay 100 -strict -2 -f flv rtmp://192.168.10.73:1935/live/1";
		String in = "rtsp://admin:Dl123456@192.168.10.233:554/h264/ch1/main/av_stream";
		String out = "rtmp://192.168.10.73:1935/live/1";
		Process process = RuntimeUtil.exec(FfmpegUtil.builderOrder(in, out));
		mointor(process);
		Long pid = getProcessPID(process);
		System.out.println(pid);
		Thread.sleep(15000);
		stopProcess(pid);

	}

	/**
	 * 开始进程
	 * 
	 * @param pid
	 */
	public static Long startProcess(String cmd) {
		Process process = RuntimeUtil.exec(cmd);
		mointor(process);// 打印检测
		return getProcessPID(process);
	}

	/**
	 * 获取PID
	 * 
	 * @param child
	 * @return
	 */
	public static long getProcessPID(Process process) {
		long pid = -1;
		Field field = null;
		try {
			field = process.getClass().getDeclaredField("handle");
			field.setAccessible(true);
			pid = Kernel32.INSTANCE.GetProcessId((Long) field.get(process));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return pid;
	}

	/**
	 * 停止进程
	 * 
	 * @param pid
	 */
	public static void stopProcess(long pid) {
		String cmd = "";
		String os = System.getProperty("os.name");
		if (os != null && os.toLowerCase().startsWith("windows")) {// Windows操作系统
			cmd = "taskkill /PID " + pid + " /F";
		}
		if (os != null && os.toLowerCase().startsWith("linux")) {// Linux操作系统
			cmd = "kill -s 9 " + pid;
		}
		LOGGER.info("正在执行命令 ====" + cmd);
		// 执行命令
		if (StringUtils.isNotBlank(cmd)) {
			RuntimeUtil.exec(cmd);
		}
	}

	/**
	 * 检测进程
	 * 
	 * @param process
	 */
	public static void mointor(Process process) {

		// 读取进程标准输出
		new Thread(() -> {
			try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
				String line = null;
				while ((line = bufferedReader.readLine()) != null) {
					LOGGER.info(line);
				}
			} catch (IOException e) {
			}
		}).start();

		// 读取进程异常输出
		new Thread(() -> {
			try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getErrorStream()))) {
				String line = null;
				while ((line = bufferedReader.readLine()) != null) {
					LOGGER.info(line);
				}
			} catch (IOException e) {
			}
		}).start();

	}

}