URLs de vídeo do Twitter (X) contêm informações de resolução.
Aqui está uma implementação que extrai esses dados com expressão regular.
Implementação
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;}Ponto principal
A expressão regular /\/(\d+)x(\d+)\// extrai da URL um trecho como 1080x1920.
Se houver correspondência, os valores são convertidos com parseInt e retornados. Se não houver, a função retorna undefined.
Exemplo de execução
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]));// undefinedA URL mp4 contém a resolução, mas a URL da playlist m3u8 não contém, então o retorno é undefined.
Resumo
Extrair a resolução de uma URL de vídeo do Twitter é algo simples com expressão regular.
Isso ajuda quando você quer obter o tamanho antes para preview ou geração de thumbnail.
hsb.horse