C#下载YouTube视频的声音

时间:2016-10-05 15:17:35

标签: c# youtube mp3

我可以从youtube下载视频,但我只想要声音。我怎么能这样做?

我下载视频的代码(使用VideoLibrary):

        YouTube youtube = YouTube.Default;
        Video vid = youtube.GetVideo(txt_youtubeurl.Text);
        System.IO.File.WriteAllBytes(source + vid.FullName, vid.GetBytes());

5 个答案:

答案 0 :(得分:18)

安装 NuGet 软件包:MediaToolkitVideoLibrary,它允许您通过文件扩展名进行转换。

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);
}

答案 1 :(得分:2)

基于此主题,我开发了一个简单而又笨的程序来下载youtube播放列表。希望这对某人有帮助。只是一个Main.cs文件:Youtube Playlist Downloader - Mp4 & Mp3

答案 2 :(得分:1)

我喜欢使用方法的想法。我尝试了SaveMP3(),但遇到了一些问题。 这为我工作:`

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

            var inputFile = new MediaFile { Filename = Path.Combine(source, vid.FullName) };
            var outputFile = new MediaFile { Filename = Path.Combine(source , $"{MP3Name}.mp3")  };

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


                engine.Convert(inputFile, outputFile);
            }


            File.Delete(Path.Combine(source, vid.FullName));

        }

`

答案 3 :(得分:0)

上面的代码非常棒,您无需先下载视频,而我创建了此过程,因此当像我这样的菜鸟看到它时,它更易于使用。 请参阅上面有关nuget软件包需求的注释。

示例网址:https://www.youtube.com/watch?v=lzm5llVmR2E 示例路径只需要一个保存文件的路径。 只需添加要保存的mp3文件的名称

希望这对我测试过此代码的人有所帮助;

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);
        }
    }

答案 4 :(得分:0)

好吧,找到了一种更好的方法,上面的代码没有将音频发布给他人,因此没有标准化。 首先添加Nuget程序包:https://www.nuget.org/packages/NReco.VideoConverter/ 将MP4转换为MP3

 // Client
        var client = new YoutubeClient();
        var videoId = NormalizeVideoId(txtFileURL.Text);
        var video = await client.GetVideoAsync(videoId);
        var streamInfoSet = await client.GetVideoMediaStreamInfosAsync(videoId);
        // Get the best muxed stream
        var streamInfo = streamInfoSet.Muxed.WithHighestVideoQuality();
        // Compose file name, based on metadata
        var fileExtension = streamInfo.Container.GetFileExtension();
        var fileName = $"{video.Title}.{fileExtension}";
        // Replace illegal characters in file name
        fileName = RemoveIllegalFileNameChars(fileName);
        tmrVideo.Enabled = true;
        // Download video
        txtMessages.Text = "Downloading Video please wait ... ";

        //using (var progress = new ProgressBar())
        await client.DownloadMediaStreamAsync(streamInfo, fileName);

        // Add Nuget package: https://www.nuget.org/packages/NReco.VideoConverter/ To Convert MP4 to MP3
        if (ckbAudioOnly.Checked)
        {
            var Convert = new NReco.VideoConverter.FFMpegConverter();
            String SaveMP3File = MP3FolderPath + fileName.Replace(".mp4", ".mp3");
            Convert.ConvertMedia(fileName, SaveMP3File, "mp3");
            //Delete the MP4 file after conversion
            File.Delete(fileName);
            LoadMP3Files();
            txtMessages.Text = "File Converted to MP3";
            tmrVideo.Enabled = false;
            txtMessages.BackColor = Color.White;
            if (ckbAutoPlay.Checked) { PlayFile(SaveMP3File); }
            return;
        }