Ejemplo n.º 1
0
        public async Task DownloadToAsync(string filePath, CancellationToken cancellationToken = default)
        {
            var tempPlaylistPath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName() + ".m3u8");
            var tempMp4Path      = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName() + ".mp4");

            try
            {
                using (var tempPlaylistStream = File.OpenWrite(tempPlaylistPath))
                    using (var response = await _httpClient.GetAsync(_playlistUri, cancellationToken))
                        using (var playlistDownloading = await response.Content.ReadAsStreamAsync())
                        {
                            await playlistDownloading.CopyToAsync(tempPlaylistStream, cancellationToken);
                        }

                await _ffmpeg.ExecuteAsync(tempPlaylistPath, tempMp4Path, cancellationToken);

                File.Move(tempMp4Path, filePath);
            }
            finally
            {
                try { File.Delete(tempPlaylistPath); } catch { }
                try { File.Delete(tempMp4Path); } catch { }
            }
        }