1

我正在尝试用 C# 为桌面制作视频下载应用程序。
现在的问题是以下代码可以正常工作:

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");

但以下代码没有:

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

(在这种情况下,会下载 200-400 KB 的垃圾文件)

两个 URL 之间的区别很明显,第一个包含文件的确切名称,而另一个似乎以某种方式加密.​​.....

我无法找到任何适当且令人满意的解决方案,因此我非常感谢您在这里提供一些帮助,谢谢。

注意:
从这里的一个问题中,我得到了一个链接http://youtubefisher.codeplex.com/所以我访问了那里,得到了源代码并阅读了它。这是一项伟大的工作,但我似乎不明白的是,这个人到底是如何知道他必须制作哪些结构和课程才能下载 YouTube 视频,以及为什么他必须经历所有这些麻烦,为什么不我的方法行不通?

请有人指导。再次感谢。

4

2 回答 2

2

要从 youtube 下载视频,您必须找到实际的视频位置。不是您用来观看视频的页面。http://www.youtube.com/watch?v=...url,是一个 html 页面(很像这个),它将从它的源位置加载视频并显示它。通常,您必须解析 html 并从 html 中提取视频位置。

在您的情况下,您已经找到了执行此操作的代码 - 幸运的是,因为从 youtube 下载视频一点也不简单。查看您在问题中提供的链接,疯狂背后的魔力可在YoutubeService.cs / GetDownloadUrl()

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

该方法正在解析 youtube 观看 url 返回的 html 页面,并找到实际的视频内容。增加的复杂性是 youtube 视频也可以是各种不同的格式。

如果下载后需要转换视频类型,我推荐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.

于 2012-07-19T01:23:17.473 回答
1

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

于 2013-05-27T05:37:46.380 回答