public static MediaItem fromCTVideo(CSTube.Video video) { MediaItem music = new MediaItem(); music.UpdateFromCTVideo(video); return(music); }
public static async Task <CSTube.Video> FetchPTVideoDataAsync(MediaItem item) { CSTube.Video videoData = await CSTube.Video.Fetch(item.URL, false); item.UpdateFromCTVideo(videoData); return(videoData); }
private async void OnFetchFromURL(object sender, RoutedEventArgs e) { string URL = URLInputTextBox.Text; if (CSTube.Extract.isVideoURL(URL)) { // Record as music item CSTube.Video video = new CSTube.Video(URL); MediaItem item = MediaItem.fromCTVideo(video); curMediaList.Add(item); UpdateMediaList(); // Fetch music data and update list once ready await video.FetchInformation(); item.UpdateFromCTVideo(video); UpdateMediaList(); } else if (CSTube.Extract.isPlaylistURL(URL)) { // Fetch playlist items and record them, then continuously update them Debug.Log("Cannot load all playlist videos yet, Google API implementation missing!"); } else { Debug.Log("ERROR: URL does not point to a valid youtube video!"); } }
public void UpdateFromCTVideo(CSTube.Video videoData) { _videoData = videoData; URL = "v=" + videoData.videoID; Source = "YouTube"; if (!_videoData.videoDataAvailable) { Title = "No video data"; } else { Title = videoData.title; Tags = string.Format("{0} streams; {1} captions", videoData.streams.Count(), videoData.captions.Count()); } OnPropertyChanged("Title"); OnPropertyChanged("Tags"); }
public static Task FetchPTVideosDataAsync(List <MediaItem> music) { StartPostLogging(true); Task fetchTask = Task.Factory.StartNew(() => { Parallel.ForEach(music, (MediaItem item) => { CSTube.Video video = new CSTube.Video(item.URL); video.FetchInformation().Wait(); item.UpdateFromCTVideo(video); postLogger.Log(video.ToString()); }); }); return(fetchTask.ContinueWith((Task t) => { EndPostLogging(); Debug.Log("Finished asynchronous PT video data fetch for " + music.Count + " videos!"); }, TaskScheduler.FromCurrentSynchronizationContext())); }
public static async Task <bool> DownloadAudioAsync(MediaItem media, string path = null, Action <MediaItem, long, long> onProgressChanged = null, Action <MediaItem, bool> onComplete = null) { media.isDownloading = true; media.downloadProgress = 0; // Fetch video data first if (media._videoData == null) { await FetchPTVideoDataAsync(media); } CSTube.Video videoData = media._videoData; if (videoData.streams.Count() == 0) { Debug.Log("ERROR: No streams to download found for '" + media.Title + "'!"); return(false); } // Sort video streams to find audio stream of best quality CSTube.Stream bestAudio = videoData.streams.FilterStreams(onlyAudio: true).All() .OrderByDescending(s => Convert.ToInt32(s.getAttribute("bitrate"))).FirstOrDefault(); if (bestAudio == null) { Debug.Log("ERROR: No single audio stream found for '" + media.Title + "'!"); return(false); } if (string.IsNullOrEmpty(path)) { // Get default path string filename = bestAudio.getFileName(media.Title); path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyMusic), "Downloaded_CSTube", filename); } Directory.CreateDirectory(Path.GetDirectoryName(path)); HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(bestAudio.URL); using (HttpWebResponse response = (HttpWebResponse)req.GetResponse()) { // Start streaming to disk long totalBytes = response.ContentLength; Debug.Log(string.Format("Starting to download {0} ({1} total MB)", media.Title, (float)totalBytes / 1000 / 1000)); // Setup wrapper for callback Action <long> progressReport = null; if (onProgressChanged != null) { progressReport = (long bytesRead) => onProgressChanged.Invoke(media, bytesRead, totalBytes); } using (Stream input = response.GetResponseStream()) using (Stream output = File.OpenWrite(path)) { // Start streaming with progress report await CopyToAsync(input, output, progressReport, 8 * 0x1000); } } media.isDownloading = false; media.isDownloaded = true; onComplete?.Invoke(media, true); return(true); }