You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

AppleScript控制Blackmagic Videohub时nc端口参数错误求助

问题

我要给Blackmagic Design Videohub路由器发送如下格式的命令:

VIDEO OUTPUT ROUTING:
0 1
2 1
3 1
6 1
7 1
8 1
9 1
10 1
11 1
↩

但现在程序还没执行命令就因为端口参数错误终止了,报错「nc: port range not valid」。我用AppleScript写了通过netcat控制设备的脚本,但端口参数始终无法正确传递给netcat,试了好几种写法都不行,附上代码求解决思路:

display dialog "Choose an option:" buttons {"Live", "Cut to Channel"}
set userChoice to button returned of result

on sendCommand(IP, port, command)
   do shell script "printf " & quoted form of command & " | nc " & IP & " " & (port as string)
end sendCommand

on checkACK(reply)
   if reply contains "NAK" then
      display dialog "Error: " & reply
   end if
end checkACK

if userChoice is "Live" then
   --Clean Switch commands
   set CleanSwitchIP to "[IP]"
   set CleanSwitchPort to "[Port]"
   set commands to "VIDEO OUTPUT ROUTING:\\n0 1\\n2 1\\n3 1\\n6 1\\n7 1\\n8 1\\n9 1\\n10 1\\n11 1\\n"
   
   set reply to sendCommand(CleanSwitchIP, CleanSwitchPort, commands)
   checkACK(reply)
   
   -- Video Hub commands
   set VideoHubIP to "[IP]"
   set VideoHubPort to "[PORT]"
   set command to "VIDEO OUTPUT ROUTING:\\n24 12\\n"
   
   set reply to sendCommand(VideoHubIP, VideoHubPort, command)
   checkACK(reply)
   
   -- Video Hub 2 commands
   set VideoHub2IP to "[IP]"
   set VideoHub2Port to "[PORT]"
   set commands to "VIDEO OUTPUT ROUTING:\\n13 30\\n14 30\\n15 30\\n16 30\\n20 30\\n22 30\\n23 30\\n24 30\\n25 30\\n27 30\\n"
   
   set reply to sendCommand(VideoHub2IP, VideoHub2Port, commands)
   checkACK(reply)
   
   display dialog "You are now Live to the studio!"
   
else if userChoice is "Cut to Channel" then
   -- Clean Switch commands
   set CleanSwitchIP to "[IP]"
   set CleanSwitchPort to "[Port]"
   set commands to "VIDEO OUTPUT ROUTING:\\n0 0\\n2 0\\n3 0\\n6 0\\n7 0\\n8 0\\n9 0\\n10 0\\n11 0\\n"
   
   set reply to sendCommand(CleanSwitchIP, CleanSwitchPort, commands)
   checkACK(reply)
   
   -- Video Hub 2 commands
   set VideoHub2IP to "[IP]"
   set VideoHub2Port to "[Port]"
   set commands to "VIDEO OUTPUT ROUTING:\\n13 30\\n14 30\\n15 30\\n16 30\\n20 30\\n22 30\\n23 30\\n24 30\\n25 30\\n27 30\\n"
   
   set reply to sendCommand(VideoHub2IP, VideoHub2Port, commands)
   checkACK(reply)
   
end if
解决思路
  • 给端口参数加引号转义:netcat报端口范围无效,大概率是端口参数被解析成了多个值(比如带空格或特殊字符)。修改sendCommand函数里的nc命令,把端口用quoted form包裹,确保shell把它当成单个参数:
    on sendCommand(IP, port, command)
       do shell script "printf '%b' " & quoted form of command & " | nc " & quoted form of IP & " " & quoted form of (port as string)
    end sendCommand
    
  • 正确解析换行符:原来的printf直接输出的\n是纯文本,不会被转换成实际换行。改用printf '%b'可以让shell解析转义字符,生成设备需要的换行格式,确保命令被正确识别。
  • 检查端口变量的实际值:替换[Port]的时候,要确保是纯数字,没有多余空格或符号(比如不要写成" 9990 ",要写"9990"),否则netcat会误判为端口范围。
  • 调试shell命令:可以在sendCommand里加一行打印,看看实际生成的shell命令是什么,方便排查参数问题:
    on sendCommand(IP, port, command)
       set shellCmd to "printf '%b' " & quoted form of command & " | nc " & quoted form of IP & " " & quoted form of (port as string)
       display dialog "Executing: " & shellCmd -- 调试用,测试完可以删掉
       do shell script shellCmd
    end sendCommand
    
修改后的完整代码
display dialog "Choose an option:" buttons {"Live", "Cut to Channel"}
set userChoice to button returned of result

on sendCommand(IP, port, command)
   do shell script "printf '%b' " & quoted form of command & " | nc " & quoted form of IP & " " & quoted form of (port as string)
end sendCommand

on checkACK(reply)
   if reply contains "NAK" then
      display dialog "Error: " & reply
   end if
end checkACK

if userChoice is "Live" then
   --Clean Switch commands
   set CleanSwitchIP to "[IP]"
   set CleanSwitchPort to "[Port]"
   set commands to "VIDEO OUTPUT ROUTING:\\n0 1\\n2 1\\n3 1\\n6 1\\n7 1\\n8 1\\n9 1\\n10 1\\n11 1\\n"
   
   set reply to sendCommand(CleanSwitchIP, CleanSwitchPort, commands)
   checkACK(reply)
   
   -- Video Hub commands
   set VideoHubIP to "[IP]"
   set VideoHubPort to "[PORT]"
   set command to "VIDEO OUTPUT ROUTING:\\n24 12\\n"
   
   set reply to sendCommand(VideoHubIP, VideoHubPort, command)
   checkACK(reply)
   
   -- Video Hub 2 commands
   set VideoHub2IP to "[IP]"
   set VideoHub2Port to "[PORT]"
   set commands to "VIDEO OUTPUT ROUTING:\\n13 30\\n14 30\\n15 30\\n16 30\\n20 30\\n22 30\\n23 30\\n24 30\\n25 30\\n27 30\\n"
   
   set reply to sendCommand(VideoHub2IP, VideoHub2Port, commands)
   checkACK(reply)
   
   display dialog "You are now Live to the studio!"
   
else if userChoice is "Cut to Channel" then
   -- Clean Switch commands
   set CleanSwitchIP to "[IP]"
   set CleanSwitchPort to "[Port]"
   set commands to "VIDEO OUTPUT ROUTING:\\n0 0\\n2 0\\n3 0\\n6 0\\n7 0\\n8 0\\n9 0\\n10 0\\n11 0\\n"
   
   set reply to sendCommand(CleanSwitchIP, CleanSwitchPort, commands)
   checkACK(reply)
   
   -- Video Hub 2 commands
   set VideoHub2IP to "[IP]"
   set VideoHub2Port to "[Port]"
   set commands to "VIDEO OUTPUT ROUTING:\\n13 30\\n14 30\\n15 30\\n16 30\\n20 30\\n22 30\\n23 30\\n24 30\\n25 30\\n27 30\\n"
   
   set reply to sendCommand(VideoHub2IP, VideoHub2Port, commands)
   checkACK(reply)
   
end if

内容的提问来源于stack exchange,提问作者Joseph La Mere

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 11:42:38