コンテンツにスキップ

Twitterの動画URLからサイズを抽出する

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;
}
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