Esempio n. 1
0
        private string DownloadTrackRepresentation(TrackRepresentation trackRepresentation, TimeSpan from, TimeSpan to)
        {
            var files = new List <string>();

            var task = DownloadFragment(trackRepresentation.InitFragmentPath);

            task.Wait(TimeSpan.FromMinutes(5));
            if (task.Result)
            {
                files.Add(Path.Combine(destinationDir, GetLastPartOfPath(trackRepresentation.InitFragmentPath)));

                foreach (var fragmentPath in trackRepresentation.GetFragmentsPaths(from, to))
                {
                    task = DownloadFragment(fragmentPath);
                    task.Wait(TimeSpan.FromMinutes(5));
                    if (task.Result)
                    {
                        files.Add(Path.Combine(destinationDir, GetLastPartOfPath(fragmentPath)));
                    }
                    else
                    {
                        break;
                    }
                }
            }

            string outputFile = Path.Combine(destinationDir, DateTime.Now.ToString("yyyyMMddHHmmss") + "_video.mp4");

            CombineChunks(files, outputFile);

            DeleteAllFilesExcept(outputFile, destinationDir);

            return(outputFile);
        }
Esempio n. 2
0
        private IEnumerable <Mp4File> DownloadTrackRepresentation(TrackRepresentation trackRepresentation, TimeSpan from, TimeSpan to, int concurrency = 2)
        {
            string initFile;
            var    files = new List <string>();

            var task = DownloadFragment(trackRepresentation.InitFragmentPath);

            task.Wait(TimeSpan.FromMinutes(5));
            if (DownloadTaskSucceded(task))
            {
                initFile = task.Result;

                bool complete        = false;
                int  downloadedCount = 0;
                while (!complete)
                {
                    var tasks = trackRepresentation.GetFragmentsPaths(from, to)
                                .Skip(downloadedCount)
                                .Take(concurrency)
                                .Select(p => DownloadFragment(p))
                                .ToList();
                    tasks.ForEach(t => t.Wait(TimeSpan.FromMinutes(5)));
                    var succeeded = tasks.Where(t => DownloadTaskSucceded(t)).ToList();
                    succeeded.ForEach(t => files.Add(t.Result));

                    downloadedCount += succeeded.Count;
                    complete         = !tasks.Any() || succeeded.Count != tasks.Count;
                }

                //var chunks = ProcessChunks(initFile, files);
                var chunks = files.Select(f => new Mp4File(f)).ToList();
                chunks.Insert(0, new Mp4InitFile(initFile));

                //DeleteAllFilesExcept(outputFile, destinationDir);

                return(chunks);
            }
            else
            {
                throw new Exception("Failed to download init file");
            }
        }
Esempio n. 3
0
 public Task <IEnumerable <Mp4File> > Download(TrackRepresentation trackRepresentation, TimeSpan from, TimeSpan to)
 {
     return(Task.Factory.StartNew(() => DownloadTrackRepresentation(trackRepresentation, from, to)));
 }
Esempio n. 4
0
 public Task <IEnumerable <Mp4File> > Download(TrackRepresentation trackRepresentation)
 {
     return(Task.Factory.StartNew(() => DownloadTrackRepresentation(trackRepresentation, TimeSpan.Zero, TimeSpan.MaxValue)));
 }
Esempio n. 5
0
 public Task <string> Download(TrackRepresentation trackRepresentation, TimeSpan from, TimeSpan to)
 {
     return(Task.Factory.StartNew(() => DownloadTrackRepresentation(trackRepresentation, from, to)));
 }
Esempio n. 6
0
 public Task <string> Download(TrackRepresentation trackRepresentation)
 {
     return(Task.Factory.StartNew(() => DownloadTrackRepresentation(trackRepresentation, TimeSpan.Zero, TimeSpan.MaxValue)));
 }