1{
2 lib,
3 fetchurl,
4 fetchpatch,
5 buildPythonPackage,
6 zip,
7 ffmpeg,
8 rtmpdump,
9 atomicparsley,
10 pycryptodome,
11 pandoc,
12 # Pandoc is required to build the package's man page. Release tarballs contain a
13 # formatted man page already, though, it will still be installed. We keep the
14 # manpage argument in place in case someone wants to use this derivation to
15 # build a Git version of the tool that doesn't have the formatted man page
16 # included.
17 generateManPage ? false,
18 ffmpegSupport ? true,
19 rtmpSupport ? true,
20 hlsEncryptedSupport ? true,
21 installShellFiles,
22 makeWrapper,
23}:
24
25buildPythonPackage rec {
26
27 pname = "youtube-dl";
28 # The websites youtube-dl deals with are a very moving target. That means that
29 # downloads break constantly. Because of that, updates should always be backported
30 # to the latest stable release.
31 version = "2021.12.17";
32 format = "setuptools";
33
34 src = fetchurl {
35 url = "https://yt-dl.org/downloads/${version}/${pname}-${version}.tar.gz";
36 sha256 = "sha256-nzuZyLd4RVFltFJfIVBehsf/Vl86wxnhlzPYEBlBNd8=";
37 };
38
39 patches = [
40 # Fixes throttling on youtube.com by decoding a "n-parameter". Without the patch
41 # downloads are capped at about 80KiB/s. See, e.g.,
42 #
43 # https://github.com/ytdl-org/youtube-dl/issues/29326
44 #
45 # The patch comes from PR https://github.com/ytdl-org/youtube-dl/pull/30184#issuecomment-1025261055
46 # plus follow-up (1e677567) from https://github.com/ytdl-org/youtube-dl/pull/30582
47 (fetchpatch {
48 name = "fix-youtube-dl-speed.patch";
49 url = "https://github.com/ytdl-org/youtube-dl/compare/57044eacebc6f2f3cd83c345e1b6e659a22e4773...1e677567cd083d43f55daef0cc74e5fa24575ae3.diff";
50 sha256 = "11s0j3w60r75xx20p0x2j3yc4d3yvz99r0572si8b5qd93lqs4pr";
51 })
52 # The above patch may fail to decode the n-parameter (if, say, YouTube is updated). Failure to decode
53 # it blocks the download instead of falling back to the throttled version. The patch below implements
54 # better fallback behaviour.
55 (fetchpatch {
56 name = "avoid-crashing-if-nsig-decode-fails.patch";
57 url = "https://github.com/ytdl-org/youtube-dl/commit/41f0043983c831b7c0c3614340d2f66ec153087b.diff";
58 sha256 = "sha256-a72gWhBXCLjuBBD36PpZ5F/AHBdiBv4W8Wf9g4P/aBY=";
59 })
60 # YouTube changed the n-parameter format in April 2022, so decoder updates are required.
61 (fetchpatch {
62 name = "fix-n-descrambling.patch";
63 url = "https://github.com/ytdl-org/youtube-dl/commit/a0068bd6bec16008bda7a39caecccbf84881c603.diff";
64 sha256 = "sha256-tSuEns4jputa2nOOo6JsFXpK3hvJ/+z1/ymcLsd3A6w=";
65 })
66 ];
67
68 nativeBuildInputs = [
69 installShellFiles
70 makeWrapper
71 ];
72 buildInputs = [ zip ] ++ lib.optional generateManPage pandoc;
73 propagatedBuildInputs = lib.optional hlsEncryptedSupport pycryptodome;
74
75 # Ensure these utilities are available in $PATH:
76 # - ffmpeg: post-processing & transcoding support
77 # - rtmpdump: download files over RTMP
78 # - atomicparsley: embedding thumbnails
79 makeWrapperArgs =
80 let
81 packagesToBinPath = [
82 atomicparsley
83 ]
84 ++ lib.optional ffmpegSupport ffmpeg
85 ++ lib.optional rtmpSupport rtmpdump;
86 in
87 [ ''--prefix PATH : "${lib.makeBinPath packagesToBinPath}"'' ];
88
89 setupPyBuildFlags = [
90 "build_lazy_extractors"
91 ];
92
93 postInstall = ''
94 installShellCompletion youtube-dl.zsh
95 '';
96
97 # Requires network
98 doCheck = false;
99
100 meta = with lib; {
101 homepage = "https://ytdl-org.github.io/youtube-dl/";
102 description = "Command-line tool to download videos from YouTube.com and other sites";
103 longDescription = ''
104 youtube-dl is a small, Python-based command-line program to download
105 videos from YouTube.com and a few more sites. youtube-dl is released to
106 the public domain, which means you can modify it, redistribute it or use
107 it however you like.
108 '';
109 license = licenses.publicDomain;
110 maintainers = with maintainers; [
111 fpletz
112 ];
113 platforms = with platforms; linux ++ darwin;
114 mainProgram = "youtube-dl";
115 knownVulnerabilities = [
116 "youtube-dl is unmaintained, migrate to yt-dlp, if possible"
117 ];
118 };
119}