Repository: yt-dlp/yt-dlp (Python) — Bug Fix

Issue:
[youtube] nsig extraction failed: Some formats may be missing
### DO NOT REMOVE OR SKIP THE ISSUE TEMPLATE

- [X] I understand that I will be **blocked** if I *intentionally* remove or skip any mandatory\* field

### Checklist

- [X] I'm reporting that yt-dlp is broken on a **supported** site
- [X] I've verified that I have **updated yt-dlp to nightly or master** ([update instructions](https://github.com/yt-dlp/yt-dlp#update-channels))
- [X] I've checked that all provided URLs are playable in a browser with the same IP and same login details
- [X] I've checked that all URLs and arguments with special characters are [properly quoted or escaped](https://github.com/yt-dlp/yt-dlp/wiki/FAQ#video-url-contains-an-ampersand--and-im-getting-some-strange-output-1-2839-or-v-is-not-recognized-as-an-internal-or-external-command)
- [X] I've searched [known issues](https://github.com/yt-dlp/yt-dlp/issues/3766) and the [bugtracker](https://github.com/yt-dlp/yt-dlp/issues?q=) for similar issues **including closed ones**. DO NOT post duplicates
- [X] I've read the [guidelines for opening an issue](https://github.com/yt-dlp/yt-dlp/blob/master/CONTRIBUTING.md#opening-an-issue)
- [X] I've read about [sharing account credentials](https://github.com/yt-dlp/yt-dlp/blob/master/CONTRIBUTING.md#are-you-willing-to-share-account-details-if-needed) and I'm willing to share it if required

### Region

_No response_

### Provide a description that is worded well enough to be understood

Since today, randomly with youtube videos I have the error "nsig extraction failed: Some formats may be missing", if I retype the same command directly after then it works.  

### Provide verbose output that clearly demonstrates the problem

- [X] Run **your** yt-dlp command with **-vU** flag added (`yt-dlp -vU <your command line>`)
- [X] If using API, add `'verbose': True` to `YoutubeDL` params instead
- [X] Copy the WHOLE output (starting with `[debug] Command-line config`) and insert it below

### Complete Verbose Output

