Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to download a private video with cookies #852

Open
4 of 6 tasks
ngoquoctoandev opened this issue Dec 20, 2024 · 1 comment
Open
4 of 6 tasks

Unable to download a private video with cookies #852

ngoquoctoandev opened this issue Dec 20, 2024 · 1 comment

Comments

@ngoquoctoandev
Copy link

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
@Tyrrrz Tyrrrz changed the title Unable to download private video Unable to download a private video with cookies Dec 26, 2024
@Tyrrrz
Copy link
Owner

Tyrrrz commented Dec 26, 2024

Are you getting status code 400 or actually 401?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants