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); }
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"); } }