YouTubery: A Tool to Automatically Download and Convert YouTube Videos to Avi, Mpeg, Mp3, or Burn Them to DVDs

As far as I know, YouTube has blocked the get_video web service. If they release another public web service to download flv's, I or anyone else can extend the library described here by adding an IResolver provider. But for now, it seems this library will not work for downloading YouTube videos. Apologies.

 In my recent two posts, I talked about downloading YouTube videos using C#, and converting audio, video to any imaginable format using FFMpeg. Hopefully not surprising is the fact that these two tutorials can be combined to form a fairly useful tool.

I know I often wished that I could download YouTube videos so that I could watch them offline or listen to them in mp3 form. The application attached at the bottom of this article lets you do just that. You can input a list of YouTube urls and the app will download them, and convert them to any format you specify including avi, mpeg, mp3, wave, etc. After you are done downloading, you can click Burn DVD and the program will automatically import all the downloaded content into Windows DVD Maker for burning.

The app uses WPF for a simple UI, and DownloadManager and ConversionManager libraries (from my previous two articles) for the downloading and converting respectively. It allows for any number of concurrent downloads and conversions to happen.

Using these two libraries to create this app couldn’t be simpler. It’s as simple as creating these two managers and subscribing to started, progressed, finished events. The main window just features a table of “download row” controls, a browse control for the output path, and a textbox for the output format.

I am attaching the source code as well so anyone interested can see an easy example of using my download, converting libraries. I hope you find use in the app, and as always, report any problems to me below. Thanks! (3,50 mb)

YouTubery source (188,25 kb)

Download YouTube Videos using C#

As far as I know, YouTube has blocked the get_video web service. If they release another public web service to download flv's, I or anyone else can extend the library described here by adding an IResolver provider. But for now, it seems this library will not work for downloading YouTube videos. Apologies.

Wouldn’t it be great to be able to download and watch your favorite YouTube videos  anytime you want, even when offline? In fact, wouldn’t it be even better if you can write a program that downloads multiple at once, and converts all of them to a specific audio or video format?

This entire process turns out to be fairly easy, and I want to provide a very brief explanation on how you can do this. I also attached a C# library that will do this for you, and that you can extend as YouTube web services may change in the future.

1. Resolve YouTube FLV download url

YouTube provides a convenient web service to download any public video if you have the two variables that identify the video server-side. These variables, “t” and “video_id”, can be extracted from the actual page source of the YouTube page in the properties that set up the flash video player. Once, we have parsed these variables, we can just download the FLV video from the following url:{0}&t={1}
where {0} is “video_id” and {1} is “t”

Understanding that the url, server variables, or page source format may change any time, and probably will, I used an interface to abstract out the actual url resolving:

public interface IResolver
     string ResolveFlvDownloadUrl(string pageSource, out string videoTitle);

If you write your own IResolver parser, you can still use the rest of my tool by extending DownloadManager and overriding

protected override IResolver CreateResolver()
     return new YtSourceDepthParser();

where YtSourceDepthParser will be your parser.

2. Download YouTube FLV

Just use an HTTPWebRequest to the resolved url to download the FLV video, and save it as a file.  In my tool, DownloadManager will do this for you, and provides events for when the download starts, progresses (bytes left, percent completed, time remaining), and finishes.

3. Watch your video

Enjoy your video in a player that can read FLV flash format videos like VLC.

Stay tuned for further articles where I will describe how to use C# to convert FLV to almost any video, and audio format (avi, mpeg, mp4, mp3, wave, etc.)

Disclaimer: This is a tutorial and a tool using public YouTube web services. The information, code, and programs relayed in this article is purely for technical purposes and is provided without any guarantee or intent.


YoutubeDownloader (5,39 kb)

Ivolo.Tools.Youtube_win32.dll (12,50 kb)

Upload and Convert Video to FLV using ASP.NET and Windows Service

Recently I did a project to create an application for a big winter vacation agency. It is photo and video uploading website, where people can upload photos/videos and have their friends vote on them. Photo part is easy, what interesting is the video part. I thought it would be as simple as uploading mp3 files and create flash player to load whatever people have uploded, as my colleague told me, but then I realized that it is not as simple as handling mp3 files because videos have to be converted to FLV in order for flash player to play the video. 
I've done a lot of research but I can't find how to stream WMV, MPG directly from flash, they have to be converted to FLV first. I found a really useful tool that can be used to convert almost any video file to FLV, or to image (JPG, PNG, etc.). It is called ffmpeg, it is an executable file (.EXE). The problem is that it is not a good idea to grant permission for ASP.NET account on the server to be able to run executable file. If people find out about this, then your server is in danger, because people can execute command to format your harddrive, for example. 

