其他分享
首页 > 其他分享> > m3u8及TS文件下载解密:m3u8文件下载及分析(三)

m3u8及TS文件下载解密:m3u8文件下载及分析(三)

作者:互联网

多线程下载TS文件

aria2c下载TS文件

aria2c 是一个用来下载文件的实用程序。支持HTTP(S), FTP, SFTP, BitTorrent,和Metalink协议。我们可以调用aria2c来下载具体的TS文件。

具体c#代码:
var url = this.cururlpath + curdownfile;
var tool = @“aria2c.exe”;
var fi = new FileInfo(textBox6.Text + “\” + textBox9.Text + “\” + curdownfile);
var command = " -c -s 10 -x 10 --file-allocation=none --check-certificate=false -d " + fi.DirectoryName + " -o " + fi.Name + " " + url;
//Console.WriteLine(command);
using (var p = new Process())
{
RedirectExcuteProcess(p, tool, command, (s, e) => ShowDownInfo(i,url, e.Data));
}

在ShowDownInfo回调函数中基于返回的输出,进行相应的判断:
if (a == null) return;

        const string re1 = ".*?"; // Non-greedy match on filler
        const string re2 = "(\\(.*\\))"; // Round Braces 1

        var r = new Regex(re1 + re2, RegexOptions.IgnoreCase | RegexOptions.Singleline);
        var m = r.Match(a);
        if (m.Success)
        {
            var rbraces1 = m.Groups[1].ToString().Replace("(", "").Replace(")", "").Replace("%", "").Replace("s", "0");
            if (rbraces1 == "OK")
            {
                listView1.BeginUpdate();
                listView1.Items[itemidx].SubItems[1].Text = "已下载";
                listView1.EndUpdate();
                 this.Invoke(new Action(() => { label20.Text = "已下载" + (downfilescnt + 2) + "/" + listView1.Items.Count; }));

            }
            //label20.Text = DateTime.Now.ToString().Replace("/", "-") + "    " + url + "    下载进度:" + rbraces1 + "%";
        }

多线程下载

为了提高下载速度,使用了线程池,来调用下载程序。

for (int i = 0; i < thStepCount; i++)
{
thStep[i] = new Thread(threaddownloadfiles);
thStep[i].Name = i.ToString();
thStep[i].Start();
}

线程的同步问题

多个线程同时调用aria2c对m3u8文件中的所有TS文件进行下载,为了保证各线程的工作不重复,不遗漏,需要有同步机制,最开始想用lock来实现,发现比较麻烦,后来改用比较笨的方式,统计TS文件总数,按照线程数进行分配,各自完成自己分配范围内的下载任务:

if (Thread.CurrentThread.Name.Equals(“0”)) //线程1处理的文件
{
startpos = 0; endpos = listView1.Items.Count / 3;

            }
            else if (Thread.CurrentThread.Name.Equals("1")) //线程2处理的文件
            {
                startpos = listView1.Items.Count / 3; endpos = listView1.Items.Count * 2 / 3;
            }
            else if (Thread.CurrentThread.Name.Equals("2")) //线程3处理的文件
            {
                startpos = listView1.Items.Count * 2 / 3; endpos = listView1.Items.Count;
            }

for (int i = startpos; i < endpos; i++)
{
//调用下载的函数;
}

工具下载

TSMatser是一个m3u8及TS文件、CKplayer视频文件下载解密工具。下载key文件和ts文件时采用了多线程技术,调用aria2c进行下载。

csdn 下载:https://download.csdn.net/download/cquptvlry/11262603
百度网盘: https://pan.baidu.com/s/17AdvQjoD0i_oiYKwcxZoGQ 提取码 r987

在这里插入图片描述

标签:文件,listView1,Items,m3u8,线程,var,下载
来源: https://blog.csdn.net/cquptvlry/article/details/94179794