public void Move(StorageFile file) { File.Move(ToString(), file.ToString()); }
private static void AddToFile(YoutubeEntry entry, Uri uri, Stream destinationStream, long? start, long? stop, MSYoutubeLoading onYoutubeLoading, StorageFile storageFile, bool retry = false) { if (entry.ExecutionStatus != ExecutionStatus.Normal) return; var response = DownloadToStreamAsync(uri, start, stop); var cache = CacheManager.Instance; if (response == null || response.StatusCode == HttpStatusCode.RequestedRangeNotSatisfiable) { cache.SetUrl(entry.YoutubeUrl.VideoId, entry.Title, (new FileInfo(storageFile.ToString())).Length); return; } var range = GetRange(response); var total = range.Length; cache.SetUrl(entry.YoutubeUrl.VideoId, entry.Title, total); var to = range.To; using (var stream = response.GetResponseStream()) { if (stream == null) return; try { stream.CopyTo(destinationStream); destinationStream.Flush(); } catch (WebException) { if (retry) return; AddToFile(entry, uri, destinationStream, start, stop, onYoutubeLoading, storageFile, true); } if (onYoutubeLoading != null && entry.ExecutionStatus == ExecutionStatus.Normal) onYoutubeLoading(to, total); if (total > to + 1) AddToFile(entry, uri, destinationStream, to + 1, to + BlockSize, onYoutubeLoading, storageFile); } }
protected void TranscodeFile(StorageFile videoFile, StorageFile audioFile) { var tmpFile = KnownFolders.TempFolder.CreateFile(Guid.NewGuid().ToString("N") + ".mp3"); var arguments = String.Format("-i \"{0}\" -acodec mp3 -y -ac 2 -ab 160 \"{1}\"", videoFile, tmpFile); var process = new Process { EnableRaisingEvents = true, StartInfo = { FileName = _applicationPath + "\\Executables\\ffmpeg.exe", Arguments = arguments, CreateNoWindow = true, RedirectStandardError = true, RedirectStandardOutput = true, UseShellExecute = false, } }; if (_onEntryDownloadStatusChange != null) { _onEntryDownloadStatusChange(_youtubeEntry, DownloadState.ConvertAudioStart, 50); } process.Start(); using (var d = process.StandardError) { var duration = new TimeSpan(); do { var s = d.ReadLine() ?? ""; if (s.Contains("Duration: ")) { duration = ParseDuration("Duration: ", ',', s); } else { if (s.Contains(" time=")) { var current = ParseDuration(" time=", ' ', s); var percentage = (current.TotalMilliseconds / duration.TotalMilliseconds) * 50; if (_onEntryDownloadStatusChange != null) { _onEntryDownloadStatusChange(_youtubeEntry, DownloadState.DownloadProgressChanged, 50 + percentage); } } } } while (!d.EndOfStream); } process.WaitForExit(); DownloadState state; if (process.ExitCode == 0) { try { Tag(tmpFile.ToString()); tmpFile.Move(audioFile); state = DownloadState.Ready; } catch { state = DownloadState.Error; } } else { if (tmpFile.Exists()) { tmpFile.Delete(); } state = DownloadState.Error; } if (_onEntryDownloadStatusChange != null) { _onEntryDownloadStatusChange(_youtubeEntry, state, 100.0); } process.Close(); }
public static void DownloadToFileAsync(YoutubeEntry entry, Uri uri, StorageFile storageFile, MSYoutubeLoading onYoutubeLoading) { if (entry.ExecutionStatus != ExecutionStatus.Normal) return; using (var destinationStream = storageFile.OpenStreamForWrite()) { if (destinationStream == null) return; var start = destinationStream.Length; destinationStream.Position = destinationStream.Length; AddToFile(entry, uri, destinationStream, start, start + BlockSize - 1, onYoutubeLoading, storageFile); } }
public bool NeedsDownload(string videoId, StorageFile storageFile) { if (UrlCaches.ContainsKey(videoId) && storageFile.Exists()) { var fileName = storageFile.ToString(); var urlCache = UrlCaches[videoId]; if (storageFile.Length >= urlCache.Length) { if (!VideoCaches.ContainsKey(fileName)) SetFinished(urlCache.VideoId, fileName, false); else if (VideoCaches[fileName].Finished) return false; } foreach (var item in VideoCaches.Where(p => p.Value.FileName != fileName && p.Value.Finished && File.Exists(p.Value.FileName) && p.Value.VideoId == videoId)) { try { File.Copy(item.Value.FileName, fileName, true); } catch (IOException) { return true; } SetFinished(urlCache.VideoId, fileName, true); return false; } } return true; }