B-Abbasi
B-Abbasi

Reputation: 823

Issue in downloading video from Youtube?

I am trying to make a video download application for desktop in C#.
Now the problem is that following code works fine:

WebClient webOne = new WebClient();
string temp1 = " http://www.c-sharpcorner.com/UploadFile/shivprasadk/visual-studio-and-net-tips-and-tricks-15/Media/Tip15.wmv";
webOne.DownloadFile(new Uri(temp1), "video.wmv");

But following code doesn't:

temp1="http://www.youtube.com/watch?v=Y_..."

(in this case a 200-400 kilobyte junk file gets downloaded )

Difference between the two URLs is obvious, first one contains exact name for file while other seems to be encrypted in some way...

I was unable to find any proper and satisfactory solution to the problem so I would highly appreciate a little help here, Thanks.

Note:
from one of the questions here I got a link http://youtubefisher.codeplex.com/ so I visited there, got the source code and read it. It's great work but what I don't seem to get is that how in the world that person came to know what structures and classes he had to make for downloading a YouTube video and why did he have to go through all that trouble why isn't my method working?

Someone please guide. Thanks again.

Upvotes: 1

Views: 3745

Answers (2)

SomeRandomProgrammer
SomeRandomProgrammer

Reputation: 314

I found this page @ CodeProject, it shows you how to make a very efficient Youtube downloader using no third party libraries. Remember it is sometimes necessary to slightly modify the code as Youtube sometimes makes changes to it's web structure, which may interfere with the way your app interacts with Youtube. Here is the link: here you can also download the C# project files and see the files directly.

CodeProject - Youtube downloader using C# .NET

Upvotes: 1

caesay
caesay

Reputation: 17271

In order to download a video from youtube, you have to find the actual video location. Not the page that you use to watch the video. The http://www.youtube.com/watch?v=... url, is an html page (much like this one) that will load the video from it's source location and display it. Normally, you have to parse the html and extract the video location from the html.

In your case, you found code that does this already - and lucky you, because downloading videos from youtube is not simple at all. Looking at the link you provided in your question, the magic behind the madness is available in YoutubeService.cs / GetDownloadUrl():

http://youtubefisher.codeplex.com/SourceControl/changeset/view/68461#1113202

That method is parsing the html page returned by a youtube watch url, and finding the actual video content. The added complexity, is that youtube videos can also be a variety of different formats.

If you need to convert the video type after downloading, i recommend FFMPEG

EDIT: In response to your comment - You didnt look at the source code of YoutubeFisher at all, did you.. I'd recommend analysing the file I mentioned (YoutubeService.cs). Although after taking a quick look myself, you'll have to parse the yt.playerConfig variable within the html page.

Use that source to help you.

EDIT: In response to your second comment: "Actually I am trying to develop an application that can download video from any video site." You say that like its easy - fyi, its not. Since every video website is different, you cant just write something that will work for everything out of the box. If I had to do it though, heres how i would: I would write custom parsers for the major video sharing websites (Metacafe, Youtube, Whatever else) so that those ones are guarenteed to work. After that, I would write a "fallover" if you will. Basically, if you're requesting a video from an unknown website, it would scour the html looking for known video extentions (flv, wmv, mp4, etc) and then extract the url from that.

You could use a regex for extracting the url in the latter case, or a combination of something like indexof, substring, and lastindexof.

Upvotes: 2

Related Questions