@bigfishtv/cockpit
Version:
248 lines (232 loc) • 6.48 kB
JavaScript
/**
* Video Utilities
* @module Utilities/videoUtils
*/
/**
* Takes a Youtube or Viemo url and returns an object with id and playlist_id (if available) extracted, otherwise returns false
* @param {String} string - Youtube or Vimeo url
* @return {Object}
*/
export function detectVideo(str) {
const youtube_id = extractYoutubeId(str)
const youtube_playlist_id = extractYoutubePlaylistId(str)
const vimeo_id = extractVimeoId(str)
const wistia_id = extractWistiaId(str)
if (youtube_playlist_id || youtube_id) {
return {
type: 'youtube',
video_id: youtube_id,
playlist_id: youtube_playlist_id || null,
start_time: extractYouTubeStartTime(str),
passcode: null,
}
}
if (vimeo_id) {
return {
type: 'vimeo',
video_id: vimeo_id,
playlist_id: null,
start_time: extractVimeoStartTime(str),
passcode: extractVimeoPasscode(str) || null,
}
}
if (wistia_id) {
return {
type: 'wistia',
video_id: wistia_id,
playlist_id: null,
start_time: extractWistiaStartTime(str),
passcode: null,
}
}
return false
}
/**
* Extracts id from Youtube url, else false
* @param {String} string - Youtube url
* @return {String}
*/
export function extractYoutubeId(str) {
str = str.trim()
const matches = str.match(
/(?:youtube\.com\/(?:[^\/]+\/.+\/|(?:v|e(?:mbed)?)\/|.*[?&]v=)|youtu\.be\/)([^"&?\/ ]{11})/i
)
if (str.match(/^[A-Za-z0-9_-]{11}$/)) {
return str
} else if (matches) {
return matches[1]
} else {
return false
}
}
/**
* Extracts playlist id from Youtube url, else false
* @param {String} string - Youtube url
* @return {String}
*/
export function extractYoutubePlaylistId(str) {
str = str.trim()
const matches = str.match(/list=(PL[A-Za-z0-9_-]{16,32})/)
if (str.match(/^PL[A-Za-z0-9_-]{16,32}$/)) {
return str
} else if (matches) {
return matches[1]
} else {
return false
}
}
/**
* Extracts id from Vimeo url, else false
* @param {String} string - Vimeo url
* @return {String}
*/
export function extractVimeoId(str) {
str = str.trim()
const matches = str.match(/vimeo.com\/(video\/)?(\d+)/i)
if (str.match(/^\d+$/)) {
return str
} else if (matches) {
return matches[2]
} else {
return false
}
}
/**
* Extracts id from Wistia video embed
* @param {String} string - Wistia embed code
* @return {String}
*/
export function extractWistiaId(str) {
if (str.indexOf('/medias/') > 0) {
return str.split('/medias/')[1]
} else if (str.indexOf('iframe') > 0) {
const matches = str.match(/embed\/iframe\/([^\?]+)/i)
if (matches) return matches[1]
} else {
const matches = str.match(/wistia_async_(.+?) /i)
if (matches) return matches[1]
}
return false
}
/**
* Returns the start time of a wistia embed video (default is 0)
*
* @param {String} str
* @returns {Number}
*/
export function extractWistiaStartTime(str) {
let matches = str.match(/[\?&]w?time=(\d+)m(\d+)s/)
if (matches) {
return parseInt(matches[1], 10) * 60 + parseInt(matches[2], 10)
}
matches = str.match(/[\?&]w?time=(\d+)/)
if (matches) {
return parseInt(matches[1], 10)
}
return 0
}
/**
* Returns the start time of a youtube embed video (default is 0)
*
* @param {String} str
* @returns {Number}
*/
export function extractYouTubeStartTime(str) {
const matches = str.match(/\bt=(\d+)/)
if (matches) {
return parseInt(matches[1], 10)
}
return 0
}
/**
* Returns the start time of a vimeo embed video (default is 0)
*
* @param {String} str
* @returns {Number}
*/
export function extractVimeoStartTime(str) {
let matches = str.match(/#t=(\d+)m(\d+)s/)
if (matches) {
return parseInt(matches[1], 10) * 60 + parseInt(matches[2], 10)
}
matches = str.match(/#t=(\d+)/)
if (matches) {
return parseInt(matches[1], 10)
}
return 0
}
/**
* Returns the passcode from a vimeo URL or embed code, false if not found
*
* @param {String} str
* @returns {String|false}
*/
export function extractVimeoPasscode(str) {
let matches = str.match(/\bh=([a-f0-9]{10,})/)
if (matches) {
return matches[1]
}
matches = str.match(/vimeo\.com\/(video\/)?\d+\/([a-f0-9]{10,})/)
if (matches) {
return matches[2]
}
return false
}
/**
* Creates an embed url from an object of video settings
* @param {Object} props - Embed video props
* @param {String} props.type - vimeo or youtube
* @param {String} props.video_id
* @param {String} props.playlist_id
* @param {Number} props.start_time - seconds
* @param {String} props.passcode
* @return {String}
*/
export function buildEmbedUrl({ type, video_id, playlist_id, start_time, passcode }) {
if (type == 'vimeo' && video_id) {
return `https://player.vimeo.com/video/${video_id}${passcode ? `?h=${passcode}` : ''}${
start_time > 0 ? `#t=${start_time}` : ''
}`
}
if (type == 'youtube' && playlist_id && video_id) {
return `https://www.youtube.com/embed/${video_id}?list=${playlist_id}${start_time > 0 ? `&t=${start_time}` : ''}`
}
if (type == 'youtube' && video_id) {
return `https://www.youtube.com/embed/${video_id}${start_time > 0 ? `?t=${start_time}` : ''}`
}
if (type == 'youtube' && playlist_id) {
return `https://www.youtube.com/embed?list=${playlist_id}`
}
if (type == 'wistia') {
return `https://fast.wistia.net/embed/iframe/${video_id}${start_time > 0 ? `?time=${start_time}` : ''}`
}
return false
}
/**
* Creates external url to Vimeo or Youtube from an object of video settings
* @param {Object} props - Embed video props
* @param {String} props.type - vimeo or youtube
* @param {String} props.video_id
* @param {String} props.playlist_id
* @param {Number} props.start_time - seconds
* @param {String} props.passcode
* @return {String}
*/
export function buildVideoUrl({ type, video_id, playlist_id, start_time, passcode }) {
if (type == 'vimeo' && video_id) {
return `https://vimeo.com/${video_id}${passcode ? `/${passcode}` : ''}${start_time > 0 ? `#t=${start_time}` : ''}`
}
if (type == 'youtube' && playlist_id && video_id) {
return `https://www.youtube.com/watch?v=${video_id}&list=${playlist_id}${start_time > 0 ? `&t=${start_time}` : ''}`
}
if (type == 'youtube' && video_id) {
return `https://www.youtube.com/watch?v=${video_id}${start_time > 0 ? `&t=${start_time}` : ''}`
}
if (type == 'youtube' && playlist_id) {
return `https://www.youtube.com/playlist?list=${playlist_id}`
}
if (type == 'wistia') {
return `https://fast.wistia.net/embed/iframe/${video_id}${start_time > 0 ? `?time=${start_time}` : ''}`
}
return false
}