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:

http://youtube.com/get_video?video_id={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 Source.zip (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 asp.net 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...

C# code to publish, delete, retrieve tweets using oauth

The following C#.net class can be used to update statuses, delete, retweet statuses, obtain information regarding a particular status and any body who retweeted a particular status using status id.

This class implements all the methods that are under the “Tweets Resources” section in Twitter API documentation

 

using System;
using System.Text;
using System.Collections.Generic;

namespace TwitterAPI
{
    public class Tweets
    {
        #region Class-Level-Declarations

        private oAuthTwitter OAuth;
        /// <summary>
        /// Create, Update, retrieve, delete tweets(status messages) using this class
        /// </summary>
        /// <param name="_oauth">An authorized and authenticated oAuth token</param>
        public Tweets(oAuthTwitter _oauth)
        {
            this.OAuth = _oauth;
        }
        public enum ResponseFormat { JSON, XML };

        #endregion

        #region Show:ID

        /// <summary>
        /// Returns a single status, specified by the id parameter below. The status's author will be returned inline.
        /// This does not require authentication as long as the status is not protected
        /// This is a rate limited call
        /// </summary>
        /// <param name="response_format">The format in which you want twitter to respond</param>
        /// <param name="statusid">The numerical ID of the desired status.</param>
        /// <param name="optionalparameters">Any other optional parameters.Use an empty string if you dont want to pass any optional parameters</param>
        /// <returns>Response string from twitter in user selected format</returns>
        public string Show_By_ID(ResponseFormat response_format, string statusid, string optionalparameters)
        {
            if (string.IsNullOrEmpty(statusid))
                throw new ArgumentNullException(statusid, "Status Id cannot be null");
            return OAuth.oAuthWebRequest(oAuthTwitter.Method.GET, "http://api.twitter.com/1/statuses/show/" + statusid + "." + response_format.ToString(), optionalparameters);
        }

        #endregion

        #region Update

        /// <summary>
        /// Updates the authenticating user's status. A status update with text identical to the authenticating user's current status will be ignored to prevent duplicates.
        /// Authentication is required and this call is not rate limited
        /// </summary>
        /// <param name="tweet_message">The text of your status update, up to 140 characters.</param>
        /// <param name="reponse_format">The format in which you want twitter to respond</param>
        /// <param name="optionalparameters">Any optional paramters you want to pass</param>
        /// <returns>Response string from twitter in user selected format </returns>
        public string UpdateStatus(ResponseFormat reponse_format, string tweet_message, string optionalparameters)
        {
            if (string.IsNullOrEmpty(tweet_message))
                throw new ArgumentNullException(tweet_message, "The status message cannot be null");
            return OAuth.oAuthWebRequest(oAuthTwitter.Method.POST, "http://api.twitter.com/1/statuses/update." + reponse_format.ToString(), "status=" + tweet_message + optionalparameters);
        }

         #endregion

        #region Destroy:Id

        /// <summary>
        /// Destroys the status specified by the required ID parameter.In other words deletes the specified tweet
        /// Requires authentication, and rate limited is false
        /// </summary>
        /// <param name="response_format">The format in which you want twitter to respond</param>
        /// <param name="statusid">The numerical ID of the desired status.</param>
        /// <param name="optionalparameters">Any other optional parameters.Use an empty string if you dont want to pass any optional parameters</param>
        /// <returns>Response string from twitter in user selected format</returns>
        public string Destroy_By_Id(ResponseFormat response_format, string statusid, string optionalparameters)
        {
            if (string.IsNullOrEmpty(statusid))
                throw new ArgumentNullException(statusid, "Status Id cannot be null");
            return OAuth.oAuthWebRequest(oAuthTwitter.Method.POST, "http://api.twitter.com/1/statuses/destroy/" + statusid + "." + response_format.ToString(), optionalparameters);
        }

        #endregion

        #region Retweet:Id

