Repository: yt-dlp/yt-dlp

Issue:
is yt-dlp setting the wrong language on the extracted audio track?
### 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.10.13** ([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 downloading an audio track in a specific language (Italian) but when I use `mediainfo` on the output file I see `Language = English`. Am I missing a parameter?

### 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
# yt-dlp -vU -f "ba[language=it]" https://www.youtube.com/watch?v=21LWzguLAFE

[debug] Command-line config: ['-vU', '-f', 'ba[language=it]', 'https://www.youtube.com/watch?v=21LWzguLAFE']
[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@2023.10.13 [b634ba742] (pip)
[debug] Python 3.11.6 (CPython x86_64 64bit) - Linux-6.5.9-1-default-x86_64-with (OpenSSL 3.1.3 19 Sep 2023)
[debug] exe versions: ffmpeg 6.0 (setts), ffprobe 6.0
[debug] Optional libraries: Cryptodome-3.19.0, brotli-1.1.0, certifi-2023.07.22, mutagen-1.47.0, sqlite3-3.41.2, websockets-12.0
[debug] Proxy map: {}
[debug] Loaded 1890 extractors
[debug] Fetching release info: https://api.github.com/repos/yt-dlp/yt-dlp/releases/latest
Available version: stable@2023.10.13, Current version: stable@2023.10.13
yt-dlp is up to date (stable@2023.10.13)
[youtube] Extracting URL: https://www.youtube.com/watch?v=21LWzguLAFE
[youtube] 21LWzguLAFE: Downloading webpage
[youtube] 21LWzguLAFE: Downloading ios player API JSON
[youtube] 21LWzguLAFE: Downloading android player API JSON
[youtube] 21LWzguLAFE: 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] 21LWzguLAFE: Downloading 1 format(s): 251
[debug] Invoking http downloader on "https://rr1---sn-npa8xgp2-vgqe.googlevideo.com/videoplayback?expire=1699822780&ei=XOhQZevGB-2ZybgPp6a94AU&ip=108.160.192.110&id=o-ALfyWq8hY0b_gUm5H3MrpzhdTB_fKLtpkUtSlgI7Psyk&itag=251&source=youtube&requiressl=yes&mh=vE&mm=31%2C29&mn=sn-npa8xgp2-vgqe%2Csn-vgqsknld&ms=au%2Crdu&mv=m&mvi=1&pl=24&initcwndbps=985000&spc=UWF9f93A4sz-SUlboV7wfdByXJa8VXk&vprv=1&svpuc=1&mime=audio%2Fwebm&gir=yes&clen=33634497&dur=2181.901&lmt=1677805644166615&mt=1699800787&fvip=5&keepalive=yes&fexp=24007246&beids=24350018&c=ANDROID&txp=5432434&sparams=expire%2Cei%2Cip%2Cid%2Citag%2Csource%2Crequiressl%2Cspc%2Cvprv%2Csvpuc%2Cmime%2Cgir%2Cclen%2Cdur%2Clmt&sig=ANLwegAwRQIgMx9TPfYOAH1w66jIltWjNCgmmLIcQtHEDvjWEi2s7lECIQChBITnkriDOqnplZhrfj4IaJrOR8nN03gMbdmS1wLHUA%3D%3D&lsparams=mh%2Cmm%2Cmn%2Cms%2Cmv%2Cmvi%2Cpl%2Cinitcwndbps&lsig=AM8Gb2swRQIhAI3KZjQhcEikKWcdRIAPdTnbqx3-ndCyvp9Oe4cBFH_bAiA7cs-VB46KINJZwajbkyb_Mb2HyZx0l5saGvVyvj8J6g%3D%3D"
[download] Destination: Veppo, il paese dei migranti [21LWzguLAFE].webm
[download] 100% of   32.08MiB in 00:00:00 at 40.04MiB/s



# mediainfo Veppo,\ il\ paese\ dei\ migranti\ \[21LWzguLAFE].webm

General
Complete name                            : Veppo, il paese dei migranti [21LWzguLAFE].webm
Format                                   : WebM
Format version                           : Version 4
File size                                : 32.1 MiB
Duration                                 : 36 min 21 s
Overall bit rate                         : 123 kb/s
Writing application                      : google/video-file
Writing library                          : google/video-file

Audio
ID                                       : 1
Format                                   : Opus
Codec ID                                 : A_OPUS
Duration                                 : 36 min 21 s
Channel(s)                               : 2 channels
Channel layout                           : L R
Sampling rate                            : 48.0 kHz
Bit depth                                : 16 bits
Compression mode                         : Lossy
Language                                 : English
Default                                  : Yes
Forced                                   : No
```



Discussion:
yt-dlp isn't doing any embedding or remuxing in your log; the wrong language metadata is coming directly from youtube.

I was about to suggest that you could manually correct this by adding `--embed-metadata` to your command, but when I tested it, it didn't work.

I looked into why that was, and it turns out that the embed metadata postprocessor only adds language metadata to streams if multiple formats are being merged (and `requested_formats` exists in the info dict). It will not do so when a single format is downloaded.

I think this might be the fix for that:
```diff
diff --git a/yt_dlp/postprocessor/ffmpeg.py b/yt_dlp/postprocessor/ffmpeg.py
index 323f4303c..7c904417b 100644
--- a/yt_dlp/postprocessor/ffmpeg.py
+++ b/yt_dlp/postprocessor/ffmpeg.py
@@ -780,7 +780,7 @@ def add(meta_list, info_list=None):
             yield ('-metadata', f'{name}={value}')
 
         stream_idx = 0
-        for fmt in info.get('requested_formats') or []:
+        for fmt in info.get('requested_formats') or [info]:
             stream_count = 2 if 'none' not in (fmt.get('vcodec'), fmt.get('acodec')) else 1
             lang = ISO639Utils.short2long(fmt.get('language') or '') or fmt.get('language')
             for i in range(stream_idx, stream_idx + stream_count):
```
Thank you for the prompt fix

Resolving patch:
diff --git a/yt_dlp/postprocessor/ffmpeg.py b/yt_dlp/postprocessor/ffmpeg.py
--- a/yt_dlp/postprocessor/ffmpeg.py
+++ b/yt_dlp/postprocessor/ffmpeg.py
@@ -780,7 +780,7 @@ def add(meta_list, info_list=None):
             yield ('-metadata', f'{name}={value}')
 
         stream_idx = 0
-        for fmt in info.get('requested_formats') or []:
+        for fmt in info.get('requested_formats') or [info]:
             stream_count = 2 if 'none' not in (fmt.get('vcodec'), fmt.get('acodec')) else 1
             lang = ISO639Utils.short2long(fmt.get('language') or '') or fmt.get('language')
             for i in range(stream_idx, stream_idx + stream_count):


Test patch: