【问题标题】:How to check if a video or an audio stream is online or not in Vbscript?如何在 Vbscript 中检查视频或音频流是否在线?
【发布时间】:2020-04-17 18:08:56
【问题描述】:

我想知道在 vbscript 中检查视频或音频流是否在线时要查找的参数或值是什么?

所以这是我到目前为止尝试的代码,但是,它给了我一个错误的结果,因为我已经用 VLC 检查了所有这些流并且它们工作 5/5,但是使用这个脚本,第二个和第三个流给出我离线了??

Option Explicit
Dim Title,URLArray,URL,ws,Msg,Data
Title = "Audio and Video Stream Checker"
URLArray = Array("https://5ac31d8a4c9af.streamlock.net/saheltv/_definst_/myStream/chunklist_w956788169.m3u8"_
,"http://aska.ru-hoster.com:8053/autodj"_
,"http://www.chocradios.ch/djbuzzradio_windows.mp3.asx")

Call ForceCScriptExecution()

For Each URL in URLArray
    wscript.echo "The stream " & URL & " is "& CheckOnline(URL) & vbCrlf & String(100,"-")
Next
'----------------------------------------------------
Function CheckOnline(URL)
    On Error Resume Next
    Const WHR_EnableRedirects = 6
    Dim h,AllResponseHeaders
    Set h = CreateObject("WinHttp.WinHttpRequest.5.1")
    h.Option(WHR_EnableRedirects) = False 'disable redirects
    h.Open "HEAD", URL , False
    h.Send()
    AllResponseHeaders = h.GetAllResponseHeaders()
    wscript.echo AllResponseHeaders 
    If Err.number = 0 Then
        If h.status = 200 OR h.status = 201 OR h.status = 202 OR h.status = 203 OR h.status = 204 Then
            CheckOnline = "ONLINE"
        Else
            CheckOnline = "OFFLINE"
        End IF
    Else
        CheckOnline = "OFFLINE" & vbCrlf & Err.Description
        On Error Goto 0
    End IF
