Skip to content

plugins.youtube: fix adaptive streams KeyError #5139

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

bastimeyer
Copy link
Member

ref #5137

$ streamlink 'https://www.youtube.com/embed/aqz-KE-bpKQ'
[cli][info] Found matching plugin youtube for URL https://www.youtube.com/embed/aqz-KE-bpKQ
Traceback (most recent call last):
  File "/home/basti/venv/streamlink-311/bin/streamlink", line 8, in <module>
    sys.exit(main())
             ^^^^^^
  File "/home/basti/repos/streamlink/src/streamlink_cli/main.py", line 933, in main
    handle_url()
  File "/home/basti/repos/streamlink/src/streamlink_cli/main.py", line 549, in handle_url
    streams = fetch_streams(plugin)
              ^^^^^^^^^^^^^^^^^^^^^
  File "/home/basti/repos/streamlink/src/streamlink_cli/main.py", line 443, in fetch_streams
    return plugin.streams(stream_types=args.stream_types,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/basti/repos/streamlink/src/streamlink/plugin/plugin.py", line 420, in streams
    ostreams = self._get_streams()
               ^^^^^^^^^^^^^^^^^^^
  File "/home/basti/repos/streamlink/src/streamlink/plugins/youtube.py", line 380, in _get_streams
    streams.update(self._create_adaptive_streams(adaptive_formats))
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/basti/repos/streamlink/src/streamlink/plugins/youtube.py", line 244, in _create_adaptive_streams
    if best_audio_itag is None or self.adp_audio[itag] > self.adp_audio[best_audio_itag]:
                                  ~~~~~~~~~~~~~~^^^^^^
KeyError: 328

--->

$ streamlink 'https://www.youtube.com/embed/aqz-KE-bpKQ'
[cli][info] Found matching plugin youtube for URL https://www.youtube.com/embed/aqz-KE-bpKQ
Available streams: audio_mp4a, audio_opus, 144p (worst), 240p, 360p, 480p, 720p, 720p60, 1080p60, 1440p60, 2160p60 (best)

This fixes how audio-only streams are included in the stream selection. Previously, the order of adaptive formats mattered and simply the last item was included, regardless the bitrate. Now, it's only using the best audio quality. HOWEVER, this implementation ignores different codec profiles/versions and merges everything together into the same codec name. This is one of the issues I've talked about in #4902. Doesn't matter here much though for the couple of audio-only streams.

The download throttling issue with unsigned adaptive stream request URLs is still present of course (see YT meta issue).

@bastimeyer bastimeyer added the plugin issue A Plugin does not work correctly label Feb 2, 2023
@bastimeyer bastimeyer force-pushed the plugins/youtube/fix-adaptive-streams-keyerror branch from 916af6d to a93703f Compare February 2, 2023 09:03
@bastimeyer bastimeyer changed the title plugins.youtube: fix adpative streams KeyError plugins.youtube: fix adaptive streams KeyError Feb 2, 2023
@gravyboat gravyboat merged commit 319a60f into streamlink:master Feb 3, 2023
@bastimeyer bastimeyer deleted the plugins/youtube/fix-adaptive-streams-keyerror branch February 3, 2023 18:24
@fe80Grau
Copy link

Only outputs audio streams for me.
streamlink "https://www.youtube.com/watch?v=oAmoqGhGmnI&ab_channel=VictorS%C3%A1nchez"
[cli][info] Found matching plugin youtube for URL https://www.youtube.com/watch?v=oAmoqGhGmnI&ab_channel=VictorS%C3%A1nchez Available streams: audio_mp4a, audio_opus, 144p (worst), 240p, 360p, 480p, 720p, 720p60, 1080p60 (best)

And yes, in VLC only plays audio.
Trying with current stable and streamlink 5.3.1+33.g142d17f8

@bastimeyer
Copy link
Member Author

Only outputs audio streams

False. Streamlink remuxes video and audio streams into the output, as you can see in its debug log, as well as ffprobe's output:

$ streamlink --loglevel=none --stdout 'https://www.youtube.com/watch?v=oAmoqGhGmnI&ab_channel=VictorS%C3%A1nchez' best 2>/dev/null \
  | ffprobe -v error -of json -show_streams pipe:0 \
  | jq -r '.streams[] | .codec_name'
h264
opus
$ streamlink --loglevel=debug --stdout 'https://www.youtube.com/watch?v=oAmoqGhGmnI&ab_channel=VictorS%C3%A1nchez' best >/dev/null
[cli][debug] OS:         Linux-6.1.12-1-git-x86_64-with-glibc2.37
[cli][debug] Python:     3.11.1
[cli][debug] Streamlink: 5.3.1+33.g142d17f8
[cli][debug] Dependencies:
[cli][debug]  certifi: 2022.12.7
[cli][debug]  isodate: 0.6.1
[cli][debug]  lxml: 4.9.2
[cli][debug]  pycountry: 22.3.5
[cli][debug]  pycryptodome: 3.17
[cli][debug]  PySocks: 1.7.1
[cli][debug]  requests: 2.28.2
[cli][debug]  urllib3: 1.26.14
[cli][debug]  websocket-client: 1.5.1
[cli][debug] Arguments:
[cli][debug]  url=https://www.youtube.com/watch?v=oAmoqGhGmnI&ab_channel=VictorS%C3%A1nchez
[cli][debug]  stream=['best']
[cli][debug]  --loglevel=debug
[cli][debug]  --player=mpv
[cli][debug]  --stdout=True
[cli][info] Found matching plugin youtube for URL https://www.youtube.com/watch?v=oAmoqGhGmnI&ab_channel=VictorS%C3%A1nchez
[plugins.youtube][debug] Using video ID: oAmoqGhGmnI
[stream.ffmpegmux][debug] ffmpeg version n5.1.2 Copyright (c) 2000-2022 the FFmpeg developers
[stream.ffmpegmux][debug]  built with gcc 12.2.0 (GCC)
[stream.ffmpegmux][debug]  configuration: --prefix=/usr --disable-debug --disable-static --disable-stripping --enable-amf --enable-avisynth --enable-cuda-llvm --enable-lto --enable-fontconfig --enable-gmp --enable-gnutls --enable-gpl --enable-ladspa --enable-libaom --enable-libass --enable-libbluray --enable-libbs2b --enable-libdav1d --enable-libdrm --enable-libfreetype --enable-libfribidi --enable-libgsm --enable-libiec61883 --enable-libjack --enable-libmfx --enable-libmodplug --enable-libmp3lame --enable-libopencore_amrnb --enable-libopencore_amrwb --enable-libopenjpeg --enable-libopus --enable-libpulse --enable-librav1e --enable-librsvg --enable-libsoxr --enable-libspeex --enable-libsrt --enable-libssh --enable-libsvtav1 --enable-libtheora --enable-libv4l2 --enable-libvidstab --enable-libvmaf --enable-libvorbis --enable-libvpx --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxcb --enable-libxml2 --enable-libxvid --enable-libzimg --enable-nvdec --enable-nvenc --enable-opencl --enable-opengl --enable-shared --enable-version3 --enable-vulkan
[stream.ffmpegmux][debug]  libavutil      57. 28.100 / 57. 28.100
[stream.ffmpegmux][debug]  libavcodec     59. 37.100 / 59. 37.100
[stream.ffmpegmux][debug]  libavformat    59. 27.100 / 59. 27.100
[stream.ffmpegmux][debug]  libavdevice    59.  7.100 / 59.  7.100
[stream.ffmpegmux][debug]  libavfilter     8. 44.100 /  8. 44.100
[stream.ffmpegmux][debug]  libswscale      6.  7.100 /  6.  7.100
[stream.ffmpegmux][debug]  libswresample   4.  7.100 /  4.  7.100
[stream.ffmpegmux][debug]  libpostproc    56.  6.100 / 56.  6.100
[plugins.youtube][debug] MuxedStream: v 299 a 251 = 1080p60
[plugins.youtube][debug] MuxedStream: v 302 a 251 = 720p60
[plugins.youtube][debug] MuxedStream: v 135 a 251 = 480p
[plugins.youtube][debug] MuxedStream: v 133 a 251 = 240p
[plugins.youtube][debug] MuxedStream: v 160 a 251 = 144p
[cli][info] Available streams: audio_mp4a, audio_opus, 144p (worst), 240p, 360p, 480p, 720p, 720p60, 1080p60 (best)
[cli][info] Opening stream: 1080p60 (muxed-stream)
[stream.ffmpegmux][debug] Opening http substream
[stream.ffmpegmux][debug] Opening http substream
[utils.named_pipe][info] Creating pipe streamlinkpipe-1790831-1-8237
[utils.named_pipe][info] Creating pipe streamlinkpipe-1790831-2-3292
[stream.ffmpegmux][debug] ffmpeg command: /usr/bin/ffmpeg -nostats -y -i /tmp/streamlinkpipe-1790831-1-8237 -i /tmp/streamlinkpipe-1790831-2-3292 -c:v copy -c:a copy -map 0 -map 1 -f matroska pipe:1
[stream.ffmpegmux][debug] Starting copy to pipe: /tmp/streamlinkpipe-1790831-1-8237
[stream.ffmpegmux][debug] Starting copy to pipe: /tmp/streamlinkpipe-1790831-2-3292
[cli][debug] Pre-buffering 8192 bytes
[cli][debug] Writing stream to output

@fe80Grau
Copy link

fe80Grau commented Mar 11, 2023

Ty, in cli is working, with cuts but working. The problem is in python streamlink.streams(query).items().
Only returns links for audio output

Captura de pantalla 2023-03-11 154343

@bastimeyer
Copy link
Member Author

Your question is completely unrelated to this PR or even the youtube plugin.

MuxedStream instances don't support to_url() or to_manifest_url(). You can see available stream URLs if you set the --json CLI parameter or --stream-url.

The only ones you'll see when using the youtube plugin are HLSStream for live streams, and HTTPStream instances which are yielded from the "adaptive" audio-only streams or streams from the formats list.

streams.update({
f"audio_{stream_codec}": HTTPStream(self.session, adaptive_streams[itag])
for stream_codec, itag in audio_streams.items()

streams[label] = HTTPStream(self.session, url)


Locking the thread, as this discussion doesn't belong here.

@streamlink streamlink locked as resolved and limited conversation to collaborators Mar 11, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
plugin issue A Plugin does not work correctly
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants