core: Media Extractor fails on YouTube links

The problem

In a nutshell, Media Extractor is raising an error when it is fed a YouTube link. This is most likely related to a warning raised yt-dlp that seems to be resolved with the latest 2024.7.9 version. HA is currently using (defining) 2024.07.01 as the version required.

What version of Home Assistant Core has the issue?

core-2024.7.2

What was the last working version of Home Assistant Core?

No response

What type of installation are you running?

Home Assistant OS

Integration causing the issue

media_extractor

Link to integration documentation on our website

No response

Diagnostics information

No response

Example YAML snippet

No response

Anything in the logs that might be useful for us?

From the Logs:


[youtube] oav5D6X0Ecs: nsig extraction failed: Some formats may be missing n = VFfWJyJN9QPW58hrb ; player = https://www.youtube.com/s/player/b22ef6e7/player_ias.vflset/en_US/base.js
[youtube] oav5D6X0Ecs: nsig extraction failed: Some formats may be missing n = XDpRT24b54AU2WZ0E ; player = https://www.youtube.com/s/player/b22ef6e7/player_ias.vflset/en_US/base.js
[youtube] pjVMnq-v4xs: nsig extraction failed: Some formats may be missing n = ZCR-3GwwmEpHNStD0 ; player = https://www.youtube.com/s/player/b22ef6e7/player_ias.vflset/en_US/base.js
[youtube] pjVMnq-v4xs: nsig extraction failed: Some formats may be missing n = rQP2yYXlgGFgj_luy ; player = https://www.youtube.com/s/player/b22ef6e7/player_ias.vflset/en_US/base.js

Followed by:

Logger: homeassistant.helpers.script.websocket_api_script
Source: helpers/script.py:527
First occurred: 14:46:23 (2 occurrences)
Last logged: 14:47:58

