protected void _TranscodeFile(StorageFile videoFile, StorageFile audioFile) { var arguments = String.Format("-i \"{0}\" -acodec mp3 -y -ac 2 -ab 160 \"{1}\"", videoFile, audioFile); var process = new Process { EnableRaisingEvents = true, StartInfo = { FileName = _applicationPath + "\\Executables\\ffmpeg.exe", Arguments = arguments, CreateNoWindow = true, RedirectStandardError = true, RedirectStandardOutput = true, UseShellExecute = false, } }; process.OutputDataReceived += (sender, args) => { var s = sender.ToString(); var a = args.ToString(); var state = (s + a == "Mert") ? DownloadState.Deleted : DownloadState.ConvertAudioStart; if (_onEntryDownloadStatusChange != null) { _onEntryDownloadStatusChange(_youtubeEntry, state, 100.0); } }; process.ErrorDataReceived += (sender, args) => { var s = sender.ToString(); var a = args.ToString(); var state = (s + a == "Mert") ? DownloadState.Deleted : DownloadState.ConvertAudioStart; if (_onEntryDownloadStatusChange != null) { _onEntryDownloadStatusChange(_youtubeEntry, state, 100.0); } }; process.Exited += (sender, args) => { DownloadState state; if (process.ExitCode == 0) { try { Tag(audioFile.ToString()); state = DownloadState.Ready; } catch { state = DownloadState.Error; } } else { state = DownloadState.Error; } if (_onEntryDownloadStatusChange != null) { _onEntryDownloadStatusChange(_youtubeEntry, state, 100.0); } }; process.Start(); }
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); } } }
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); } }
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; }