home-assistant / core

:house_with_garden: Open source home automation that puts local control and privacy first.
https://www.home-assistant.io
Apache License 2.0
73.56k stars 30.74k forks source link

Media Extractor integration doesn't play YouTube audio to Sonos #103984

Closed Sleinous closed 4 months ago

Sleinous commented 12 months ago

The problem

A previous issue was raised and fixed by @joostlek regarding playing youtube audio from media_extractor It seems i'm experiencing a similar issue whereby my Sonos device will not play audio from youtube links. If my understanding is correct media_extractor should search for audio in youtube links, however, despite modifying media_extractor configuration.yaml it still tries to play the video mp4 url to sonos (which obviously fails given it's an audio device) --> Error calling SonosMediaPlayerEntity._play_media on media_player.sonos_chambre: UPnP Error 714 received: Illegal MIME-Type

I tried the following configuration.yaml for media_extractor:

media_extractor:
  default_query: best
  customize:
    media_player.sonos_chambre:
      music: bestaudio[ext=mp3]

also tried : bestaudio[ext=mp3] bestaudio[ext=m4a] bestaudio[ext=ogg]

Here's the error log output Service Call :

Error executing service: <ServiceCall media_player.play_media (c:01HF7SPG1HCYCF2CMBD0JQDNBD): extra=, entity_id=['media_player.sonos_chambre'], media_type=music, media_id=https://rr3---sn-4gxx-25gee.googlevideo.com/videoplayback?expire=1700016739&ei=A95TZZqGJ6u9xN8Pv_WY-AU&ip=2a01%3Ae0a%3Ab51%3A1881%3A%3A758&id=o-ANDoVZOYQPRD9eFecMeZZYSQ5dis8oJbme__SLHEfPRv&itag=22&source=youtube&requiressl=yes&mh=4N&mm=31%2C29&mn=sn-4gxx-25gee%2Csn-4gxx-25gy&ms=au%2Crdu&mv=m&mvi=3&nh=EAE%2CEAE&pl=49&gcr=fr&initcwndbps=837500&spc=UWF9f3ksna66yPJfg5R5tlQkMj_Jasg&vprv=1&svpuc=1&mime=video%2Fmp4&cnr=14&ratebypass=yes&dur=280.566&lmt=1679738956453811&mt=1699994724&fvip=3&fexp=24007246&beids=24350018&c=ANDROID&txp=4532434&sparams=expire%2Cei%2Cip%2Cid%2Citag%2Csource%2Crequiressl%2Cgcr%2Cspc%2Cvprv%2Csvpuc%2Cmime%2Ccnr%2Cratebypass%2Cdur%2Clmt&sig=ANLwegAwRgIhAKJX4v021cEPTwDENpC0rKfpWwXIZGEZlUDXYDVsnOWPAiEAuJFJ5OMnGaiJP9TrD4knIyXWfw0kEWTqXQEBLtf_bto%3D&lsparams=mh%2Cmm%2Cmn%2Cms%2Cmv%2Cmvi%2Cnh%2Cpl%2Cinitcwndbps&lsig=AM8Gb2swQwIgCJOm_XExUAvulkk8qa7H3GcUVI6lGxskqgAcfyByPlkCH3_DH6lyvN1F2bfTQ6C73t_EA_JpQV8SSNELSHQ7xw0%3D> Error executing service: <ServiceCall media_player.play_media (c:01HF7SZ48YHD6GSMZCZRAQQ30E): extra=, entity_id=['media_player.sonos_chambre'], media_type=music, media_id=https://rr3---sn-4gxx-25gy.googlevideo.com/videoplayback?

more specifically we can see media_extractor trying to pass mp4 video through to Sonos ; &mime=video%2Fmp4 this results in a MIME Type error.

Here's my service payload:

service: media_extractor.play_media
data:
  media_content_id: https://www.youtube.com/watch?v=9bFHsd3o1w0
  media_content_type: music
target:
  entity_id: media_player.sonos_chambre

If anyone's able to shed som elight on this it'd be much appreciated, thanks!! Sleinous

Note: Only music & playlist seem to work, MUSIC, PLAYLIST, audio/youtube etc don't work and aren't accepted by Sonos.

What version of Home Assistant Core has the issue?

core-2023.11.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

https://www.home-assistant.io/integrations/media_extractor/

Diagnostics information

No response

Example YAML snippet

service: media_extractor.play_media
data:
  media_content_id: https://www.youtube.com/watch?v=9bFHsd3o1w0
  media_content_type: music
target:
  entity_id: media_player.sonos_chambre

Anything in the logs that might be useful for us?

