Setting the video quality for an extracted YouTube video using 'libvideo' .NET library

Viewed 313

I need help with a code to download video with better video quality 720p/1080p including audio using 'libvideo'.NET library.

I have used 'GetVideo' in the code but the downloaded video is defaulted with '360p' resolution. So I am trying out this in the code (max resolution is set as '1080p').

var videoInfos = youTube.GetAllVideos(link);
var maxResolution = videoInfos.FirstOrDefault(v => v.Resolution == 1080);

But how do I use 'GetAllVideos' to save the video with the resolution that I have set it in the code.

enter image description here

https://github.com/omansak/libvideo

1 Answers

I solved it this, way. I download the high res video. And then download the video with Audio, extract the Audio with the MediaToolkit Nuget and put it together with FFmpeg For the mp3 Tags i used TagLibSharp

private async Task<string> Download(string Url, string Title, string Interpreter, bool AudioForVideo = false)
{
    string audioPath = "";
    try
    {
        YouTubeVideo video;
        if (AudioForVideo || !cbDownloadVideo.Checked)
        {
            video = youtube.GetVideo(Url);
        }
        else
        {
            IEnumerable<YouTubeVideo> videos = youtube.GetAllVideos(Url);
            video = videos.OrderByDescending(o => o.Resolution).ThenByDescending(o => o.Fps).First();

        }

        string fileName = video.FullName;

        if (Title != "")
        {
            fileName = $"{Title}.mp4";
        }
        if (AudioForVideo)
        {
            fileName = fileName.Replace(".mp4", "_temp.mp4");
        }
        string videoPath = Path.Combine(Settings.Default.DownloadDirectory, fileName);

        File.WriteAllBytes(videoPath, video.GetBytes());

        if (!cbDownloadVideo.Checked || AudioForVideo)
        {
            audioPath = videoPath.Replace(".mp4", ".mp3");
            MediaFile inputFile = new(videoPath);
            MediaFile outputFile = new(audioPath);
            using (Engine engine = new())
            {
                engine.GetMetadata(inputFile);
                engine.Convert(inputFile, outputFile);
            }
            File.Delete(videoPath);
            if (!AudioForVideo)
            {
                TagLib.File mFile = TagLib.File.Create(audioPath);
                mFile.Tag.Title = fileName.Replace(".mp4", "");
                mFile.Tag.Artists = new string[] { Interpreter };
                mFile.Save();
            }
        }
        else
        {
            if (video.AudioFormat == AudioFormat.Unknown)
            {
                string audioInputPath = await Download(Url, Title, Interpreter, true);
                string tempVideoPath = videoPath.Replace(".mp4", "_output.mp4");
                FFMpeg.ReplaceAudio(videoPath, audioInputPath, videoPath.Replace(".mp4", "_output.mp4"));
                File.Delete(videoPath);
                File.Move(tempVideoPath, videoPath);
                File.Delete(audioInputPath);
            }
        }
        exception = "";
    }
    catch (Exception ex)
    {
        exception = ex.Message;
    }
    return audioPath;
}
Related