```shell
[debug] Command-line config: ['https://youtu.be/9dcVOmEQzKA', '-f', 'bestaudio', '--proxy', 'http://user:password@localhost:3002/', '-vU']
[debug] Encodings: locale UTF-8, fs utf-8, pref UTF-8, out utf-8, error utf-8, screen utf-8
[debug] yt-dlp version stable@2024.07.07 from yt-dlp/yt-dlp [b337d2989] (source)
[debug] Lazy loading extractors is disabled
[debug] Git HEAD: 39bc699d2
[debug] Python 3.9.7 (CPython x86_64 64bit) - Linux-5.10.0-30-amd64-x86_64-with-glibc2.31 (OpenSSL 1.1.1w  11 Sep 2023, glibc 2.31)
[debug] exe versions: ffmpeg N-116058-g2902ed25b5-20240630 (setts), ffprobe N-116058-g2902ed25b5-20240630, phantomjs 2.1.1
[debug] Optional libraries: sqlite3-3.34.1
[debug] Proxy map: {'all': 'http://user:password@localhost:3002/'}
[debug] Request Handlers: urllib
[debug] Extractor Plugins: NSigDeno (YoutubeIE)
[debug] Plugin directories: ['/opt/yt-dlp/yt_dlp_plugins']
[debug] Loaded 1834 extractors
[debug] Fetching release info: https://api.github.com/repos/yt-dlp/yt-dlp/releases/latest
Latest version: stable@2024.07.07 from yt-dlp/yt-dlp
yt-dlp is up to date (stable@2024.07.07 from yt-dlp/yt-dlp)
[youtube+NSigDeno] Extracting URL: https://youtu.be/9dcVOmEQzKA
[youtube+NSigDeno] 9dcVOmEQzKA: Downloading webpage
[youtube+NSigDeno] 9dcVOmEQzKA: Downloading ios player API JSON
[debug] [youtube+NSigDeno] Extracting signature function js_b22ef6e7_108
[youtube+NSigDeno] 9dcVOmEQzKA: Downloading player b22ef6e7
[debug] Saving youtube-sigfuncs.js_b22ef6e7_108 to cache
WARNING: [youtube+NSigDeno] 9dcVOmEQzKA: nsig extraction failed: Some formats may be missing
         n = 6C0NBSvskQxbZw3d- ; player = https://www.youtube.com/s/player/b22ef6e7/player_ias.vflset/en_US/base.js
[debug] [youtube+NSigDeno] Unable to extract nsig function code (caused by RegexNotFoundError('Unable to extract \x1b[0;94mInitial JS player n function name\x1b[0m; please report this issue on  https://github.com/yt-dlp/yt-dlp/issues?q= , filling out the appropriate issue template. Confirm you are on the latest version using  yt-dlp -U')); please report this issue on  https://github.com/yt-dlp/yt-dlp/issues?q= , filling out the appropriate issue template. Confirm you are on the latest version using  yt-dlp -U
WARNING: [youtube+NSigDeno] 9dcVOmEQzKA: nsig extraction failed: Some formats may be missing
         n = b6HcntHGkvBLk_FRf ; player = https://www.youtube.com/s/player/b22ef6e7/player_ias.vflset/en_US/base.js
[debug] [youtube+NSigDeno] Extracting signature function js_b22ef6e7_104
[debug] Saving youtube-sigfuncs.js_b22ef6e7_104 to cache
[youtube+NSigDeno] 9dcVOmEQzKA: Downloading m3u8 information
[debug] Sort order given by extractor: quality, res, fps, hdr:12, source, vcodec:vp9.2, channels, acodec, lang, proto
[debug] Formats sorted by: hasvid, ie_pref, quality, res, fps, hdr:12(7), source, vcodec:vp9.2(10), channels, acodec, lang, proto, size, br, asr, vext, aext, hasaud, id
[info] 9dcVOmEQzKA: Downloading 1 format(s): 140
[debug] Invoking http downloader on "https://rr5---sn-nx57ynlk.googlevideo.com/videoplayback?expire=1720483049&ei=iSiMZtauKZ_csfIP0_ag2A0&ip=XXX&id=o-AGE-fExIWW0nN6j_2IGCoIdifX2gJQnMiTmvj0wiHKKv&itag=140&source=youtube&requiressl=yes&xpc=EgVo2aDSNQ%3D%3D&mh=SV&mm=31%2C26&mn=sn-nx57ynlk%2Csn-n4v7snee&ms=au%2Conr&mv=m&mvi=5&pl=24&gcr=us&initcwndbps=3521250&vprv=1&svpuc=1&mime=audio%2Fmp4&rqh=1&gir=yes&clen=3946516&dur=243.809&lmt=1706142022262992&mt=1720461212&fvip=2&keepalive=yes&c=IOS&txp=4532434&sparams=expire%2Cei%2Cip%2Cid%2Citag%2Csource%2Crequiressl%2Cxpc%2Cgcr%2Cvprv%2Csvpuc%2Cmime%2Crqh%2Cgir%2Cclen%2Cdur%2Clmt&sig=AJfQdSswRAIgEKsCMHKjGMjEVO24N2s8LpP_lHITGy6ZDrWCWO471boCIEfPoyBsgxm0Re65OENpT7If1SZj5l9_6cBXFtX4sVj4&lsparams=mh%2Cmm%2Cmn%2Cms%2Cmv%2Cmvi%2Cpl%2Cinitcwndbps&lsig=AHlkHjAwRgIhALYSL1X79BsW1iKNdPR5-jsOyJkFEH5uFGNN6hrh_HQxAiEAuVAo4Szd5JQkvusy7FUFlKO5R86YpYgZfqrtYNt4FNo%3D"
[download] Destination: Eminem - Just Lose It (Official Music Video) [9dcVOmEQzKA].m4a
[download] 100% of    3.76MiB in 00:00:01 at 2.51MiB/s
[FixupM4a] Correcting container of "Eminem - Just Lose It (Official Music Video) [9dcVOmEQzKA].m4a"
[debug] ffmpeg command line: ffmpeg -y -loglevel repeat+info -i 'file:Eminem - Just Lose It (Official Music Video) [9dcVOmEQzKA].m4a' -map 0 -dn -ignore_unknown -c copy -f mp4 -movflags +faststart 'file:Eminem - Just Lose It (Official Music Video) [9dcVOmEQzKA].temp.m4a'
```



Discussion:

Resolving patch:
diff --git a/yt_dlp/extractor/youtube.py b/yt_dlp/extractor/youtube.py
--- a/yt_dlp/extractor/youtube.py
+++ b/yt_dlp/extractor/youtube.py
@@ -3130,7 +3130,8 @@ def _decrypt_nsig(self, s, video_id, player_url):
 
     def _extract_n_function_name(self, jscode):
         funcname, idx = self._search_regex(
-            r'\.get\("n"\)\)&&\(b=(?P<nfunc>[a-zA-Z0-9$]+)(?:\[(?P<idx>\d+)\])?\([a-zA-Z0-9]\)',
+            r'''(?x)(?:\.get\("n"\)\)&&\(b=|b=String\.fromCharCode\(110\),c=a\.get\(b\)\)&&\(c=)
+            (?P<nfunc>[a-zA-Z0-9$]+)(?:\[(?P<idx>\d+)\])?\([a-zA-Z0-9]\)''',
             jscode, 'Initial JS player n function name', group=('nfunc', 'idx'))
         if not idx:
             return funcname


Test patch:
diff --git a/test/test_youtube_signature.py b/test/test_youtube_signature.py
--- a/test/test_youtube_signature.py
+++ b/test/test_youtube_signature.py
@@ -167,6 +167,10 @@
         'https://www.youtube.com/s/player/590f65a6/player_ias.vflset/en_US/base.js',
         '1tm7-g_A9zsI8_Lay_', 'xI4Vem4Put_rOg',
     ),
+    (
+        'https://www.youtube.com/s/player/b22ef6e7/player_ias.vflset/en_US/base.js',
+        'b6HcntHGkvBLk_FRf', 'kNPW6A7FyP2l8A',
+    ),
 ]