注:一切的操作都是在windows操作。
1.安装ffmpeg
安装好ffmpeg,并配置环境变量,在命令行窗口输入,ffmpeg -verson
2.配置nginx
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
access_log off;
server {
listen 8080;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
location /hls {
types{
application/vnd.apple.mpegurl m3u8;
video/mp2t ts;
}
root html;
add_header Cache-Control no-cache;
add_header Access-Control-Allow-Origin *;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
3.java调用ffmpeg命令,在输入http://ip:8080/hls/test.m3u8就可以观看
//开启
public void onLine(int pass){
try {
getData("ffmpeg -f rtsp -rtsp_transport tcp -i \"rtsp://admin:123@172.16.10.153:554/cam/realmonitor?channel="+pass+"&subtype=0\" -codec copy -f hls -hls_list_size 20 -hls_time 5 \"E:/nginx-rtmp-http-flv/html/online/test.m3u8\"");
} catch (Exception e) {
System.err.println(e);
e.printStackTrace();
}
}
public static void getData(String stam){
BufferedReader br = null;
try {
File file = new File("E:\\daemonTmp");
File tmpFile = new File("E:\\daemonTmp\\temp.tmp");//新建一个用来存储结果的缓存文件
if (!file.exists()){
file.mkdirs();
}
if(!tmpFile.exists()) {
tmpFile.createNewFile();
}
ProcessBuilder pb = new ProcessBuilder().command("cmd.exe", "/c", stam).inheritIO();
pb.redirectErrorStream(true);//这里是把控制台中的红字变成了黑字,用通常的方法其实获取不到,控制台的结果是pb.start()方法内部输出的。
pb.redirectOutput(tmpFile);//把执行结果输出。
Process process = pb.start();
process.waitFor();//等待语句执行完成,否则可能会读不到结果。
InputStream in = new FileInputStream(tmpFile);
br= new BufferedReader(new InputStreamReader(in));
String line = null;
while((line = br.readLine()) != null) {
System.out.println(line);
}
br.close();
br = null;
tmpFile.delete();//卸磨杀驴。
System.out.println("执行完成");
} catch (Exception e) {
e.printStackTrace();
} finally {
if(br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//关闭
public void close() throws IOException {
ProcessBuilder pb = new ProcessBuilder().command("cmd.exe", "/c", "taskkill /f /t /im ffmpeg.exe");
pb.start();
}