logo hsb.horse
← Back to blog index

Blog

TypeScript Implementation to Extract Size Information from Twitter Video URLs

A TypeScript implementation that extracts resolution information from Twitter (X) video URLs with a regular expression. A quick summary of the URL pattern and how to read the size.

Published:

Twitter (X) video URLs contain resolution information.

This is a small implementation that extracts that information with a regular expression.

Implementation

interface MediaSize {
height: number;
width: number;
}
function extractVideoSize(url: string): MediaSize | undefined {
const sizeRegex = /\/(\d+)x(\d+)\//;
const match = url.match(sizeRegex);
if (match && match[1] && match[2]) {
const width = parseInt(match[1], 10);
const height = parseInt(match[2], 10);
return { width, height };
}
return undefined;
}

Point

The regular expression /\/(\d+)x(\d+)\// extracts a part like 1080x1920 from the URL.

If it matches, the values are converted with parseInt and returned. If not, the function returns undefined.

Example

const inputs = [
"https://video.twimg.com/ext_tw_video/1111/pu/vid/avc1/1080x1920/1.mp4",
"https://video.twimg.com/ext_tw_video/1111/pu/pl/1.m3u8"
];
console.log(extractVideoSize(inputs[0]));
// { width: 1080, height: 1920 }
console.log(extractVideoSize(inputs[1]));
// undefined

The mp4 URL contains resolution information, but the m3u8 playlist URL does not, so it returns undefined.

Summary

Extracting resolution from a Twitter video URL is easy with a regular expression.

It is useful when you want to read the size in advance for preview rendering or thumbnail generation.