Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Download the sound of a youtube video

Tags:

c#

youtube

mp3

I can download a video from youtube but I want the sound only. How can I do that?

Code I have for downloading the video (Using VideoLibrary):

        YouTube youtube = YouTube.Default;
        Video vid = youtube.GetVideo(txt_youtubeurl.Text);
        System.IO.File.WriteAllBytes(source + vid.FullName, vid.GetBytes());
like image 841
Josh C Avatar asked Oct 05 '16 15:10

Josh C


2 Answers

Install the NuGet packages: MediaToolkit and VideoLibrary, it will allow you to do the conversion by file extension.

var source = @"<your destination folder>";
var youtube = YouTube.Default;
var vid = youtube.GetVideo("<video url>");
File.WriteAllBytes(source + vid.FullName, vid.GetBytes());

var inputFile = new MediaFile { Filename = source + vid.FullName };
var outputFile = new MediaFile { Filename = $"{source + vid.FullName}.mp3" };

using (var engine = new Engine())
{
    engine.GetMetadata(inputFile);

    engine.Convert(inputFile, outputFile);
}
like image 99
Stefano d'Antonio Avatar answered Oct 21 '22 17:10

Stefano d'Antonio


The above code works awesome you don't need to download the video first I created this procedure so when rookies like myself see this makes it easier to use. You need the nuget packages MediaToolkit and VideoLibrary.

example url: https://www.youtube.com/watch?v=lzm5llVmR2E example path just needs a path to save file to. just add the name of the mp3 file to save

Hope this helps someone I have tested this code;

private void SaveMP3(string SaveToFolder, string VideoURL, string MP3Name)
{
    var source = @SaveToFolder;
    var youtube = YouTube.Default;
    var vid = youtube.GetVideo(VideoURL);
    File.WriteAllBytes(source + vid.FullName, vid.GetBytes());

    var inputFile = new MediaFile { Filename = source + vid.FullName };
    var outputFile = new MediaFile { Filename = $"{MP3Name}.mp3" };

    using (var engine = new Engine())
    {
        engine.GetMetadata(inputFile);

        engine.Convert(inputFile, outputFile);
    }
}
like image 39
user2180706 Avatar answered Oct 21 '22 17:10

user2180706