Exemplo n.º 1
0
        public async Task <M3U8DownloadResult> DownloadM3U8FilesAsParallel(M3U8File mfile, string savedir, CancellationToken token, bool skipexistfile = true)
        {
            var result = new M3U8DownloadResult();

            try
            {
                Directory.CreateDirectory(savedir);
                DownloadM3U8Head(mfile.Head, savedir);
                if (token.IsCancellationRequested)
                {
                    result.IsCancelled = true;
                    return(result);
                }
                var action = new Action <M3U8Segment, bool>((node, succ) =>
                {
                    if (succ)
                    {
                        result.DownloadedNodes.Add(node);
                    }
                    var e = new M3U8VideoDownloadProgressChangedArgs(mfile, node)
                    {
                        IsNodeDownloadSuccess = succ,
                        Count = result.DownloadedNodes.Count
                    };
                    OnReport(e);
                });
                await DownloadM3U8SegmentsAsParallel(mfile.Segments, savedir, token, skipexistfile, action);
            }
            catch (Exception ex)
            {
                result.Exception = ex;
            }
            return(result);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 异步下载M3U8视频
        /// </summary>
        /// <param name="target"></param>
        /// <param name="savedir"></param>
        public async Task <M3U8DownloadResult> DownloadM3U8VideoFilesAsync(M3U8File target, string savedir, CancellationToken token, bool skipexistsegment = false)
        {
            var result = new M3U8DownloadResult();

            try
            {
                Directory.CreateDirectory(savedir);
                await Task.Factory.StartNew(() => DownloadM3U8Head(target.Head, savedir), token);

                var action = new Action <M3U8Segment, bool>((node, succ) =>
                {
                    if (succ)
                    {
                        result.DownloadedNodes.Add(node);
                    }
                    var arg = new M3U8VideoDownloadProgressChangedArgs(target, node)
                    {
                        IsNodeDownloadSuccess = succ,
                        Count = result.DownloadedNodes.Count
                    };
                    OnReport(arg);
                    token.ThrowIfCancellationRequested();
                });
                result.IsCancelled = await DownloadM3U8VideoSegments(target.Segments, savedir, skipexistsegment, action);

                if (!result.IsCancelled)
                {
                    result.IsComplete = true;
                }
            }
            catch (Exception ex)
            {
                result.Exception = ex;
            }
            return(result);
        }