package com.github.bluesbruce.spring.service; import com.github.bluesbruce.ffch.CommandManager; import com.github.bluesbruce.ffch.CommandManagerImpl; import com.github.bluesbruce.ffch.commandbuidler.CommandBuidlerFactory; import com.github.bluesbruce.ffch.data.CommandTasker; import com.github.bluesbruce.ffch.handler.OutHandler; import com.github.bluesbruce.spring.config.CmdParam; import lombok.extern.slf4j.Slf4j; import org.h2.util.StringUtils; import org.springframework.boot.ApplicationArguments; import org.springframework.boot.ApplicationRunner; import org.springframework.core.annotation.Order; import org.springframework.stereotype.Component; import java.util.Collection; import java.util.Iterator; import static com.github.bluesbruce.ffch.util.PropertiesUtil.load; @Slf4j @Component @Order(value=2) public class FFrtmpServer implements ApplicationRunner { public static final CmdParam cmdParam=load("/loadCmd.properties", CmdParam.class); CommandManager manager = new CommandManagerImpl(); int status=0; @Override public void run(ApplicationArguments args) { try { // -rtsp_transport tcp //测试多个任何同时执行和停止情况 //false表示使用配置文件中的ffmpeg路径,true表示本条命令已经包含ffmpeg所在的完整路径 //manager.start("tomcat", "ffmpeg -i http://192.168.10.101:18000/flv/live/34020000001110000002_34020000001320000071_0200000071.flv -vcodec copy -acodec copy -f flv -y rtmp://192.168.10.101:19350/rlive/stream_9?sign=f8a15b6n",false); //manager.start("tomcat", "ffmpeg -i rtsp://192.168.144.25:554/stream=0 -vcodec copy -acodec copy -f flv -y rtmp://221.226.114.142:19350/rlive/stream_9?sign=f8a15b6n",false); //manager.start("tomcat1", "ffmpeg -i rtsp://192.168.144.25:554/stream=0 -vcodec copy -acodec copy -f flv -y rtmp://221.226.114.142:19350/rlive/stream_11?sign=rHtBg3sz",false); /*if (cmdParam.getType()==1) { manager.start("test0", CommandBuidlerFactory.createBuidler() .add("ffmpeg") .add("-rtsp_transport","tcp").add("-i", cmdParam.getPlayUrl()) .add("-vcodec","copy") .add("-acodec","copy") .add("-f","flv") .add("-b:v", "2M") .add("-maxrate", "2M") .add("-bufsize", "4M") .add("-y").add(cmdParam.getPushUrl())); }else if (cmdParam.getType()==3) { manager.start("test1", CommandBuidlerFactory.createBuidler() .add("ffmpeg") .add("-rtsp_transport","tcp").add("-i", cmdParam.getPlayUrl()) .add("-vcodec","copy") .add("-acodec","copy") .add("-f","flv") .add("-b:v", "2M") .add("-maxrate", "2M") .add("-bufsize", "1M") .add("-y").add(cmdParam.getPushUrl())); Thread.sleep(5000); manager.start("test2", CommandBuidlerFactory.createBuidler() .add("ffmpeg") .add("-rtsp_transport","tcp").add("-i",cmdParam.getPlayUrl()) .add("-vcodec","copy") .add("-acodec","copy") .add("-f","flv") .add("-b:v", "2M") .add("-maxrate", "2M") .add("-bufsize", "1M") .add("-y").add(cmdParam.getPushUrl2())); }else if (cmdParam.getType()==4){ //单进程 多路 manager.start("test4", CommandBuidlerFactory.createBuidler() .add("ffmpeg") .add("-rtsp_transport", "tcp").add("-i", cmdParam.getPlayUrl()) .add("-vcodec", "copy") .add("-acodec", "copy") .add("-b:v", "2M") .add("-maxrate", "2M") .add("-bufsize", "1M") //多路推流测试 .add("-map", "0") .add("-f").add(cmdParam.getPushUrlMap())); } status =1; //检测线程 Thread thread = new Thread(new Runnable() { public void run() { try { checkMsg(); } catch (Exception e) { e.printStackTrace(); } } }); thread.start(); Thread.sleep(20000); // 停止全部任务 manager.stopAll(); status=0;*/ }catch (Exception e){ log.error("",e); } //ds.close(); } private void checkMsg() throws InterruptedException { long oldtime = System.currentTimeMillis(); String oldMsg = ""; while (true) { String msg = OutHandler.outMsg==null?"":OutHandler.outMsg; if (!msg.equals(oldMsg)) { oldMsg=msg; } if (System.currentTimeMillis()-oldtime>15000&&msg.equals(oldMsg)&&status==1){ log.info("消息未输出{}",msg); if(manager.queryAll().size()>0){ Collection list = manager.queryAll(); Iterator commandTaskerIterator = list.iterator(); CommandTasker s = commandTaskerIterator.next(); log.info("0000{},{}",s.getId(),s.getCommand()); log.info("任务消息未输出",s); manager.stopAll(); Thread.sleep(2000); if (status==0){ return; } String result= manager.start(s.getId(),s.getCommand().split("bin/")[1]); if (!StringUtils.isNullOrEmpty(result)){ oldtime= System.currentTimeMillis(); }else{ log.info("推流失败,重新推流"); } }; } if (status==0){ return; } Thread.sleep(2000); } } }