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());
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);
}
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);
    }
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With