Repository: yt-dlp/yt-dlp

Issue:
Downloadling audio files (m4a, mp3) from google drive
### 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 asking a question and **not** reporting a bug or requesting a feature
- [X] I've looked through the [README](https://github.com/yt-dlp/yt-dlp#readme)
- [X] I've verified that I'm running yt-dlp version **2023.09.24** ([update instructions](https://github.com/yt-dlp/yt-dlp#update)) or later (specify commit)
- [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 questions **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)

### Please make sure the question is worded well enough to be understood

I'm using the following python code to download audio files (with m4a and mp3 formats) from google drive:

```python
FILE_URL = "https://drive.google.com/file/d/1Il5LrGlp_iWFNhtgrD449tDUeFuuBWMh/view?usp=sharing"
ydl_opts = {
    'format': 'bestaudio/best',
    'postprocessors': [{  
        'key': 'FFmpegExtractAudio',
        'preferredcodec': 'm4a',
    }],
    'quiet': False,
    'verbose': True,
    'outtmpl': '/tmp/%(id)s.%(ext)s',
}

with yt_dlp.YoutubeDL(ydl_opts) as ydl:
    error_code = ydl.download(FILE_URL)
```

Strangely, when I try a link for the first time, they are downloaded without any issues, but when I try to download the same file using the same URL again (e.g. when I'm changing other parts of my code and I'm in the process of developing my script) I can't download the files and I get the following error (for m4a file):

> [debug] Encodings: locale UTF-8, fs utf-8, pref UTF-8, out UTF-8 (No ANSI), error UTF-8 (No ANSI), screen UTF-8 (No ANSI)
[debug] yt-dlp version stable@2023.09.24 [088add956] (pip) API
[debug] params: {'format': 'bestaudio/best', 'postprocessors': [{'key': 'FFmpegExtractAudio', 'preferredcodec': 'm4a'}], 'quiet': False, 'verbose': True, 'outtmpl': '/tmp/%(id)s.%(ext)s', 'compat_opts': set(), 'http_headers': {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36', 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'Accept-Language': 'en-us,en;q=0.5', 'Sec-Fetch-Mode': 'navigate'}}
[debug] Python 3.9.17 (CPython arm64 64bit) - macOS-13.5.1-arm64-arm-64bit (OpenSSL 3.1.2 1 Aug 2023)
[debug] exe versions: ffmpeg 6.0 (setts), ffprobe 6.0
[debug] Optional libraries: Cryptodome-3.19.0, brotli-1.1.0, certifi-2021.10.08, mutagen-1.47.0, sqlite3-3.42.0, websockets-11.0.3
[debug] Proxy map: {}
[debug] Loaded 1886 extractors

> [GoogleDrive] Extracting URL: https://drive.google.com/file/d/1Il5LrGlp_iWFNhtgrD449tDUeFuuBWMh/view?usp=sharing
[GoogleDrive] 1Il5LrGlp_iWFNhtgrD449tDUeFuuBWMh: Downloading video webpage

> ERROR: 1Il5LrGlp_iWFNhtgrD449tDUeFuuBWMh: An extractor error has occurred. (caused by KeyError('50')); 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
  File "/opt/homebrew/lib/python3.9/site-packages/yt_dlp/extractor/common.py", line 715, in extract
    ie_result = self._real_extract(url)
  File "/opt/homebrew/lib/python3.9/site-packages/yt_dlp/extractor/googledrive.py", line 197, in _real_extract
    'ext': self._FORMATS_EXT[format_id],
KeyError: '50'

And almost the same error with `KeyError: '140'` for mp3 files. I looked into the [google drive extractor part of the code](https://github.com/yt-dlp/yt-dlp/blob/master/yt_dlp/extractor/googledrive.py#L57C5-L57C17), and the answer to my question probably is "yt-dlp doesn't support downloading audio files from google drive", but since it is able to download them sometimes, I thought I'm missing something probably.

### 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
(pasted in the question above)
```



Discussion:
seems like we just need to add IDs for audio file extensions, something like this:

```diff
diff --git a/yt_dlp/extractor/googledrive.py b/yt_dlp/extractor/googledrive.py
index 2fdec20f6..b51a2fa1a 100644
--- a/yt_dlp/extractor/googledrive.py
+++ b/yt_dlp/extractor/googledrive.py
@@ -70,7 +70,9 @@ class GoogleDriveIE(InfoExtractor):
         '44': 'webm',
         '45': 'webm',
         '46': 'webm',
+        '50': 'm4a',
         '59': 'mp4',
+        '140': 'mp3',
     }
     _BASE_URL_CAPTIONS = 'https://drive.google.com/timedtext'
     _CAPTIONS_ENTRY_TAG = {
```
Yeah exactly. I was wondering if this is an intentional choice (for some technical reasons) to not allow downloading audio files from google drive or not.
IMO yt-dlp shouldn't fail completely if a format extension isn't known

I'm not that familiar with the gdrive extractor though
FWIW I was unable to download a specific video file from Drive until I patched `googledrive.py` with these IDs: 
```diff
diff --git a/yt_dlp/extractor/googledrive.py b/yt_dlp/extractor/googledrive.py
index 2fdec20f6..4bb96f22e 100644
--- a/yt_dlp/extractor/googledrive.py
+++ b/yt_dlp/extractor/googledrive.py
@@ -71,6 +71,9 @@ class GoogleDriveIE(InfoExtractor):
         '45': 'webm',
         '46': 'webm',
         '59': 'mp4',
+        '134': 'mp4',
+        '136': 'mp4',
+        '140': 'mp4',
     }
     _BASE_URL_CAPTIONS = 'https://drive.google.com/timedtext'
     _CAPTIONS_ENTRY_TAG = {
```
Not sure where the `134` came from but it was the first `KeyError` reported by `yt-dlp stable@2023.10.07`. According to the "stats for nerds" option in the Google Drive embedded video player, the video format was `avc1` (`136`) with audio `mp4a` (`140`). 
I've got the same error with an m4a error : 
```
# ./yt-dlp_linux --version
2024.04.09.170053
```

I anonymize the URL because it's note mine.

```
./yt-dlp_linux -v "https://drive.google.com/file/d/1P3ZwdrIDp7z6g8cAd4NgkdXXX/view"
[debug] Command-line config: ['-v', 'https://drive.google.com/file/d/1P3ZwdrIDp7z6g8cAd4NgkdlvdXXX/view']
[debug] Encodings: locale UTF-8, fs utf-8, pref UTF-8, out utf-8, error utf-8, screen utf-8
[debug] yt-dlp version master@2024.04.09.170053 from yt-dlp/yt-dlp-master-builds [ff0779267] (linux_exe)
[debug] Python 3.10.14 (CPython x86_64 64bit) - Linux-5.15.0-101-generic-x86_64-with-glibc2.35 (OpenSSL 3.2.1 30 Jan 2024, glibc 2.35)
[debug] exe versions: ffmpeg 4.4.2 (setts), ffprobe 4.4.2, rtmpdump 2.4
[debug] Optional libraries: Cryptodome-3.19.0, brotli-1.1.0, certifi-2024.02.02, mutagen-1.47.0, requests-2.31.0, secretstorage-3.3.3, sqlite3-3.45.2, urllib3-2.2.1, websockets-12.0
[debug] Proxy map: {}
[debug] Request Handlers: urllib, requests, websockets
[debug] Loaded 1810 extractors
[GoogleDrive] Extracting URL: https://drive.google.com/file/d/1P3ZwdrIDp7z6g8cAd4NgkdlvdXXX/view
[GoogleDrive] 1P3ZwdrIDp7z6g8cAd4Ngkdlvd5ZTT8b5: Downloading video webpage
ERROR: 1P3ZwdrIDp7z6g8cAd4NgkdlvdXXX: An extractor error has occurred. (caused by KeyError('50')); 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
  File "yt_dlp/extractor/common.py", line 734, in extract
  File "yt_dlp/extractor/googledrive.py", line 200, in _real_extract
KeyError: '50'
```

```
$ file XXX.m4a 
XXX.m4a: ISO Media, Apple iTunes ALAC/AAC-LC (.M4A) Audio
```
None of the posted URLs work anymore. Someone needs to provide working URLs for this to be fixed
Very strange.
I upload a personal file on a drive, and it works fine : 

```
/yt-dlp_linux -v https://drive.google.com/file/d/1ShMdrxXiRyzKHnkvQtTpC9POqWxdoZM9/view
[debug] Command-line config: ['-v', 'https://drive.google.com/file/d/1ShMdrxXiRyzKHnkvQtTpC9POqWxdoZM9/view']
[debug] Encodings: locale UTF-8, fs utf-8, pref UTF-8, out utf-8, error utf-8, screen utf-8
[debug] yt-dlp version master@2024.04.09.170053 from yt-dlp/yt-dlp-master-builds [ff0779267] (linux_exe)
[debug] Python 3.10.14 (CPython x86_64 64bit) - Linux-5.15.0-101-generic-x86_64-with-glibc2.35 (OpenSSL 3.2.1 30 Jan 2024, glibc 2.35)
[debug] exe versions: ffmpeg 4.4.2 (setts), ffprobe 4.4.2, rtmpdump 2.4
[debug] Optional libraries: Cryptodome-3.19.0, brotli-1.1.0, certifi-2024.02.02, mutagen-1.47.0, requests-2.31.0, secretstorage-3.3.3, sqlite3-3.45.2, urllib3-2.2.1, websockets-12.0
[debug] Proxy map: {}
[debug] Request Handlers: urllib, requests, websockets
[debug] Loaded 1810 extractors
[GoogleDrive] Extracting URL: https://drive.google.com/file/d/1ShMdrxXiRyzKHnkvQtTpC9POqWxdoZM9/view
[GoogleDrive] 1ShMdrxXiRyzKHnkvQtTpC9POqWxdoZM9: Downloading video webpage
[GoogleDrive] 1ShMdrxXiRyzKHnkvQtTpC9POqWxdoZM9: Requesting source file
[debug] Formats sorted by: hasvid, ie_pref, lang, quality, res, fps, hdr:12(7), vcodec:vp9.2(10), channels, acodec, size, br, asr, proto, vext, aext, hasaud, source, id
[debug] Default format spec: bestvideo*+bestaudio/best
[info] 1ShMdrxXiRyzKHnkvQtTpC9POqWxdoZM9: Downloading 1 format(s): source
[debug] Invoking http downloader on "https://drive.usercontent.google.com/download?id=1ShMdrxXiRyzKHnkvQtTpC9POqWxdoZM9&export=download&confirm=t"
[download] Destination: mix_audio.m4a [1ShMdrxXiRyzKHnkvQtTpC9POqWxdoZM9].m4a
[download] 100% of   14.65MiB in 00:00:01 at 9.81MiB/s
```

I cannot send the real URL because the file is not mine, and contents personal data.

Resolving patch:
diff --git a/yt_dlp/extractor/googledrive.py b/yt_dlp/extractor/googledrive.py
--- a/yt_dlp/extractor/googledrive.py
+++ b/yt_dlp/extractor/googledrive.py
@@ -1,9 +1,11 @@
 import re
 
 from .common import InfoExtractor
+from .youtube import YoutubeIE
 from ..compat import compat_parse_qs
 from ..utils import (
     ExtractorError,
+    bug_reports_message,
     determine_ext,
     extract_attributes,
     get_element_by_class,
@@ -38,6 +40,17 @@ class GoogleDriveIE(InfoExtractor):
             'duration': 45,
             'thumbnail': 'https://drive.google.com/thumbnail?id=0ByeS4oOUV-49Zzh4R1J6R09zazQ',
         }
+    }, {
+        # has itag 50 which is not in YoutubeIE._formats (royalty Free music from 1922)
+        'url': 'https://drive.google.com/uc?id=1IP0o8dHcQrIHGgVyp0Ofvx2cGfLzyO1x',
+        'md5': '322db8d63dd19788c04050a4bba67073',
+        'info_dict': {
+            'id': '1IP0o8dHcQrIHGgVyp0Ofvx2cGfLzyO1x',
+            'ext': 'mp3',
+            'title': 'My Buddy - Henry Burr - Gus Kahn - Walter Donaldson.mp3',
+            'duration': 184,
+            'thumbnail': 'https://drive.google.com/thumbnail?id=1IP0o8dHcQrIHGgVyp0Ofvx2cGfLzyO1x',
+        },
     }, {
         # video can't be watched anonymously due to view count limit reached,
         # but can be downloaded (see https://github.com/ytdl-org/youtube-dl/issues/14046)
@@ -58,22 +71,8 @@ class GoogleDriveIE(InfoExtractor):
         'only_matching': True,
     }]
     _FORMATS_EXT = {
-        '5': 'flv',
-        '6': 'flv',
-        '13': '3gp',
-        '17': '3gp',
-        '18': 'mp4',
-        '22': 'mp4',
-        '34': 'flv',
-        '35': 'flv',
-        '36': '3gp',
-        '37': 'mp4',
-        '38': 'mp4',
-        '43': 'webm',
-        '44': 'webm',
-        '45': 'webm',
-        '46': 'webm',
-        '59': 'mp4',
+        **{k: v['ext'] for k, v in YoutubeIE._formats.items() if v.get('ext')},
+        '50': 'm4a',
     }
     _BASE_URL_CAPTIONS = 'https://drive.google.com/timedtext'
     _CAPTIONS_ENTRY_TAG = {
@@ -194,10 +193,13 @@ def get_value(key):
                 if len(fmt_stream_split) < 2:
                     continue
                 format_id, format_url = fmt_stream_split[:2]
+                ext = self._FORMATS_EXT.get(format_id)
+                if not ext:
+                    self.report_warning(f'Unknown format {format_id}{bug_reports_message()}')
                 f = {
                     'url': lowercase_escape(format_url),
                     'format_id': format_id,
-                    'ext': self._FORMATS_EXT[format_id],
+                    'ext': ext,
                 }
                 resolution = resolutions.get(format_id)
                 if resolution:
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
@@ -1171,7 +1171,7 @@ class YoutubeIE(YoutubeBaseInfoExtractor):
         r'/(?P<id>[a-zA-Z0-9_-]{8,})/player(?:_ias\.vflset(?:/[a-zA-Z]{2,3}_[a-zA-Z]{2,3})?|-plasma-ias-(?:phone|tablet)-[a-z]{2}_[A-Z]{2}\.vflset)/base\.js$',
         r'\b(?P<id>vfl[a-zA-Z0-9_-]+)\b.*?\.js$',
     )
-    _formats = {
+    _formats = {  # NB: Used in YoutubeWebArchiveIE and GoogleDriveIE
         '5': {'ext': 'flv', 'width': 400, 'height': 240, 'acodec': 'mp3', 'abr': 64, 'vcodec': 'h263'},
         '6': {'ext': 'flv', 'width': 450, 'height': 270, 'acodec': 'mp3', 'abr': 64, 'vcodec': 'h263'},
         '13': {'ext': '3gp', 'acodec': 'aac', 'vcodec': 'mp4v'},


Test patch: