Skip to content

Instantly share code, notes, and snippets.

@rajogit
Forked from astockwell/README.md
Last active August 23, 2016 12:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rajogit/fc383a3a33811e6a51891c59105fc5f2 to your computer and use it in GitHub Desktop.
Save rajogit/fc383a3a33811e6a51891c59105fc5f2 to your computer and use it in GitHub Desktop.
PHP Video Url Parser

Youtube/Vimeo Video Url Parser

Parses URLs from major cloud video providers. Capable of extracting keys from various video embed and link urls to manipulate and access videos in various ways. Also you can return thumnails video url.

Usage

VideoUrlParser::identify_service("https://www.youtube.com/watch?v=x_8kFbZf20I&feature=youtu.be");
// returns string "youtube"

VideoUrlParser::get_url_id("https://www.youtube.com/watch?v=x_8kFbZf20I&feature=youtu.be");
// returns string "x_8kFbZf20I"

VideoUrlParser::get_url_embed("https://www.youtube.com/watch?v=x_8kFbZf20I&feature=youtu.be");
// returns string "http://youtube.com/embed/x_8kFbZf20I?autoplay=1"

VideoUrlParser::get_vimeo_url_thumb("https://www.youtube.com/watch?v=y1pe6ysmNvE");
// returns string "http://img.youtube.com/vi/y1pe6ysmNvE/0.jpg"

Supports

  • Vimeo
  • Youtube
<?php
/**
* Video Url Parser
*
* Parses URLs from major cloud video providers. Capable of returning
* keys from various video embed and link urls to manipulate and
* access videos in various ways.
*/
class VideoUrlParser
{
/**
* Determines which cloud video provider is being used based on the passed url.
*
* @param string $url The url
* @return null|string Null on failure to match, the service's name on success
*/
public static function identify_service($url)
{
if (preg_match('%youtube|youtu\.be%i', $url)) {
return 'youtube';
}
elseif (preg_match('%vimeo%i', $url)) {
return 'vimeo';
}
return null;
}
/**
* Determines which cloud video provider is being used based on the passed url,
* and extracts the video id from the url.
*
* @param string $url The url
* @return null|string Null on failure, the video's id on success
*/
public static function get_url_id($url)
{
$service = self::identify_service($url);
if ($service == 'youtube') {
return self::get_youtube_id($url);
}
elseif ($service == 'vimeo') {
return self::get_vimeo_id($url);
}
return null;
}
/**
* Determines which cloud video provider is being used based on the passed url,
* extracts the video id from the url, and builds an embed url.
*
* @param string $url The url
* @return null|string Null on failure, the video's embed url on success
*/
public static function get_url_embed($url)
{
$service = self::identify_service($url);
$id = self::get_url_id($url);
if ($service == 'youtube') {
return self::get_youtube_embed($id);
}
elseif ($service == 'vimeo') {
return self::get_vimeo_embed($id);
}
return null;
}
/**
* Parses various youtube urls and returns video identifier.
*
* @param string $url The url
* @return string the url's id
*/
public static function get_youtube_id($url)
{
$youtube_url_keys = array('v','vi');
// Try to get ID from url parameters
$key_from_params = self::parse_url_for_params($url, $youtube_url_keys);
if ($key_from_params) return $key_from_params;
// Try to get ID from last portion of url
return self::parse_url_for_last_element($url);
}
/**
* Builds a Youtube embed url from a video id.
*
* @param string $youtube_video_id The video's id
* @return string the embed url
*/
public static function get_youtube_embed($youtube_video_id, $autoplay = 1)
{
$embed = "http://youtube.com/embed/$youtube_video_id?autoplay=$autoplay";
return $embed;
}
/**
* Parses various vimeo urls and returns video identifier.
*
* @param string $url The url
* @return string The url's id
*/
public static function get_vimeo_id($url)
{
// Try to get ID from last portion of url
return self::parse_url_for_last_element($url);
}
/**
* Builds a Vimeo embed url from a video id.
*
* @param string $vimeo_video_id The video's id
* @return string the embed url
*/
public static function get_vimeo_embed($vimeo_video_id, $autoplay = 1)
{
$embed = "http://player.vimeo.com/video/$vimeo_video_id?byline=0&amp;portrait=0&amp;autoplay=$autoplay";
return $embed;
}
/**
* Find the first matching parameter value in a url from the passed params array.
*
* @access private
*
* @param string $url The url
* @param array $target_params Any parameter keys that may contain the id
* @return null|string Null on failure to match a target param, the url's id on success
*/
private static function parse_url_for_params($url, $target_params)
{
parse_str( parse_url( $url, PHP_URL_QUERY ), $my_array_of_params );
foreach ($target_params as $target) {
if (array_key_exists ($target, $my_array_of_params)) {
return $my_array_of_params[$target];
}
}
return null;
}
/**
* Find the last element in a url, without any trailing parameters
*
* @access private
*
* @param string $url The url
* @return string The last element of the url
*/
private static function parse_url_for_last_element($url)
{
$url_parts = explode("/", $url);
$prospect = end($url_parts);
$prospect_and_params = preg_split("/(\?|\=|\&)/", $prospect);
if ($prospect_and_params) {
return $prospect_and_params[0];
} else {
return $prospect;
}
return $url;
}
/**
* Fet a Youtube thumb url from a video id.
*
* @param string $youtube_video_id The video's id
* @return string the thumb url
*/
public static function get_youtube_url_thumb($url)
{
$youtube_video_id = self::get_youtube_id($url);
$thumb = "http://img.youtube.com/vi/" .$youtube_video_id. "/0.jpg";
return $thumb;
}
/**
* Get a Vimeo thumb url from a video id.
*
* @param string $vimeo_video_id The video's id
* @return string the thumb url
*/
public static function get_vimeo_url_thumb($url)
{
$vimeo_video_id = self::get_vimeo_id($url);
$data = file_get_contents("http://vimeo.com/api/v2/video/" .$vimeo_video_id. ".json");
$data = json_decode($data);
$thumb = $data[0]->thumbnail_large;
return $thumb;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment