fix: improve media extraction

This commit is contained in:
Peter Steinberger
2026-01-03 18:34:32 +01:00
parent b4542b2213
commit d8643ae1d1
2 changed files with 59 additions and 8 deletions
+11 -8
View File
@@ -189,18 +189,21 @@ export function extractMedia(result: GraphqlTweetResult | undefined): TweetMedia
// Extract video URL for video/animated_gif
if ((item.type === 'video' || item.type === 'animated_gif') && item.video_info?.variants) {
// Find highest bitrate mp4 variant
const mp4Variants = item.video_info.variants
.filter((v): v is { bitrate: number; content_type: string; url: string } =>
v.content_type === 'video/mp4' && typeof v.bitrate === 'number' && typeof v.url === 'string'
)
// Prefer highest bitrate MP4, fall back to first MP4 when bitrate is missing.
const mp4Variants = item.video_info.variants.filter(
(v): v is { bitrate?: number; content_type: string; url: string } =>
v.content_type === 'video/mp4' && typeof v.url === 'string',
);
const mp4WithBitrate = mp4Variants
.filter((v): v is { bitrate: number; content_type: string; url: string } => typeof v.bitrate === 'number')
.sort((a, b) => b.bitrate - a.bitrate);
const selectedVariant = mp4WithBitrate[0] ?? mp4Variants[0];
if (mp4Variants.length > 0) {
mediaItem.videoUrl = mp4Variants[0].url;
if (selectedVariant) {
mediaItem.videoUrl = selectedVariant.url;
}
if (item.video_info.duration_millis) {
if (typeof item.video_info.duration_millis === 'number') {
mediaItem.durationMs = item.video_info.duration_millis;
}
}