websocket_api script: Error executing script. Unexpected error for call_service at pos 1: list index out of range
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 527, in _async_step
    await getattr(self, handler)()
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 764, in _async_call_service_step
    response_data = await self._async_run_long_action(
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 727, in _async_run_long_action
    return await long_task
           ^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/core.py", line 2731, in async_call
    response_data = await coro
                    ^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/core.py", line 2774, in _execute_service
    return await target(service_call)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/media_extractor/__init__.py", line 123, in extract_media_url
    url = get_best_stream_youtube(selected_media["formats"])
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/media_extractor/__init__.py", line 315, in get_best_stream_youtube
    return get_best_stream(
           ^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/media_extractor/__init__.py", line 306, in get_best_stream
    return cast(str, formats[len(formats) - 1]["url"])
                     ~~~~~~~^^^^^^^^^^^^^^^^^^
IndexError: list index out of range


### Additional information

Likely just needs a bump to the newest version.

I used an older version of yt-dlp (binary) and see the warning in its output but the download still worked.
I updated yt-dlp to the latest version 2024.7.9 and ran the same command again but didn't receive a warning so I _assume_ this issue was fixed in yt-dlp.

About this issue

  • Original URL
  • State: open
  • Created 2 years ago
  • Reactions: 1
  • Comments: 44 (7 by maintainers)

Most upvoted comments

I think this error has returned. I am running 2025.4.3 and media extractor is throwing this error again:-

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/core.py", line 2794, in async_call
    response_data = await coro
                    ^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/core.py", line 2837, in _execute_service
    return await target(service_call)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/script/__init__.py", line 718, in _service_handler
    response = await self._async_start_run(
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        variables=service.data, context=service.context, wait=True
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File "/usr/src/homeassistant/homeassistant/components/script/__init__.py", line 674, in _async_start_run
    script_result = await coro
                    ^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/script/__init__.py", line 707, in _async_run
    return await self.script.async_run(script_vars, context)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 1828, in async_run
    return await asyncio.shield(create_eager_task(run.async_run()))
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 460, in async_run
    await self._async_step(log_exceptions=False)
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 526, in _async_step
    self._handle_exception(
    ~~~~~~~~~~~~~~~~~~~~~~^
        ex, continue_on_error, self._log_exceptions or log_exceptions
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 556, in _handle_exception
    raise exception
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 524, in _async_step
    await getattr(self, handler)()
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 1009, in _async_step_call_service
    response_data = await self._async_run_long_action(
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    ...<9 lines>...
    )
    ^
  File "/usr/src/homeassistant/homeassistant/helpers/script.py", line 624, in _async_run_long_action
    return await long_task
           ^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/core.py", line 2794, in async_call
    response_data = await coro
                    ^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/core.py", line 2844, in _execute_service
    return await self._hass.async_add_executor_job(target, service_call)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.13/concurrent/futures/thread.py", line 59, in run
    result = self.fn(*self.args, **self.kwargs)
  File "/usr/src/homeassistant/homeassistant/components/media_extractor/__init__.py", line 95, in play_media
    MediaExtractor(hass, config.get(DOMAIN, {}), call.data).extract_and_send()
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^
  File "/usr/src/homeassistant/homeassistant/components/media_extractor/__init__.py", line 169, in extract_and_send
    self.call_media_player_service(stream_selector, entity_id)
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/media_extractor/__init__.py", line 229, in call_media_player_service
    stream_url = stream_selector(stream_query)
  File "/usr/src/homeassistant/homeassistant/components/media_extractor/__init__.py", line 216, in stream_selector
    return get_best_stream_youtube(requested_stream["formats"])
  File "/usr/src/homeassistant/homeassistant/components/media_extractor/__init__.py", line 278, in get_best_stream_youtube
    return get_best_stream(
        [
    ...<4 lines>...
        ]
    )
  File "/usr/src/homeassistant/homeassistant/components/media_extractor/__init__.py", line 269, in get_best_stream
    return cast(str, formats[len(formats) - 1]["url"])
                     ~~~~~~~^^^^^^^^^^^^^^^^^^
IndexError: list index out of range

Given I’m stopped from playing anything from YouTube while the integration gets a version bump, I decided to modify my own manifest.json to get it working for me.

I know it won’t survive an update and is frowned upon but I assume the next version released will see the bump to yt-dlp anyway.

For anyone interested and skilled enough in doing this that’s using Home Assistant OS version. Here’s what I did.

  • Take a full backup.
  • Install Advanced SSH & Web Termninal add-on if it isn’t already. Ensure Protection mode is off and create a username and password in the Configuration section. Start the add-on.
  • SSH to the Home Assistant server IP address and log in with the username and password.
  • Run the following which simply reads media_extractor’s current manifest.json file out to console - allowing you to see which version of yt-dlp is being used by Home Assistant. docker exec -it homeassistant cat /usr/src/homeassistant/homeassistant/components/media_extractor/manifest.json
  • Run the following to modify the manifest.json to use the latest version of yt-dlp. docker exec -it homeassistant sed -i 's/\["yt-dlp==[0-9]\{4\}\.[0-9]\{2\}\.[0-9]\{2\}"\]/\["yt-dlp==2024.07.25"\]/' /usr/src/homeassistant/homeassistant/components/media_extractor/manifest.json
  • Run this again to check the version was updated. If it wasn’t or the file looks completely broken…restore from the backup you took earlier. docker exec -it homeassistant cat /usr/src/homeassistant/homeassistant/components/media_extractor/manifest.json
  • Restart Home Assistant.

Be aware that the above will not survive a version upgrade to Home Assistant but, as I said, I expect this will be resolved in the next release anyway (didn’t get it with 2024.7.4 from today so I needed to do this from my point of view).

I know this is frowned on etc. but for anyone stuck and needing a workaround now without waiting for the next version, this should get media_extractor.play_media working again with YouTube links.

I am repeating myself…

There are workarounds above if you absolutely must get it working before HA core gets a version bump.

I’m using a VM in Proxmox using this script https://tteck.github.io/Proxmox/#home-assistant-os-vm

Is there any option to modify this file with “File editor” addon or similar?

Bumped that version for the beta