{"record":{"id":"b11638f04c40911b","repo":"ytdl-org/youtube-dl","slug":"unable-to-extract-nsig-function-code","errorCode":null,"errorMessage":"Unable to extract nsig function code","messagePattern":"Unable to extract nsig function code","errorType":"exception","errorClass":"ExtractorError","httpStatus":null,"severity":"error","filePath":"youtube_dl/extractor/youtube.py","lineNumber":1991,"sourceCode":"            self._extract_signature_function, 'sig', player_url, self._signature_cache_id(s))\n        func = extract_sig(video_id, player_url, s)\n        self._print_sig_code(func, s)\n        return func(s)\n\n    # from yt-dlp\n    # See also:\n    # 1. https://github.com/ytdl-org/youtube-dl/issues/29326#issuecomment-894619419\n    # 2. https://code.videolan.org/videolan/vlc/-/blob/4fb284e5af69aa9ac2100ccbdd3b88debec9987f/share/lua/playlist/youtube.lua#L116\n    # 3. https://github.com/ytdl-org/youtube-dl/issues/30097#issuecomment-950157377\n    def _decrypt_nsig(self, n, video_id, player_url):\n        \"\"\"Turn the encrypted n field into a working signature\"\"\"\n        if player_url is None:\n            raise ExtractorError('Cannot decrypt nsig without player_url')\n\n        try:\n            jsi, player_id, func_code = self._extract_n_function_code(video_id, player_url)\n        except ExtractorError as e:\n            raise ExtractorError('Unable to extract nsig function code', cause=e)\n        if self.get_param('youtube_print_sig_code'):\n            self.to_screen('Extracted nsig function from {0}:\\n{1}\\n'.format(\n                player_id, func_code[1]))\n\n        try:\n            extract_nsig = self._cached(self._extract_n_function_from_code, 'nsig func', player_url)\n            ret = extract_nsig(jsi, func_code)(n)\n        except JSInterpreter.Exception as e:\n            self.report_warning(\n                '%s (%s %s)' % (\n                    'Unable to decode n-parameter: expect download to be blocked or throttled',\n                    error_to_compat_str(e),\n                    traceback.format_exc()),\n                video_id=video_id)\n            return\n\n        self.write_debug('Decrypted nsig {0} => {1}'.format(n, ret))\n        return ret","sourceCodeStart":1973,"sourceCodeEnd":2009,"githubUrl":"https://github.com/ytdl-org/youtube-dl/blob/956b8c585591b401a543e409accb163eeaaa1193/youtube_dl/extractor/youtube.py#L1973-L2009","documentation":"A wrapping error raised in _decrypt_nsig when _extract_n_function_code fails for any reason (the original ExtractorError is chained as cause). _extract_n_function_code downloads the player JS and locates the nsig function; failure means the code could not be fetched or the function pattern was not found. The cause chain carries the precise underlying reason.","triggerScenarios":"Calling _decrypt_nsig with a valid player_url where downloading the base.js fails (network, 404 after a player rollout) or the regex/JS traversal used to find the n function no longer matches YouTube's obfuscated code.","commonSituations":"YouTube changes the shape/location of the nsig function in player JS; an old youtube-dl against a new player; transient fetch failures of /s/player/.../base.js; datacenter IPs getting blocked from player JS.","solutions":["Update youtube-dl (or switch to yt-dlp) — nsig extraction patterns are among the most frequently patched parts of the extractor.","Inspect the cause exception in the chain (raise from) to distinguish 'download failed' (network/404) from 'regex did not match' (pattern change).","Retry after some time if the player rollout is transient — YouTube often serves mixed player versions during rollouts.","For library users, catch this and fall back to using the un-decrypted URL (throttled download) rather than failing the whole extraction."],"exampleFix":null,"handlingStrategy":"fallback","validationCode":null,"typeGuard":null,"tryCatchPattern":"try:\n    jsi, player_id, func_code = self._extract_n_function_code(video_id, player_url)\nexcept ExtractorError as e:\n    # keep the original URL (throttled) instead of failing extraction\n    self.report_warning('nsig extraction failed: %s' % error_to_compat_str(e))\n    return None","preventionTips":["Check e.cause to distinguish JS download failure (retry/update) from pattern mismatch (update required).","Cache extracted nsig functions keyed by player_url to reduce exposure to transient fetch failures."],"tags":["youtube","nsig","player-js","site-change"],"backgroundTag":null,"analyzedSha":"956b8c585591b401a543e409accb163eeaaa1193","analyzedAt":"2026-08-14T18:59:47.863Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}