示例#1
0
        public static async Task DownloadShowAsync(One show, Live live, IDown d)
        {
            DownManager.down = d;
            // http://media2.neu6.edu.cn/review/program-1524520800-1524526860-chchd.m3u8
            var url = String.Format("http://media2.neu6.edu.cn/review/program-{0}-{1}-{2}.m3u8",
                                    show.start, show.end, live.GetSimpleName());

            Debug.WriteLine(url);
            //创建下载目录
            var downloadDic = await KnownFolders.VideosLibrary.CreateFolderAsync("NEUTV download",
                                                                                 CreationCollisionOption.OpenIfExists);

            //下载m3u8文件
            HttpClient httpClient = new HttpClient();
            var        response   = await httpClient.GetAsync(new Uri(url));

            var res = await response.Content.ReadAsStringAsync();

            Regex           re      = new Regex(@"(?<url>http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?)");
            MatchCollection mc      = re.Matches(res);
            List <String>   urlList = new List <string>();
            var             tsRe    = new Regex(@"(?<ts>[0-9]+\.ts)");

            foreach (Match m in mc)
            {
                var clipUrl = m.Result("${url}");
                urlList.Add(clipUrl);
            }
            if (down != null)
            {
                down.Start(urlList.Count);
            }
            //创建临时文件夹
            var tempDic = await downloadDic.CreateFolderAsync("temp",
                                                              CreationCollisionOption.GenerateUniqueName);

            //下载 视频分片
            var         Tasks = new List <Task>();
            TaskFactory fac   = new TaskFactory();

            Debug.WriteLine("start all");
            try
            {
                Task.Run(() =>
                {
                    Parallel.ForEach <string>(urlList, new ParallelOptions()
                    {
                        MaxDegreeOfParallelism = 10
                    },
                                              videoUrl => {
                        Act(videoUrl, httpClient, tempDic).Wait();
                    });
                }).ContinueWith((obj) =>
                {
                    Debug.WriteLine("end all");
                    SaveVideoFile(tempDic, downloadDic, urlList);
                });
            }
            catch (Exception e)
            {
            }
        }