fix(extension): import the best-quality Twitter video variant#1351
Open
abhay-codes07 wants to merge 1 commit into
Open
fix(extension): import the best-quality Twitter video variant#1351abhay-codes07 wants to merge 1 commit into
abhay-codes07 wants to merge 1 commit into
Conversation
Tweet import stored the video URL as video_info.variants[0].url. Twitter returns several variants for one video: an HLS `.m3u8` playlist (no bitrate) plus multiple `video/mp4` renditions at different bitrates, in no guaranteed order. Taking the first entry therefore frequently stored the HLS playlist URL, which is not a directly usable video file, or whichever low-bitrate clip happened to come first. Add pickBestVideoVariantUrl, which prefers `video/mp4` renditions (by content_type, or by a `.mp4` URL when content_type is missing) and picks the highest bitrate among them, falling back to the first variant when no MP4 is present. Models the previously-untyped bitrate/content_type fields and adds unit tests for the selection.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
When importing bookmarked tweets, the video URL was taken as
video_info.variants[0].url:Twitter returns several variants for a single video: an HLS
.m3u8playlist (which has nobitrateand is not a directly usable video file) plus multiplevideo/mp4renditions at different bitrates, in no guaranteed order. Sovariants[0]frequently stored either the HLS playlist URL or whatever low-bitrate rendition happened to be first, rather than a good, directly-playable MP4.The variant type also didn't model
bitrate/content_typeat all, so there was nothing to select on.Fix
Add a small pure helper and use it at the call site:
It prefers MP4 renditions (by
content_type, or by a.mp4URL whencontent_typeis absent), picks the highest bitrate among them, and falls back to the first variant when there is no MP4 (e.g. HLS-only), which preserves the old behaviour in that degenerate case. TheVideoVarianttype now includes thebitrate/content_typefields the API actually returns.Tests
twitter-video-variant.test.ts(bun:test): highest-bitrate MP4 wins over first/lower entries, the HLS playlist is never chosen when MP4s exist, MP4 is detected by extension whencontent_typeis missing, HLS-only falls back to the first variant, and empty/undefined returns"". 5 tests pass; extensiontscis clean for this file.