Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Download you tube video using c#?

Tags:

c#

youtube

I am trying to download you tube video using c# code but i am not getting proper code. I have searched many link but din't get any proper links and code.

I want to download the you tube video in my local folder with c# code. I have tried one link but that code just getting null video in my local folder so any one have the idea how can do that.

Below is the code i have tried so far.

var VedioUrl = "https://www.youtube.com/embed/" + objYouTube.VideoID + ".mp4";

WebRequest MyRequest = HttpWebRequest.Create(VedioUrl);
WebResponse MyResponse = MyRequest.GetResponse();
string RealURL = MyResponse.ResponseUri.ToString();
HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(RealURL);
HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
Stream receiveStream = myHttpWebResponse.GetResponseStream();
Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
StreamReader readStream = new StreamReader(receiveStream, encode);
StreamWriter writer = new StreamWriter(Server.MapPath("~/youtube/" + objYouTube.VideoID + ".mp4"), true);
writer.Write(readStream.ReadToEnd());
writer.Close();

so here is my video url which i am trying to download : "https://www.youtube.com/embed/UCsiNPbLbwZk43FOCRrdKBlA.mp4"

like image 581
coderwill Avatar asked May 23 '17 05:05

coderwill


People also ask

How can I download YouTube video with code?

Go to the video URL and add “ss” before the “youtube.com…” and click enter. You will be directed to another page where you will save the video. This page is the parent website of ssyoutube.com, known as savefrom.net. This page will display all the information regarding the YouTube video you want to download.

How do I download YouTube to my computer?

You can download YouTube videos on your computer using VLC media player. You can also use WinX YouTube Downloader on PC or MacX YouTube Downloader on Mac. To download YouTube videos on the mobile app, simply tap "Download" on the selected video.


3 Answers

I have find the solution for download you tube videos using c# code.

First need install the "libvideo" on NuGet package manager console in visual studio.

Here Fire this command on package manger console :

Install-Package VideoLibrary

First add this namespace on top in your controller :

using VideoLibrary;

Now here just write just code and pass the url link :

var VedioUrl = "https://www.youtube.com/embed/" + objYouTube.VideoID + ".mp4";
var youTube = YouTube.Default;
var video = youTube.GetVideo(VedioUrl);
System.IO.File.WriteAllBytes(Server.MapPath("~/youtube/" + video.FullName + ".mp4"), video.GetBytes());
like image 130
coderwill Avatar answered Nov 07 '22 04:11

coderwill


You can embed youtube-dl in your app.
It provides extensive Youtube download options.

Basically, you do something like this.

using System;
using System.Diagnostics;
using System.ComponentModel;

namespace MyProcessSample
{
    class MyProcess
    {
        public static void Main()
        {
            Process myProcess = new Process();

            try
            {
                myProcess.StartInfo.UseShellExecute = false;
                myProcess.StartInfo.FileName = @"yourpath\youtube-dl.exe";
                myProcess.StartInfo.CreateNoWindow = false;
                myProcess.StartInfo.Arguments = "https://www.youtube.com/watch?v=KFqrp4KSxio";
                myProcess.Start();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
    }
}

You can use this C# wapper for youtube-dl.
You can extend it to suite your needs.

Process Class

like image 30
Acha Bill Avatar answered Nov 07 '22 05:11

Acha Bill


For a more up to date solution, check out YoutubeExplode. It offers rich API to query and download Youtube videos and, unlike other libraries, is still actively maintained.

like image 2
Tyrrrz Avatar answered Nov 07 '22 06:11

Tyrrrz