I'm looking for a way to get video width and height by Node.js (all I have is the video URL).
For that matter, I'm also using Puppeteer for getting video URLs from the web, so any manipulation needed that requires DOM is pretty much optionally doable - on the data crawling process I have access to the element.
Any suggestions for that issue?
CODE:
export async function _getSourcesLinks(targetUrl: string): Promise < string[] > {
return await puppeteer.launch({
args: ['--no-sandbox', '--disable-setuid-sandbox'],
headless: true,
}).then(async browser => {
try {
const page = await browser.newPage()
page.setJavaScriptEnabled(true)
await page.goto(targetUrl, {
waitUntil: 'load'
})
try {
// waiting for video to appear
await page.waitForTimeout(1500)
// await page.waitForSelector('video', { timeout: 2500 }) // die after 2.5 seconds
} catch (error) {
console.error(error)
}
const srcs = await page.$$eval < Element > ('video, .video, [src]', (videos) => {
// TODO get also width and height
return videos.map(video => {
// @ts-ignore
return video.getAttribute('src')
})
}) as never as string[]
const downloadTargets = srcs.filter(src => {
console.log(src)
if (!src) return false
const fitPattern = new RegExp('.*\.mp4.*')
return fitPattern.test(src)
})
browser.close()
return downloadTargets
} catch (error) {
console.error(error)
}
})
}
As for the test video - I don't have one, it's just happened from time to time