End Function
'----------------------------------------------------
Sub ForceCScriptExecution()
    Dim Arg, Str, cmd
    cmd = "CMD /K Title " & Title &" & color 0A & "
    If Not LCase( Right( WScript.FullName, 12 ) ) = "\cscript.exe" Then
        For Each Arg In WScript.Arguments
            If InStr( Arg, " " ) Then Arg = """" & Arg & """"
            Str = Str & " " & Arg
        Next
        CreateObject( "WScript.Shell" ).Run _
           cmd & "cscript //nologo """ & _
            WScript.ScriptFullName & _
            """ " & Str
        WScript.Quit
    End If
End Sub
'--------------------------------------------------

我得到了这样的回应:

Cache-Control: no-cache
Date: Sat, 28 Dec 2019 00:14:27 GMT
Content-Length: 232
Content-Type: application/vnd.apple.mpegurl
Accept-Ranges: bytes
Server: WowzaStreamingEngine/4.7.8
Access-Control-Expose-Headers: Date, Server, Content-Type, Content-Length
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
Access-Control-Allow-Methods: OPTIONS, GET, POST, HEAD
Access-Control-Allow-Headers: Content-Type, User-Agent, If-Modified-Since, Cache-Control, Range


The stream https://5ac31d8a4c9af.streamlock.net/saheltv/_definst_/myStream/chunklist_w956788169.m3u8 is ONLINE
-------------------------------------------------------------------------------
Cache-Control: no-cache
Date: Sat, 28 Dec 2019 00:14:28 GMT
Pragma: no-cache
Content-Type: text/html; charset=utf-8
Expires: Mon, 26 Jul 1997 05:00:00 GMT
Server: Icecast 2.4.2


The stream http://aska.ru-hoster.com:8053/autodj is OFFLINE
-------------------------------------------------------------------------------
Connection: keep-alive
Date: Sat, 28 Dec 2019 00:14:28 GMT
Content-Type: text/html; charset=iso-8859-1
Location: https://www.chocradios.ch/djbuzzradio_windows.mp3.asx
Server: nginx
X-Powered-By: PleskLin


The stream http://www.chocradios.ch/djbuzzradio_windows.mp3.asx is OFFLINE
-------------------------------------------------------------------------------

【问题讨论】:

  • MsgBox URL & vbCrlf & All这是什么意思,是什么状态,如果有错误号是多少?
  • @Mark 我刚刚编辑了我的问题参考您的评论!
  • 我在 IE 中使用 HTTP 调试器 Fiddler (docs.telerik.com/fiddler/configure-fiddler/tasks/…) 转到第三个,它似乎使用了一个名为 icy 的协议,并且数据没有通过 HTTPS 或 HTTP 进入。
  • 那么每个h.status的状态是什么?
  • 似乎请求不适用于第二个 URL。您可以尝试通过从location 标头对 URL 的下一个请求来解开第三个 URL。虽然我猜WinHttp.WinHttpRequest.5.1(以及其他 HTTP 请求)由于连接变化而不太适合该任务。我会尝试找到一些支持命令行界面或 ActiveX 的应用程序,并且能够确定流是否存在,并且会在后台默默地为您完成所有工作。那可能是 VLC 或 WMP,或其他东西......

标签: vbscript http-headers video-streaming audio-streaming http-live-streaming


【解决方案1】:

以下是 WMP 的可能实现方式:

Option Explicit

forceCScriptExecution "Audio and Video Stream Checker"
testStreams

Sub testStreams()

    Dim urls
    Dim sourceUrl
    Dim isOnline
    Dim mediaName
    Dim streamUrl

    urls = Array(_
        "https://5ac31d8a4c9af.streamlock.net/saheltv/_definst_/myStream/chunklist_w956788169.m3u8", _
        "http://aska.ru-hoster.com:8053/autodj", _
        "http://www.chocradios.ch/djbuzzradio_windows.mp3.asx" _
    )
    For Each sourceUrl In urls
        WScript.Echo ""
        WScript.Echo "sourceUrl " & sourceUrl
        WScript.Echo "Checking..."
        checkStreamOnline sourceUrl, 30, isOnline, mediaName, streamUrl
        WScript.Echo "isOnline " & isOnline
        WScript.Echo "mediaName " & mediaName
        WScript.Echo "streamUrl " & streamUrl
    Next

End Sub

Sub checkStreamOnline(sourceUrl, timeout, isOnline, mediaName, streamUrl)

    Dim wmp
    Dim isTimeout
    Dim t

    Set wmp = CreateObject("WMPlayer.OCX")
    wmp.settings.autostart = False
    wmp.url = sourceUrl
    wmp.settings.volume = 0
    wmp.controls.play
    t = DateAdd("s", timeout, Now)
    Do
        isOnline = wmp.playState = 3
        isTimeout = Now >= t
        WScript.Sleep 5
    Loop Until isOnline Or isTimeout
    mediaName = wmp.currentMedia.Name
    streamUrl = wmp.currentMedia.sourceUrl
    wmp.controls.stop

End Sub

Sub forceCScriptExecution(title)

    Dim arg, args, cmd

    cmd = "%comspec% /K Title " & title &" & color 0A & "
    If Not LCase(Right(WScript.FullName, 12)) = "\cscript.exe" Then
        For Each arg In WScript.Arguments
            If InStr(arg, " ") Then arg = """" & arg & """"
            args = args & " " & arg
        Next
        CreateObject("WScript.Shell").Run _
           cmd & "cscript //nologo """ & _
            WScript.ScriptFullName & _
            """ " & args
        WScript.Quit
    End If

End Sub

【讨论】:

  • 结果准确!谢谢你,兄弟 !我们可以给wmp对象添加静音功能吗?
  • 流播放时是否有任何不良声音?我在测试时没有注意到...
  • @Hackoo 我无论如何都添加了wmp.settings.volume = 0,这应该使检查完全静默。
猜你喜欢
  • 1970-01-01
  • 2012-07-27
  • 1970-01-01
  • 1970-01-01
  • 2021-01-22
  • 1970-01-01
  • 2013-05-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多