        /// <summary>
        /// Retweets a tweet. Returns the original tweet with retweet details embedded.
        /// Does not require authentication, and rate limited is false
        /// </summary>
        /// <param name="response_format">The format in which you want twitter to respond</param>
        /// <param name="statusid">The numerical ID of the desired status.</param>
        /// <param name="optionalparameters">Any other optional parameters.Use an empty string if you dont want to pass any optional parameters</param>
        /// <returns>Response string from twitter in user selected format</returns>
        public string Retweet_By_Id(ResponseFormat response_format, string statusid, string optionalparameters)
        {
            if (string.IsNullOrEmpty(statusid))
                throw new ArgumentNullException(statusid, "Status Id cannot be null");
            return OAuth.oAuthWebRequest(oAuthTwitter.Method.POST, "http://api.twitter.com/1/statuses/retweet/" + statusid + "." + response_format.ToString(), optionalparameters);
        }

        #endregion

        #region Show Retweets:Id

        /// <summary>
        ///Returns up to 100 of the first retweets of a given tweet.
        /// Does not require authentication, and rate limited is false
        /// </summary>
        /// <param name="response_format">The format in which you want twitter to respond</param>
        /// <param name="statusid">The numerical ID of the desired status.</param>
        /// <param name="optionalparameters">Any other optional parameters.Use an empty string if you dont want to pass any optional parameters</param>
        /// <returns>Response string from twitter in user selected format</returns>
        public string Show_Retweets_By_Id(ResponseFormat response_format, string statusid, string optionalparameters)
        {
            if (string.IsNullOrEmpty(statusid))
                throw new ArgumentNullException(statusid, "Status Id cannot be null");
            return OAuth.oAuthWebRequest(oAuthTwitter.Method.GET, "http://api.twitter.com/1/statuses/retweets/" + statusid + "." + response_format.ToString(), optionalparameters);
        }

        #endregion

        #region Show Retweeted By:Id

        /// <summary>
        /// Show user objects of up to 100 members who retweeted the status.
        /// Requires authentication, and rate limited
        /// </summary>
        /// <param name="response_format">The format in which you want twitter to respond</param>
        /// <param name="statusid">The numerical ID of the desired status.</param>
        /// <param name="optionalparameters">Any other optional parameters.Use an empty string if you dont want to pass any optional parameters</param>
        /// <returns>Response string from twitter in user selected format</returns>
        public string Show_Retweetedby_By_Id(ResponseFormat response_format, string statusid, string optionalparameters)
        {
            if (string.IsNullOrEmpty(statusid))
                throw new ArgumentNullException(statusid, "Status Id cannot be null");
            return OAuth.oAuthWebRequest(oAuthTwitter.Method.GET, "http://api.twitter.com/1/statuses/" + statusid + "/retweeted_by." + response_format.ToString(), optionalparameters);
        }

        #endregion

        #region Show Retweeted By:Id

        /// <summary>
        /// Show user ids of up to 100 users who retweeted the status.
        /// Requires authentication, and rate limited
        /// </summary>
        /// <param name="response_format">The format in which you want twitter to respond</param>
        /// <param name="statusid">The numerical ID of the desired status.</param>
        /// <param name="optionalparameters">Any other optional parameters.Use an empty string if you dont want to pass any optional parameters</param>
        /// <returns>Response string from twitter in user selected format</returns>
        public string Show_Retweetedby_By_Id(ResponseFormat response_format, string statusid, string optionalparameters)
        {
            if (string.IsNullOrEmpty(statusid))
                throw new ArgumentNullException(statusid, "Status Id cannot be null");
            return OAuth.oAuthWebRequest(oAuthTwitter.Method.GET, "http://api.twitter.com/1/statuses/" + statusid + "/retweeted_by/ids." + response_format.ToString(), optionalparameters);
        }        #endregion
    }
}

 

This class file uses OAuth implementation by shannon whitley (for more information see my previous post Implementing oauth twitter search in C# and JSON).

You can download the complete source code along with this class from https://sites.google.com/site/coderbuddy/downloads/TwitterAPI.zip?attredirects=0&d=1

TwitterAPI.zip (43,25 kb)

Kiểm tra máy tính có kết nối hay không

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace Coderbuddy
{

    public class CheckInternetConnection
    {
        [DllImport("wininet.dll")]
        private extern static bool InternetGetConnectedState(out int Description, int ReservedValue);



        public bool IsInternetConnectionAvailable()
        {
            int Desc;
            return InternetGetConnectedState(out Desc, 0);
        }
    }
}