Logger: homeassistant.core
Source: core.py:2051
First occurred: 21:52:20 (2 occurrences)
Last logged: 21:57:03

Error executing service: <ServiceCall media_player.play_media (c:01HF7SPG1HCYCF2CMBD0JQDNBD): extra=, entity_id=['media_player.sonos_chambre'], media_type=music, media_id=https://rr3---sn-4gxx-25gee.googlevideo.com/videoplayback?expire=1700016739&ei=A95TZZqGJ6u9xN8Pv_WY-AU&ip=2a01%3Ae0a%3Ab51%3A1881%3A%3A758&id=o-ANDoVZOYQPRD9eFecMeZZYSQ5dis8oJbme__SLHEfPRv&itag=22&source=youtube&requiressl=yes&mh=4N&mm=31%2C29&mn=sn-4gxx-25gee%2Csn-4gxx-25gy&ms=au%2Crdu&mv=m&mvi=3&nh=EAE%2CEAE&pl=49&gcr=fr&initcwndbps=837500&spc=UWF9f3ksna66yPJfg5R5tlQkMj_Jasg&vprv=1&svpuc=1&mime=video%2Fmp4&cnr=14&ratebypass=yes&dur=280.566&lmt=1679738956453811&mt=1699994724&fvip=3&fexp=24007246&beids=24350018&c=ANDROID&txp=4532434&sparams=expire%2Cei%2Cip%2Cid%2Citag%2Csource%2Crequiressl%2Cgcr%2Cspc%2Cvprv%2Csvpuc%2Cmime%2Ccnr%2Cratebypass%2Cdur%2Clmt&sig=ANLwegAwRgIhAKJX4v021cEPTwDENpC0rKfpWwXIZGEZlUDXYDVsnOWPAiEAuJFJ5OMnGaiJP9TrD4knIyXWfw0kEWTqXQEBLtf_bto%3D&lsparams=mh%2Cmm%2Cmn%2Cms%2Cmv%2Cmvi%2Cnh%2Cpl%2Cinitcwndbps&lsig=AM8Gb2swQwIgCJOm_XExUAvulkk8qa7H3GcUVI6lGxskqgAcfyByPlkCH3_DH6lyvN1F2bfTQ6C73t_EA_JpQV8SSNELSHQ7xw0%3D>
Error executing service: <ServiceCall media_player.play_media (c:01HF7SZ48YHD6GSMZCZRAQQ30E): extra=, entity_id=['media_player.sonos_chambre'], media_type=music, media_id=https://rr3---sn-4gxx-25gy.googlevideo.com/videoplayback?expire=1700017022&ei=Ht9TZa6LJb2NxN8Pqs2toAI&ip=2a01%3Ae0a%3Ab51%3A1881%3A%3A758&id=o-AGpFQApzvoyp_kVzCZB60kKK7DeE-cvpZQ0u2dgwm0dK&itag=22&source=youtube&requiressl=yes&mh=4N&mm=31%2C26&mn=sn-4gxx-25gy%2Csn-5hnekn7l&ms=au%2Conr&mv=m&mvi=3&nh=EAE%2C&pl=49&gcr=fr&initcwndbps=931250&spc=UWF9fzeIFy9Kc6RMOpq2cVPhUE-Cjyg&vprv=1&svpuc=1&mime=video%2Fmp4&cnr=14&ratebypass=yes&dur=280.566&lmt=1679738956453811&mt=1699994969&fvip=2&fexp=24007246&beids=24350018&c=ANDROID&txp=4532434&sparams=expire%2Cei%2Cip%2Cid%2Citag%2Csource%2Crequiressl%2Cgcr%2Cspc%2Cvprv%2Csvpuc%2Cmime%2Ccnr%2Cratebypass%2Cdur%2Clmt&sig=ANLwegAwRQIgFBrLnS393yeV6HmhvC0EUdmZr0EcFrEdXsP4x0UMf2MCIQCxoMUoleJdCzx7NFx8QKbI2VwgyxeWoVVcLB18AV82hA%3D%3D&lsparams=mh%2Cmm%2Cmn%2Cms%2Cmv%2Cmvi%2Cnh%2Cpl%2Cinitcwndbps&lsig=AM8Gb2swRAIgLK_KeJgxYEPhf-SYbqwZueu1wVr-9IXVKjMaxupnK8wCIA6xwcP7pv8MVljmcsAmQlyMTGtFTSaEyzn2_J5aEhl2>
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/components/sonos/helpers.py", line 64, in wrapper
    result = funct(self, *args, **kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/sonos/media_player.py", line 625, in _play_media
    soco.play_uri(media_id, force_radio=is_radio)
  File "/usr/local/lib/python3.11/site-packages/soco/core.py", line 148, in inner_function
    return function(self, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/soco/core.py", line 825, in play_uri
    self.avTransport.SetAVTransportURI(
  File "/usr/local/lib/python3.11/site-packages/soco/services.py", line 206, in _dispatcher
    return self.send_command(action, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/soco/services.py", line 514, in send_command
    self.handle_upnp_error(response.text)
  File "/usr/local/lib/python3.11/site-packages/soco/services.py", line 567, in handle_upnp_error
    raise SoCoUPnPException(
soco.exceptions.SoCoUPnPException: UPnP Error 714 received: Illegal MIME-Type from 192.168.2.176

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/core.py", line 2051, in _run_service_call_catch_exceptions
    await coro_or_task
  File "/usr/src/homeassistant/homeassistant/core.py", line 2072, in _execute_service
    return await target(service_call)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/entity_component.py", line 235, in handle_service
    return await service.entity_service_call(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 876, in entity_service_call
    response_data = await _handle_entity_call(
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/service.py", line 948, in _handle_entity_call
    result = await task
             ^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/sonos/media_player.py", line 542, in async_play_media
    await self.hass.async_add_executor_job(
  File "/usr/local/lib/python3.11/concurrent/futures/thread.py", line 58, in run
    result = self.fn(*self.args, **self.kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/components/sonos/helpers.py", line 78, in wrapper
    raise SonosUpdateError(message) from err
homeassistant.components.sonos.exception.SonosUpdateError: Error calling SonosMediaPlayerEntity._play_media on media_player.sonos_chambre: UPnP Error 714 received: Illegal MIME-Type from 192.168.2.176

Additional information

No response

home-assistant[bot] commented 12 months ago

Hey there @joostlek, mind taking a look at this issue as it has been labeled with an integration (media_extractor) you are listed as a code owner for? Thanks!

Code owner commands Code owners of `media_extractor` can trigger bot actions by commenting: - `@home-assistant close` Closes the issue. - `@home-assistant rename Awesome new title` Renames the issue. - `@home-assistant reopen` Reopen the issue. - `@home-assistant unassign media_extractor` Removes the current integration label and assignees on the issue, add the integration domain after the command.

(message by CodeOwnersMention)


media_extractor documentation media_extractor source (message by IssueLinks)

jobrittain commented 11 months ago

I've got the same issue

issue-triage-workflows[bot] commented 8 months ago

There hasn't been any activity on this issue recently. Due to the high number of incoming GitHub notifications, we have to clean some of the old issues, as many of them have already been resolved with the latest updates. Please make sure to update to the latest Home Assistant version and check if that solves the issue. Let us know if that works for you by adding a comment πŸ‘ This issue has now been marked as stale and will be closed if no further activity occurs. Thank you for your contributions.

Sleinous commented 8 months ago

Issue still exists

EnvoyΓ© Γ  partir de Outlook pour Androidhttps://aka.ms/AAb9ysg


From: issue-triage-workflows[bot] @.> Sent: Wednesday, March 13, 2024 11:08:15 PM To: home-assistant/core @.> Cc: Sleinous @.>; Author @.> Subject: Re: [home-assistant/core] Media Extractor integration doesn't play YouTube audio to Sonos (Issue #103984)

There hasn't been any activity on this issue recently. Due to the high number of incoming GitHub notifications, we have to clean some of the old issues, as many of them have already been resolved with the latest updates. Please make sure to update to the latest Home Assistant version and check if that solves the issue. Let us know if that works for you by adding a comment πŸ‘ This issue has now been marked as stale and will be closed if no further activity occurs. Thank you for your contributions.

β€” Reply to this email directly, view it on GitHubhttps://github.com/home-assistant/core/issues/103984#issuecomment-1995970864, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AIAUWR7Z3QAW4F4ID3O5YRDYYDE47AVCNFSM6AAAAAA7LOSGA6VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTSOJVHE3TAOBWGQ. You are receiving this because you authored the thread.Message ID: @.***>

joostlek commented 8 months ago

For some reason youtubedl ignores that best=mp3 from the looks of it, not sure why that happens

issue-triage-workflows[bot] commented 5 months ago

There hasn't been any activity on this issue recently. Due to the high number of incoming GitHub notifications, we have to clean some of the old issues, as many of them have already been resolved with the latest updates. Please make sure to update to the latest Home Assistant version and check if that solves the issue. Let us know if that works for you by adding a comment πŸ‘ This issue has now been marked as stale and will be closed if no further activity occurs. Thank you for your contributions.

mwaterbu commented 4 months ago

@joostlek, I think this should be re-opened. media_extractor.extract_media_url seems to ignore all format_query strings, which seems like a potential cause of this issue. Using the example from media_extractor.play_media (https://soundcloud.com/bruttoband/brutto-11), when manually checking formats with youtube-dl on my computer (2021.12.17), I get the following:

$ youtube-dl -F https://soundcloud.com/bruttoband/brutto-11
[soundcloud] bruttoband/brutto-11: Downloading info JSON
[soundcloud] 223644255: Downloading JSON metadata
[soundcloud] 223644255: Downloading JSON metadata
[soundcloud] 223644255: Downloading JSON metadata
[info] Available formats for 223644255:
format code   extension  resolution note
hls_opus_64   opus       audio only audio@ 64k
hls_mp3_128   mp3        audio only audio@128k
http_mp3_128  mp3        audio only audio@128k (best)

In this case, the one labeled (best) is indeed the ideal format, as the http stream simply returns an mp3 file (whereas the hls returns an m3u). And by default, it is the url which is returned βœ…:

$ youtube-dl -g https://soundcloud.com/bruttoband/brutto-11
https://cf-media.sndcdn.com/50remGX1OqRY.128.mp3?Policy=<... rest of url>

The same thing is returned when I pass in any of the following: best, bestaudio, best[ext=mp3], bestaudio[ext=mp3], http_mp3_128 Ex. βœ…:

youtube-dl -f http_mp3_128 -g  https://soundcloud.com/bruttoband/brutto-11
https://cf-media.sndcdn.com/50remGX1OqRY.128.mp3?Policy=<... rest of url>

By contrast, if I instead give hls_mp3_128 for the format, I receive an m3u link:

youtube-dl -f hls_mp3_128 -g https://soundcloud.com/bruttoband/brutto-11
https://cf-hls-media.sndcdn.com/playlist/50remGX1OqRY.128.mp3/playlist.m3u8?Policy=<... rest of url>

But when I use media_extractor.extract_media_url I always get the same thing back:

service: media_extractor.extract_media_url
data:
  url: https://soundcloud.com/bruttoband/brutto-11
  format_query: bestaudio[ext=mp3]

Returns ❌:

url: >-
  https://cf-hls-opus-media.sndcdn.com/playlist/50remGX1OqRY.64.opus/playlist.m3u8?Policy=<... rest of url>

I can pass in anything and get this response: best, bestaudio, best[ext=mp3], bestaudio[ext=mp3], http_mp3_128, hls_mp3_128, garbage, etc. It makes no difference, it is always returning the hls_opus_64 format (the first available format in the list).

Edit: after typing this up, I noticed that there was a switch to yt-dlp instead of youtube-dl last year. I retried my queries again with yt-dlp (2024.05.27), and most (best[ext=mp3], bestaudio[ext=mp3], http_mp3_128, hls_mp3_128) returned the same things as youtube-dl, except best, bestaudio, and the default (no -f given), which returned the hls_opus_64 format. With yt-dlp, the format list is in a different order:

$ yt-dlp -F  https://soundcloud.com/bruttoband/brutto-11
[soundcloud] Extracting URL: https://soundcloud.com/bruttoband/brutto-11
[soundcloud] bruttoband/brutto-11: Downloading info JSON
[soundcloud] 223644255: Downloading hls_mp3 format info JSON
[soundcloud] 223644255: Downloading http_mp3 format info JSON
[soundcloud] 223644255: Downloading hls_opus format info JSON
[info] Available formats for 223644255:
ID           EXT  RESOLUTION β”‚ FILESIZE  TBR PROTO β”‚ VCODEC     ACODEC  ABR
───────────────────────────────────────────────────────────────────────────
hls_mp3_128  mp3  audio only β”‚ ~3.50MiB 128k m3u8  β”‚ audio only mp3    128k
http_mp3_128 mp3  audio only β”‚ ~3.50MiB 128k http  β”‚ audio only mp3    128k
hls_opus_64  opus audio only β”‚ ~1.75MiB  64k m3u8  β”‚ audio only opus    64k

This refutes my point about it picking the first format. But it seems like yt-dlp is always being called with the default format from media_extractor.extract_media_url, regardless of format_query.

Pl1997 commented 4 months ago

Hello,

I'm encountering exactly the same issue as mentioned above. I'm trying to broadcast the audio from a youtube video to my Sonos speaker, but it's not working. I too tried to check the output of media_extractor.extract_media_url , and it shows that yt-dlp doesn't seem to care at all about the format_query, which I guess might be the cause of the problem.

@joostlek , should I open a new issue ? Or could this one be reopened ?

Thanks !