Content: Slate Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate Marble
Background: Slate Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate Marble
Pattern: Blank Waves Notes Sharp Wood Rockface Leather Honey Vertical Triangles
Welcome to Xbox Chaos: Modding Evolved

Register now to gain access to all of our features. Once registered and logged in, you will be able to contribute to this site by submitting your own content or replying to existing content. You'll be able to customize your profile, receive reputation points as a reward for submitting content, while also communicating with other members via your own private inbox, plus much more! This message will be removed once you have signed in.

Sign in to follow this  
Followers 0
Guest

C#
YouTube Video Downloader

2 posts in this topic

Few days ago, I wrote a C# class which obtains the direct link to the best quality video and audio data from a YouTube video for my Music Videos app I'm currently developing. This script might come in handy for someone who wants to download videos from YouTube or something. This is significantly more reliable than other publicly available C# YouTube download scripts as far as I can see with an about 98% success rate. I plan on rewriting it sometime later to be future-proof and 99.99% reliable.

Note that most if not all videos on YouTube are actually made up of two files, one for video and one for audio so you'll need to merge them.

Licensed under the Microsoft Public License.

Requirements

Note: If you want to use this outside of Windows Store apps, change the Windows.Web.Http reference to System.Net.Http

Usage

var audioVideoSource = await YouTubeVideo.GetDirectLinkAsync("video id");string audioUrl = audioVideoSource.AudioUrl;string videoUrl = audioVideoSource.VideoUrl;

Code

// YouTube Video Direct Link Grabber// Copyright (c) 2014 Matt Savilleusing Newtonsoft.Json.Linq;using System;using System.Collections.Generic;using System.Net.Http;using System.Threading.Tasks;namespace Crescent.MusicVideoPlayer.Helpers{	public static class YouTubeVideo	{		public struct AudioVideoSource		{			public string VideoUrl { get; set; }			public string AudioUrl { get; set; }		}		/// <summary>		/// Obtains a direct link to a video hosted on YouTube.		/// </summary>		/// <param name="videoId">The id of the video</param>		/// <param name="maxVerticalResolution">		/// The maximum acceptable vertical resolution (height in pixels).		/// </param>		/// <returns>A valid direct link to the video with the best possible quality.</returns>		public static async Task<AudioVideoSource> GetDirectLinkAsync (string videoId, int maxVerticalResolution = 1080)		{			string selectedVideoUrl = null, selectedAudioUrl = null;			int selectedVideoWidth = -1, selectedVideoHeight = -1;			using ( var client = new HttpClient() )			{				// Obtain relevant JSON data from the web page for the video.				var contents = await client.GetStringAsync(new Uri("http://www.youtube.com/watch?v=" + videoId));				string search = "ytplayer.config = ";				contents = contents.Substring(contents.IndexOf(search) + search.Length);				contents = contents.Substring(0, contents.IndexOf("};</script>") + 1);				var json = JObject.Parse(contents);				// Obtain all possible video formats and pick the best one.				var adaptiveFormats = Uri.UnescapeDataString(json["args"]["adaptive_fmts"].ToString()).Split(',');				foreach ( var format in adaptiveFormats )				{					int width = -1, height = -1;					string baseUrl = "";					string query = "";					string audioUrl = null;					var parameters = format.Split('&');					foreach ( var parameter in parameters )					{						if ( parameter.StartsWith("url=") )						{							baseUrl = parameter.Substring("url=".Length);						}						else						{							if ( string.IsNullOrEmpty(query) )								query = "?" + parameter;							else if ( !query.Contains(parameter) )								query += "&" + parameter;						}						// Obtain audio stream.						if ( parameter.Contains("mime=audio") )							audioUrl = baseUrl;						// Obtain video resolution.						if ( parameter.StartsWith("size=") )						{							var dimensions = parameter.Substring("size=".Length).Split('x');							width = Int32.Parse(dimensions[0]);							height = Int32.Parse(dimensions[1]);						}					}					// Generate a valid direct link to the video and audio.					string videoUrl = baseUrl + query;					if ( selectedAudioUrl == null && audioUrl != null )					{						selectedAudioUrl = audioUrl + query;					}					// Determine whether this format is the best so far.					if ( audioUrl == null && height <= maxVerticalResolution && ( width * height > selectedVideoWidth * selectedVideoHeight ) )					{						selectedVideoWidth = width;						selectedVideoHeight = height;						selectedVideoUrl = videoUrl;					}				}			}			return new AudioVideoSource			{				AudioUrl = selectedAudioUrl,				VideoUrl = selectedVideoUrl			};		}	}}
Edited by Matt

Share this post


Link to post
Share on other sites

That is cool and all but I use this place. www.convertfiles.com and does more then youtube. You convert the youtubbe or whatever file into format you want, then right click save as or download.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0