对M3u8视频文件进行下载
2020-03-29 11:59

Imports System.IO
Imports System.Threading
Imports System.Text.RegularExpressions

Public Class Form1
    Dim mythread As Thread
    Dim strWebFile() As String
    Dim Flag As String

    Private Sub btnChange_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnChange.Click
        Dim i As Integer, j As Integer, myfile As String, strAllfile() As String

        lblState.Text = "检测目录..."
        Dim fi As New DirectoryInfo("D:\Open21\") '创建两个目录
        If Not fi.Exists Then
            fi.Create()
        End If
        fi = Nothing
        fi = New DirectoryInfo("D:\Open21\1\")
        If Not fi.Exists Then
            fi.Create()
        End If

        dlgOpen.Multiselect = True                      '输入m3u8原始文件
        dlgOpen.Filter = "m3u8 files (*.m3u8)|*.m3u8"
        If dlgOpen.ShowDialog = Windows.Forms.DialogResult.OK Then
            strAllFile = dlgOpen.FileNames.ToArray
        Else
            Exit Sub
        End If

        lblState.Text = "逐个文件分析...."
        For i = 0 To strAllfile.GetUpperBound(0)

            Dim fso As New StreamReader(strAllfile(i))  '逐个m3u8文件读取
            Dim strTemp As String = fso.ReadToEnd

            lblState.Text = "提取服务器上各小TS地址"
            Dim reg As New Regex("http:[^,]*.ts")       '提取服务器上各小TS地址
            For j = 1 To reg.Matches(strTemp).Count
                ReDim Preserve strWebFile(j - 1)
                strWebFile(j - 1) = reg.Matches(strTemp)(j - 1).Value
            Next

            For j = 0 To strWebFile.GetUpperBound(0)    '下载各小Ts文件
                Flag = "down"
                lblState.Text = "下载第" & i + 1 & "个文件中第" & j + 1 & "号ts视频文件..."
                mythread = New Thread(AddressOf DownFile)
                mythread.Start(j)

                Do While Flag = "down"
                    Application.DoEvents()
                Loop
            Next

            lblState.Text = "第" & i & "个文件全部小ts下载完成!"
            Dim file As New FileInfo(strAllfile(i))
            myfile = "D:\Open21\" & Replace(file.Name, ".m3u8", "") & ".ts"
            '合并
            j = Shell("cmd /c copy /b D:\Open21\1\*.ts  """ & myfile & """", vbNormalFocus) ', vbHide)
            System.Threading.Thread.Sleep(60000) '等待6秒,上面基本合并完成
            Kill("D:\Open21\1\*.ts") '删除
        Next
    End Sub

    Private Sub DownFile(ByVal n As Integer)
        My.Computer.Network.DownloadFile(strWebFile(n), "D:\Open21\1\" & n + 1 & ".ts")

        Flag = "stop"
        mythread.Abort()
    End Sub


End Class
'———————————————
'版权声明:本文为CSDN博主「dzweather」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
'原文链接:https://blog.csdn.net/dzweather/java/article/details/49700961