下载youtube视频的声音

本文关键字:声音 视频 youtube 下载 | 更新日期: 2023-09-27 18:11:35

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

代码我有下载视频(使用videlibrary):
        YouTube youtube = YouTube.Default;
        Video vid = youtube.GetVideo(txt_youtubeurl.Text);
        System.IO.File.WriteAllBytes(source + vid.FullName, vid.GetBytes());

下载youtube视频的声音

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

上面的代码工作棒极了,你不需要下载视频首先我创建了这个过程,所以当像我这样的新手看到这使它更容易使用。需要安装MediaToolkitVideoLibrary两个软件包

示例url: 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);
    }
}

基于这个主题,我开发了一个简单而愚蠢的程序来下载youtube播放列表。希望这能帮助到一些人。它只是一个Main.cs文件:Youtube播放列表下载器- Mp4 &Mp3

Ok找到了一个更好的方法,上面的代码没有为其他人发布音频。首先添加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;
        }

我喜欢使用方法的想法。我尝试了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));
        }

"