Last active
November 21, 2022 04:00
-
-
Save onigetoc/461614e97a76e84606d7cc3675b65428 to your computer and use it in GitHub Desktop.
Javascript REGEX - Get video extension from video url and return video Type with IF
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/**************************** AUTOMATICALLY GET AND SET AUDIO & VIDEO TYPE ******************************/ | |
const rtmp_suffix = /^rtmp:\/\//; | |
const hls_suffix = /\.m3u8/; | |
const mp4_suffix = /\.(mp4|m4p|m4v|mov)/i; | |
const hds_suffix = /\.f4m/; | |
const dash_suffix = /\.mpd/; | |
const flv_suffix = /\.flv/; | |
const webm_suffix = /\.webm/; | |
/* AUDIO */ | |
//const mp3_suffix = /\.mp3/; | |
const mpeg_suffix = /\.(mp3|m4a)/i; | |
const ogg_suffix = /\.ogg/; | |
//const youtube_suffix = /\.youtube.com/; | |
//const yt_suffix = /^(?:https?:\/\/)?(?:www\.)?youtube\.com\/watch\?(?=.*v=((\w|-){11}))(?:\S+)?$/; | |
const yt_suffix = /^.*(youtu.be\/|v\/|u\/\w\/|embed\/|watch\?v=|\&v=)([^#\&\?]*).*/; | |
const dm_suffix = /\.?dailymotion.com/; | |
const vm_suffix = /\.?vimeo.com/; | |
/* ADVANCED REGEX */ | |
// const regVimeo = /^.*(vimeo.com\/)((channels\/[A-z]+\/)|(groups\/[A-z]+\/videos\/))?([0-9]+)/; | |
// const regDailymotion = /^.+dailymotion.com\/(video|hub)\/([^_]+)[^#]*(#video=([^_&]+))?/; | |
// const regMetacafe = /^.*(metacafe.com)(\/watch\/)(d+)(.*)/i; | |
// const youtube_suffix = /(youtu.be\/|v\/|u\/\w\/|embed\/|watch\?v=|\&v=)([^#\&\?]*).*/; | |
function getType(url) { | |
/* AUDIO */ | |
if (mpeg_suffix.test(url)) | |
return 'audio/mpeg'; | |
if (ogg_suffix.test(url)) | |
return 'audio/ogg'; | |
/* VIDEO */ | |
if (dash_suffix.test(url)) | |
return 'application/dash+xml'; | |
if (rtmp_suffix.test(url)) | |
return 'rtmp/mp4'; | |
if (hls_suffix.test(url)) | |
return 'application/x-mpegurl'; | |
if (mp4_suffix.test(url)) | |
return 'video/mp4'; | |
if (hds_suffix.test(url)) | |
return 'application/adobe-f4m'; | |
if (flv_suffix.test(url)) | |
return 'video/flv'; | |
if (webm_suffix.test(url)) | |
return 'video/webm'; | |
if (yt_suffix.test(url)) | |
return 'video/youtube'; | |
if (dm_suffix.test(url)) | |
return 'video/dailymotion'; | |
if (vm_suffix.test(url)) | |
return 'video/vimeo'; | |
console.log('could not find link type: "' + url + '" assuming is mp4'); | |
return 'video/mp4'; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment