From 095b12ebbdb4fe15a718f6795d4585f6366e62af Mon Sep 17 00:00:00 2001 From: 648540858 <648540858@qq.com> Date: 星期四, 08 六月 2023 10:40:16 +0800 Subject: [PATCH] 优化ffmpeg拉流代理 --- src/main/java/com/genersoft/iot/vmp/service/impl/StreamProxyServiceImpl.java | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 files changed, 55 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/genersoft/iot/vmp/service/impl/StreamProxyServiceImpl.java b/src/main/java/com/genersoft/iot/vmp/service/impl/StreamProxyServiceImpl.java index 68e1122..d63781b 100644 --- a/src/main/java/com/genersoft/iot/vmp/service/impl/StreamProxyServiceImpl.java +++ b/src/main/java/com/genersoft/iot/vmp/service/impl/StreamProxyServiceImpl.java @@ -111,9 +111,43 @@ logger.warn("淇濆瓨浠g悊鏈壘鍒板湪绾跨殑ZLM..."); throw new ControllerException(ErrorCode.ERROR100.getCode(), "淇濆瓨浠g悊鏈壘鍒板湪绾跨殑ZLM"); } - String dstUrl = String.format("rtmp://%s:%s/%s/%s", "127.0.0.1", mediaInfo.getRtmpPort(), param.getApp(), - param.getStream() ); + String dstUrl; + if ("ffmpeg".equalsIgnoreCase(param.getType())) { + JSONObject jsonObject = zlmresTfulUtils.getMediaServerConfig(mediaInfo); + if (jsonObject.getInteger("code") != 0) { + throw new ControllerException(ErrorCode.ERROR100.getCode(), "鑾峰彇娴佸獟浣撻厤缃け璐�"); + } + JSONArray dataArray = jsonObject.getJSONArray("data"); + JSONObject mediaServerConfig = dataArray.getJSONObject(0); + String ffmpegCmd = mediaServerConfig.getString(param.getFfmpeg_cmd_key()); + String schema = getSchemaFromFFmpegCmd(ffmpegCmd); + if (schema == null) { + throw new ControllerException(ErrorCode.ERROR100.getCode(), "ffmpeg鎷夋祦浠g悊鏃犳硶浠巉fmpeg cmd涓幏鍙栧埌杈撳嚭鏍煎紡"); + } + int port; + String schemaForUri; + if (schema.equalsIgnoreCase("rtsp")) { + port = mediaInfo.getRtspPort(); + schemaForUri = schema; + }else if (schema.equalsIgnoreCase("flv")) { + port = mediaInfo.getHttpPort(); + schemaForUri = "http"; + }else if (schema.equalsIgnoreCase("rtmp")) { + port = mediaInfo.getRtmpPort(); + schemaForUri = schema; + }else { + port = mediaInfo.getRtmpPort(); + schemaForUri = schema; + } + + dstUrl = String.format("%s://%s:%s/%s/%s", schemaForUri, "127.0.0.1", port, param.getApp(), + param.getStream()); + }else { + dstUrl = String.format("rtmp://%s:%s/%s/%s", "127.0.0.1", mediaInfo.getRtmpPort(), param.getApp(), + param.getStream()); + } param.setDst_url(dstUrl); + logger.info("[鎷夋祦浠g悊] 杈撳嚭鍦板潃涓猴細{}", dstUrl); param.setMediaServerId(mediaInfo.getId()); boolean saveResult; // 鏇存柊 @@ -160,6 +194,25 @@ } } + private String getSchemaFromFFmpegCmd(String ffmpegCmd) { + ffmpegCmd = ffmpegCmd.replaceAll(" + ", " "); + String[] paramArray = ffmpegCmd.split(" "); + if (paramArray.length == 0) { + return null; + } + for (int i = 0; i < paramArray.length; i++) { + if (paramArray[i].equalsIgnoreCase("-f")) { + if (i + 1 < paramArray.length - 1) { + return paramArray[i+1]; + }else { + return null; + } + + } + } + return null; + } + /** * 鏂板浠g悊娴� * @param streamProxyItem -- Gitblit v1.8.0