Skip to content

fix: handle None quality in video/audio stream comparison - #2

Open
xiaoyuyu6420 wants to merge 1 commit into
nooblong:mainfrom
xiaoyuyu6420:fix/none-quality-stream-cmp
Open

xiaoyuyu6420 wants to merge 1 commit into
nooblong:mainfrom
xiaoyuyu6420:fix/none-quality-stream-cmp

Conversation

@xiaoyuyu6420

Copy link
Copy Markdown

Problem

VideoDownloadURLDataDetecter.detect_best_streams() crashes with AttributeError: 'NoneType' object has no attribute 'value' when a stream in the DASH response has video_quality / audio_quality set to None.

Both comparator functions (video_stream_cmp / audio_stream_cmp) call .value directly on the quality enums:

  • video_stream_cmp: if s1.video_quality.value != s2.video_quality.values1.video_quality.value - s2.video_quality.value
  • audio_stream_cmp: return s1.audio_quality.value - s2.audio_quality.value

Reproduced on PyPI bilibili-api-python==17.4.2 (detect_best_streams on line 2636), breaking video download/resolution detection for some videos.

Fix

Guard both comparators: if either stream's quality is None, return 0 before the Dolby/HDR/Hi-Res priority checks.

if s1.video_quality is None or s2.video_quality is None:
    return 0
if s1.audio_quality is None or s2.audio_quality is None:
    return 0

Verification

A/B tested against the released 17.4.2 source with detect() mocked to return streams carrying None quality:

  • unpatched: AttributeError: 'NoneType' object has no attribute 'value' (video.py:2636)
  • patched: detect_best_streams() returns the stream list normally

No behavior change for streams with valid quality values.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant