Skip to content

Unable to download a private video with cookies (400 Bad Request) #852

@ngoquoctoandev

Description

@ngoquoctoandev

Version

6.5

Platform

.NET 9 / Windows 11

Steps to reproduce

Summary

I want to download private videos after having the Youtube cookie file.
I tried to check with the yt-dlp tool and the video can still be downloaded normally.
yt-dlp --cookies "youtube.com_cookies.txt" url
I solved it by reading the content from the cookie file and initializing the Youtube Client to download the video but got an error

Details

Details

  1. Parse cookie file
    private static List<Cookie> ParseCookieFile(string cookieFilePath)
    {
        var lines = File.ReadAllLines(cookieFilePath);

        return (from line in lines
            where !string.IsNullOrWhiteSpace(line) && !line.StartsWith('#')
            select line.Split('\t')
            into parts
            where parts.Length >= 7
            select new Cookie
            {
                Domain = parts[0],
                Path   = parts[2],
                Secure = bool.Parse(parts[3]),
                Name   = parts[5],
                Value  = parts[6]
            }).ToList();
    }

image

  1. Initialize youtube client with cookies
public async Task DownloadPrivateVideoAsync(string videoUrl, FileFormat fileFormat, string cookieFilePath, CancellationToken cancellationToken = default)
{
    var cookies = ParseCookieFile(cookieFilePath);
    var youtube = new YoutubeClient(cookies);
    var request = new YtdlpRequest
    {
        ClassName  = "Private",
        FileFormat = fileFormat,
        LessonName = "Private",
        LessonNo   = 0,
        StartTrim  = 0,
        EndTrim    = 0,
        Url        = videoUrl
    };

    var audioFileName = await DownloadMediaV2Async(youtube, request, Constants.Folder.Videos, cancellationToken).ConfigureAwait(false);

    if (fileFormat == FileFormat.OnlyVideo) audioFileName.DeleteFile();
}
  1. Get video information
private async Task<string> DownloadMediaV2Async(YoutubeClient youtube, YtdlpRequest request, string outputPath, CancellationToken cancellationToken = default)
{
    Video          video          = null!;
    StreamManifest streamManifest = null!;
    await AnsiConsoleHelper.LiveStatusAsync(async _ =>
    {
        var videoUrl = VideoId.Parse(request.Url);
        video          = await youtube.Videos.GetAsync(videoUrl, cancellationToken).ConfigureAwait(false);
        streamManifest = await youtube.Videos.Streams.GetManifestAsync(videoUrl, cancellationToken);
    }, "[darkorange]Getting video info...[/]");

    ...

    return audioFileName;
 }

image

After getting the video information, the following code gives an error.
streamManifest = await youtube.Videos.Streams.GetManifestAsync(videoUrl, cancellationToken);

image

image

Checklist

  • I have looked through existing issues to make sure that this bug has not been reported before
  • I have provided a descriptive title for this issue
  • I have made sure that this bug is reproducible on the latest version of the package
  • I have provided all the information needed to reproduce this bug as efficiently as possible
  • I have sponsored this project
  • I have not read any of the above and just checked all the boxes to submit the issue

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions