From 920e63996e47918a9ca6eb137ec3a354dba71b2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=9A=93=E6=9C=88?= <353374337@qq.com> Date: Sat, 6 Oct 2018 13:33:14 +0800 Subject: [PATCH] =?UTF-8?q?m3u8=E8=A7=A3=E6=9E=90=E5=88=B0=E4=B8=B4?= =?UTF-8?q?=E6=97=B6=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../HaoYue.CQTVShow.Hub/HtmlDoIt.cs | 73 ++++++++++++++++++- .../HaoYue.CQTVShow.Hub/MainWindow.xaml.cs | 8 +- .../HaoYue.CQTVShow.Hub/StaticClass.cs | 26 +++++++ 3 files changed, 105 insertions(+), 2 deletions(-) diff --git a/HaoYue.CQTVShow/HaoYue.CQTVShow.Hub/HtmlDoIt.cs b/HaoYue.CQTVShow/HaoYue.CQTVShow.Hub/HtmlDoIt.cs index 95d1dd6..7dd5dda 100644 --- a/HaoYue.CQTVShow/HaoYue.CQTVShow.Hub/HtmlDoIt.cs +++ b/HaoYue.CQTVShow/HaoYue.CQTVShow.Hub/HtmlDoIt.cs @@ -4,6 +4,8 @@ using System.Linq; using System.Text; using HtmlAgilityPack; +using System.IO; +using System.Net; namespace HaoYue.CQTVShow.Hub { @@ -60,6 +62,75 @@ namespace HaoYue.CQTVShow.Hub return ""; } - + + public static ShowInfo GetShowInfo(string url) + { + HtmlWeb htmlweb = new HtmlWeb(); + HtmlDocument htmldoc = htmlweb.Load(url); + string scriptstr = htmldoc.DocumentNode.SelectNodes("//div[@class='main']/script")[0].InnerHtml; + string[] strArr = scriptstr.Split('\n'); + + string vurl_1 = strArr[1].Substring(strArr[1].IndexOf("\"") + 1); + string vurl = vurl_1.Substring(0, vurl_1.LastIndexOf('\"')); + + string url_1 = strArr[6].Substring(strArr[6].IndexOf("\'") + 1); + string url_2 = url_1.Substring(0, url_1.LastIndexOf('\'')); + + ShowInfo si = new ShowInfo(); + si.PlayListM3u8Url = url_2.Replace("' + vurl + '", vurl); + + var list = CheckM3u8File(si.PlayListM3u8Url); + + var normalm3u8 = list.Where(w => w.filetype == 1 && w.Path.Contains("b3000")).FirstOrDefault(); + if (normalm3u8 != null) + { + si.PlayNormalM3u8Url = si.PlayListM3u8Url.Substring(0, si.PlayListM3u8Url.LastIndexOf("/")+1) + normalm3u8.Path; + } + + var hdm3u8 = list.Where(w => w.filetype == 1 && w.Path.Contains("b5000")).FirstOrDefault(); + if (hdm3u8 != null) + { + si.PlayNormalM3u8Url = si.PlayListM3u8Url.Substring(0, si.PlayListM3u8Url.LastIndexOf("/")+1) + normalm3u8.Path; + } + + return si; + } + + + private static List CheckM3u8File(string url) + { + HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url); + webRequest.Method = "GET"; HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse(); + StreamReader sr = new StreamReader(webResponse.GetResponseStream(), Encoding.Default); + + + + + + //AddLog("开始解析m3u8文件"); + var ALLList = new List(); + //string FileText = File.ReadAllText(FilePath); + string FileText = sr.ReadToEnd(); + + int i = 0; + foreach (string s in FileText.Split('\n')) + { + if (s.Trim().Length > 0 && s.Trim().Substring(0, 1) != "#")//去空格后长度不为1 且不是注释 + { + int filetype = 0; + + + if (s.Substring(s.LastIndexOf(".")).Contains("m3u8")) + { + filetype = 1; + } + ALLList.Add(new ModelOfM3u8() { Path = s, filetype = filetype, State = 0, Index = ++i }); + + } + } + //AddLog("解析完毕,共" + ALLList.Where(w => w.filetype == 0).Count() + "个ts视频切片,共" + ALLList.Where(w => w.filetype == 1).Count() + "个m3u8配置文件"); + return ALLList; + } + } } diff --git a/HaoYue.CQTVShow/HaoYue.CQTVShow.Hub/MainWindow.xaml.cs b/HaoYue.CQTVShow/HaoYue.CQTVShow.Hub/MainWindow.xaml.cs index 911decd..2159c16 100644 --- a/HaoYue.CQTVShow/HaoYue.CQTVShow.Hub/MainWindow.xaml.cs +++ b/HaoYue.CQTVShow/HaoYue.CQTVShow.Hub/MainWindow.xaml.cs @@ -116,6 +116,11 @@ namespace HaoYue.CQTVShow.Hub { //获取选择的节目 int SelectedIndex = this.ListBoxShow.SelectedIndex; + + //大概是重新装载列表的时候也会出发选择事件、然而这时候选择的Index会为-1 执行则会错误 + if (SelectedIndex < 0) + return; + ThreadPool.QueueUserWorkItem((obj) => { this.Image_SelectedShow.Dispatcher.BeginInvoke(new myintsetter(SetShowSelected), SelectedIndex); @@ -182,7 +187,8 @@ namespace HaoYue.CQTVShow.Hub /// private void PlayMedia(object sender, RoutedEventArgs e) { - System.Diagnostics.Process.Start(StaticClass.PlayUrl); + //System.Diagnostics.Process.Start(StaticClass.PlayUrl); + StaticClass.StaticShowInfo = HtmlDoIt.GetShowInfo(StaticClass.PlayUrl); } } } diff --git a/HaoYue.CQTVShow/HaoYue.CQTVShow.Hub/StaticClass.cs b/HaoYue.CQTVShow/HaoYue.CQTVShow.Hub/StaticClass.cs index 803d07e..07bb325 100644 --- a/HaoYue.CQTVShow/HaoYue.CQTVShow.Hub/StaticClass.cs +++ b/HaoYue.CQTVShow/HaoYue.CQTVShow.Hub/StaticClass.cs @@ -9,6 +9,7 @@ namespace HaoYue.CQTVShow.Hub { public static List StaticShowList = new List(); public static string PlayUrl = ""; + public static ShowInfo StaticShowInfo = new ShowInfo(); } @@ -22,4 +23,29 @@ namespace HaoYue.CQTVShow.Hub public string Time { get; set; } public string ImageSrc { get; set; } } + + public class ShowInfo + { + public string Title { get; set; } + public string Info { get; set; } + public string PlayList { get; set; } + public string PlayListM3u8Url { get; set; } + public string PlayNormalM3u8Url { get; set; } + public string PlayHDM3u8Url { get; set; } + } + + public class ModelOfM3u8 + { + public string Path { get; set; } + /// + /// [0] 视频格式 [1]m3u8文件 + /// + public int filetype { get; set; } + /// + /// [0]未处理[1]在下中[2]已下载[3]处理失败 + /// + public int State { get; set; } + + public int Index { get; set; } + } }