The solution was to create a simple windows service program or you can also make desktop application to handle all the video convertion process. The code is shown below:

You probably wonder what flvtool is. We use flvtool to index the FLV file in order to add the correct metadata inside FLV file. 
This is actually a simple form of a youtube-like application. You let people upload videos, convert them to FLV and create thumbnails for the preview. However, this kind of technique doesn't work in a shared hosing environment because you usually can't install any program on the server.

Làm sao để chạy trên localhost

đây là một câu hỏi mà rất nhiều bạn đã hỏi trung , đây là một vấn đề mà có thể nói là hơi khó hiểu , cài iis asp thường thì chạy rất good còn asp. net thì báo lỗi một cách lạ lùng 

Sao thế nhỉ đây là một lỗi cho chúng ta cài cài VS 2005 hoặc 2003 mà chưa cài IIS Khác...

Tìm kiếm theo TagName và XPath

Tìm kiếm một element trong XML là công việc được thực hiện rất phổ biến. Ta tìm kiếm các element chủ yếu qua tên, thuộc tính của element đó.
Để thực hiện tìm kiếm, ta có thể sử dụng GetElementsByTagName() của XmlDocument để lấy về danh sách các node XmlNodeList hoặc sử dụng XPath – là một cách cao cấp hơn.

1. Tìm kiếm theo XmlNodeList
Trong ví dụ sau, ta sẽ tìm tên của diễn viên đóng trong phim “Tear of the Sun”.
Ta sẽ bắt đầu với việc tìm các DVD có Title = "Tear of the Sun" bằng hàm GetElementsByTagName("Title"). Sau đó ta sẽ duyệt các DVD này và tìm tên các diễn viên tham gia.

static void Main(string[] args)
    string fileName = "DVDList.xml";
    string content = SearchByXMLNodeList(fileName);

private static string SearchByXMLNodeList(string fileName)
    XmlDocument xmlDocument = new XmlDocument();


    // Search all film's title
    XmlNodeList nodeList = xmlDocument.GetElementsByTagName("Title");

    StringBuilder sb = new StringBuilder();

    foreach (XmlNode node in nodeList)
         string title = node.ChildNodes[0].Value;

         if (title.Equals("Tear of the Sun"))
              XmlNode parentNode = node.ParentNode;

              XmlNodeList starList = 

              foreach (XmlNode star in starList)
                   sb.Append("   - Star: ");

     return sb.ToString();

2. Tìm kiếm theo XPath
Sử dụng GetElementByTagName() thực sự có rất nhiều hạn chế. Phương thức này chỉ cho phép ta tìm kiếm theo tên của element.
Việc này khiến người ta suy nghĩ đến một cách thức tìm kiếm cho phép lọc dữ liệu theo element và theo thuộc tính. XPath hoạt động theo phương thức này
Cấu trúc XML có dạng cây, XPath có dạng là đường dẫn từ Root đến bất kỳ node hay attribute nào. Sử dụng phương thức SelectNodes() của XmlDocument để tìm kiếm một node bất kỳ

Ví dụ về sử dụng XPath
- Tìm các tên phim thuộc vào Category
xPath = "/DVDList/DVD/Title[../@Catagory='War']";
- Tìm Category của DVD có ID = 2
xPath = "/DVDList/DVD/@Catagory[../@ID='2']";
- Tìm DVD mà Title bắt đầu bằng chữ 't'
xPath = "/DVDList/DVD[starts-with(Title,'t')]";
- Tìm DVD mà Title có chứa chữa 't'
xPath = "/DVDList/DVD[contains(Title,'t')]";
- Tìm mã số ID của các phim mà có diễn viên Bruce Willis tham gia
xPath = "/DVDList/DVD/@ID[../Stars/Star='Bruce Willis']";
- Tìm các DVD có ID >= 2
xPath = "/DVDList/DVD[@ID >= 2]";
- Tìm các DVD phát hành sau ngày 08/06/2008
xPath = "/DVDList/DVD[translate(@Date,'/','') > 08062008]";


private static string SearchByXPath(string fileName)
    // Create document
    XmlDocument xmlDocument = new XmlDocument();

    // Load xml's content to memory

    // Get DVD's ID that Bruce Willis cast
    xPath = "/DVDList/DVD/@ID[../Stars/Star='Bruce Willis']";

    // Select nodes by XPath
    XmlNodeList nodeList = xmlDocument.SelectNodes(xPath);

    StringBuilder sb = new StringBuilder();

    foreach (XmlNode node in nodeList)

    return sb.ToString();

File XML dùng trong ví dụ


DVDList.xml (1,15 kb)