Using PHP, how can I get video information like title, description, thumbnail from a youtube video URL e.g.
http://www.youtube.com/watch?v=B4CRkpBGQzU
You can get data from youtube oembed interface in two formats: XML and JSON
Interface address: http://www.youtube.com/oembed?url=youtubeurl&format=json
Use this PHP function to get data
function get_youtube($url){
$youtube = "http://www.youtube.com/oembed?url=". $url ."&format=json";
$curl = curl_init($youtube);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$return = curl_exec($curl);
curl_close($curl);
return json_decode($return, true);
}
$url = // youtube video url
// Display Data
print_r(get_youtube($url));
Don't forget to enable extension=php_curl.dll in your php.ini
This returns metadata about a video:
http://www.youtube.com/oembed?url={videoUrlHere}&format=json
Using your example, a call to:
http://www.youtube.com/oembed?url=http://www.youtube.com/watch?v=B4CRkpBGQzU&format=json
Returns the following, which you can digest and parse with PHP:
{
"provider_url": "http:\/\/www.youtube.com\/",
"thumbnail_url": "http:\/\/i3.ytimg.com\/vi\/B4CRkpBGQzU\/hqdefault.jpg",
"title": "Joan Osborne - One Of Us",
"html": "\u003ciframe width=\"459\" height=\"344\" src=\"http:\/\/www.youtube.com\/embed\/B4CRkpBGQzU?fs=1\u0026feature=oembed\" frameborder=\"0\" allowfullscreen\u003e\u003c\/iframe\u003e",
"author_name": "jzsdhk",
"height": 344,
"thumbnail_width": 480,
"width": 459,
"version": "1.0",
"author_url": "http:\/\/www.youtube.com\/user\/jzsdhk",
"provider_name": "YouTube",
"type": "video",
"thumbnail_height": 360
}
Yet another URL API that can be helpful is: https://www.youtube.com/get_video_info?video_id=B4CRkpBGQzU
video_id is "v" argument of youTube. Result is a dictionary in URL-encoded format (key1=value1&key2=value2&...)
This is undocumented API existing for long time, so exploring it is up to developer. I am aware of "status" (ok/fail), "errorcode" (100 and 150 in my practice), "reason" (string description of error). I am getting duration ("length_seconds") this way because oEmbed does not provide this information (strange, but true) and I can hardly motivate every employer to get keys from youTube to use official API
To get youtube video description, different sized thumbnail, tags, etc use updated v3 googleapis
Sample URL: https://www.googleapis.com/youtube/v3/videos?part=snippet&id=7wtfhZwyrcc&key=my_google_api_key
$google_api_key = 'google_api_key';
$video_id = 'youtube_video_id';
$api_url = 'https://www.googleapis.com/youtube/v3/videos?part=snippet&id='.$video_id.'&key='.$google_api_key;
$json = file_get_contents($api_url);
$obj = json_decode($json);
var_dump($obj);
I wrote this class to display a Youtube video on our website:
<?php
/*
Copyright (c) 2013 ReFri Software / Internet Publication
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following
conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this
list of conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution.
* Neither the name of ReFri Software / Internet Publicion nor the names of its contributors
may be used to endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
youtube.php
Author : Cynthia Fridsma
http://www.heathernova.us
Date : February 4, 2013
Version: 1.1
Property's width and height are now private
Added: variable error message
*/
class YouTube {
public $video;
private $width;
private $height;
public $error = "<b>Video not found, sorry</b>" ;
function __construct ($width=640, $height=360){
$this->width=$width;
$this->height=$height;
if($this->width<220){
// minimum width
$this->width=220;
}
if($this->height<220){
// minimum height
$this->height=220;
}
}
function playVideo(){
// Default video settings:
#
# youtube_list = false
# youtube_video = false
$youtube_list = false;
$youtube_video = false;
// always check contains youtube.com or youtube.be as a reference....
if(stristr($this->video, "youtube.com")==true ||(stristr($this->video, "youtube.be") || (stristr($this->video, "youtu.be")))){
// Test if the video contains a query..
$test = (parse_url($this->video));
if(isset($test['query'])){
$testing = $test['query'];
parse_str($testing);
if(isset($v)&&(isset($list))){
// we're dealing with a play list and a selected video.
$test = $list;
$youtube_list = true;
}
if(isset($list) &&(empty($v))){
// we're only dealing wih a play list.
$test = $list;
$youtube_list = true;
$test = $list;
}
if(isset($v) &&(empty($list))){
// we're only dealing with a single video.
$test = $v;
$youtube_video = true;
}
if(empty($v) &&(empty($list))){
// we're not dealing with a valid request.
$youtube_video = false;
}
} else {
// Apperently we're dealing with a shared link.
$testing =parse_url($this->video, PHP_URL_PATH);
$test = stristr($testing, "/");
$test = substr($test,1);
$youtube_video = true;
}
if($youtube_video==true){
// Display a single video
$play ='<iframe width="'.$this->width.'" height="'.$this->height.'" src="http://www.youtube.com/embed/'.$test.'?rel=0" frameborder="0" allowfullscreen></iframe>';
}
if($youtube_list==true){
// Display a video play list.
$youtube_video = true;
$play = '<iframe width="'.$this->width.'" height="'.$this->height.'" src="http://www.youtube.com/embed/videoseries?list='.$test.'" frameborder="0" allowfullscreen></iframe>';
}
if($youtube_video == false){
// We are unable to determine the video.
$play = $this->error;
}
} else {
// This is not a valid youtube requeust
$play = $this->error;
}
// Return the results
return $this->playVideo=$play;
}
}
And this is how you can use the class:
<?php
include_once("youtube.php");
$video = new YouTube(320,240);
// playlist
$video->video = "http://www.youtube.com/playlist?list=PL6F5E9FD99326F98C";
$youtube = $video->playVideo();
echo $youtube . "<hr>";
// 1 video
$video->video="http://www.youtube.com/watch?v=uTA5bLxfvwo";
$youtube = $video->playVideo();
echo $youtube;
// shared link
echo "<hr>";
$video = new YouTube(420,315);
$video->video="http://youtu.be/VRl2XFJ_tg4";
$youtube = $video->playVideo();
echo $youtube;
// invalid request
$video->video = "http://wrongvideo.com?nothere";
// Your own error message
$video->error ="<br>Video not found :-(<br>" ;
$youtube = $video->playVideo();
echo $youtube;
?>
demo page:
http://www.heathernova.us/youtube.php
TTFN
Cynthia Fridsma
I know this is an old question but I wrote a package for this purpose, I will put it here maybe someone needs this.
composer require smoqadam/youtube-video-info:dev-master
$details = $video->getDetails();
echo $details->getVideoId();
echo $details->getTitle();
echo $details->getThumbnails();
echo $details->getViewCount();
echo $details->getRating();
more info in here
Here is a method that can return several pieces of information:
<?php
extension_loaded('openssl') or die('openssl');
$http['method'] = 'POST';
$http['header'] = 'Content-Type: application/json';
$http['content'] = <<<eof
{
"videoId": "lTGjFpjOIP8", "context": {
"client": {"clientName": "WEB", "clientVersion": "1.19700101"}
}
}
eof;
$con = stream_context_create(['http' => $http]);
$key = '?key=AIzaSyAO_FJ2SlqU8Q4STEHLGCilw_Y9_11qcW8';
echo file_get_contents(
'https://www.youtube.com/youtubei/v1/player' . $key, context: $con
);
From oembed:
$video_url = 'https://www.youtube.com/watch?v=bHQqvYy5KYo';
$url = 'http://www.youtube.com/oembed?format=json&url=' . $video_url;
$json = json_decode(file_get_contents($url), true);
print_r($json); // it will return details of video
function to get video id from video url
function getYTid($ytURL) {
$ytvIDlen = 11; // This is the length of YouTube's video IDs
// The ID string starts after "v=", which is usually right after
// "youtube.com/watch?" in the URL
$idStarts = strpos($ytURL, "?v=");
// In case the "v=" is NOT right after the "?" (not likely, but I like to keep my
// bases covered), it will be after an "&":
if($idStarts === FALSE)
$idStarts = strpos($ytURL, "&v=");
// If still FALSE, URL doesn't have a vid ID
if($idStarts === FALSE)
die("YouTube video ID not found. Please double-check your URL.");
// Offset the start location to match the beginning of the ID string
$idStarts +=3;
// Get the ID string and return it
$ytvID = substr($ytURL, $idStarts, $ytvIDlen);
return $ytvID;
}
and then
$video = getYTid($videourl);
$video_feed = file_get_contents("http://gdata.youtube.com/feeds/api/videos/$video");
$sxml = new SimpleXmlElement($video_feed);
//set up nodes
$namespaces = $sxml->getNameSpaces(true);
$media = $sxml->children($namespaces['media']);
$yt = $media->children($namespaces['yt']);
$yt_attrs = $yt->duration->attributes();
//vars
$video_title = $sxml->title;
$video_description = $sxml->content;
$video_keywords = $media->group->keywords;
$video_length = $yt_attrs['seconds'];
Related
I need to obtain a file's meta-data from just using the Google Drive FileID only. The assumption being that I don't have knowledge of the folderID. By just using the FileID the API just returns null for the filesize but the correct values for the other meta-data.
Just to be clear, I can get all the meta data (including filesize) by processing the files within a folder, for example:
<?php
/*
* With this example I have the folderID from a Google Drive shared link
*/
$folderId = "https://drive.google.com/drive/folders/1ggWcacF9qQroZOkhfb3tTEwvBzclccwI?usp=share_link";
$Auth = new Oauth2Authentication();
$client = $Auth->GetClient();
$client = $Auth->initializeClient($client);
$drive = new DriveService($client);
$pattern = "/[-\w]{25,}(?!.*[-\w]{25,})/";
if (preg_match($pattern, $folderId, $match)); // grabs folderID from the url
/*
* Now list the folder contents
*/
$responseFiles = $drive->ListFiles($match[0]);
$files = $responseFiles['files'];
foreach ($files as $file) {
$id[] = $file->id;
$name[] = $file->name;
$mimeType[] = $file->mimeType;
$size[] = $file->size; //<- correct file size
};
$drive->DownloadFile($id, $name, $size);
The code below, which I need to work, returns most of the meta-data except that, as stated above, the $file->size is always null. My DownloadFile method needs the file size so that it can better handle large files. Is there anyone who can help me better understand the problem?
<?php
/*
* With this example I have the only the fileID from a Google Drive shared link
*/
$fileId = "https://drive.google.com/file/d/1EjNk1ijPLKJMwXfzkEIG487HFzx0v80v/view?usp=share_link";
$Auth = new Oauth2Authentication();
$client = $Auth->GetClient();
$client = $Auth->initializeClient($client);
$drive = new DriveService($client);
$pattern = "/[-\w]{25,}(?!.*[-\w]{25,})/";
if (preg_match($pattern, $fileId, $match)); // grabs fileID from the url
/*
* Now get the file meta data and download the file
*/
$fileId = $match[0];
$service = new Google_Service_Drive($client);
$file = $service->files->get($fileId);
$drive->DownloadFile($file->id, $file->name, $file->size); //$file->size is always null
Update: Thanks to #DaImTo I was pointed in the right direction. The full fileID's details can be obtained by adding
array('fields' =>'*')
to the argument list of the $service->files->get method, i.e.
$file = $service->files->get($fileId,array('fields' =>'*'));
Files.list retuns a list of files with limited response.
files": [
{
"kind": "drive#file",
"mimeType": "application/vnd.google-apps.spreadsheet",
"id": "1x8-vD-X2wltablGF22Lpwup8VtxNY",
"name": "Experts Activity Dump go/ExpertsActivities"
},
To get the file size either tweek the optimal parameter fields or just use .
Fileds=*
or
$responseFiles = $drive->ListFiles($match[0],array('fields' =>'*'))
I am having trouble getting Cloudfront videos to play when using a signed url. If I do NOT require a signed URL, everything works fine. Here is the code that signs the url:
function rsa_sha1_sign($policy, $private_key_filename)
{
$signature = "";
// load the private key
$fp = fopen($private_key_filename, "r");
$priv_key = fread($fp, 8192);
fclose($fp);
//echo $priv_key;
$pkeyid = openssl_get_privatekey($priv_key);
// compute signature
openssl_sign($policy, $signature, $pkeyid);
// free the key from memory
openssl_free_key($pkeyid);
//echo $signature;
return $signature;
}
function url_safe_base64_encode($value)
{
$encoded = base64_encode($value);
// replace unsafe characters +, = and / with
// the safe characters -, _ and ~
return str_replace(
array('+', '=', '/'),
array('-', '_', '~'),
$encoded);
}
// No restriction
$keyPairId = "KEYPAIRID-DIST-NOT-REQUIRING-SIGNEDURL";
$download_url = "http://URL-DIST-NOT-REQUIRING-SIGNEDURL.cloudfront.net/myvideo.mp4";
//This is just a flag to aid in switching between the 2 testing distributions
if($restrict) {
$key_pair_id = "KEYPAIRID-DIST-REQUIRING-SIGNEDURL"";
$download_url = "http://URL-DIST-REQUIRING-SIGNEDURL.cloudfront.net/myvideo.mp4";
}
$DateLessThan = time() + (24*7*60*60);
$policy = '{"Statement":[{"Resource":"'.$download_url.'","Condition":{"DateLessThan":{"AWS:EpochTime":'.$DateLessThan.'}}}]}';
$private_key_file = "/path/to/privatekey.pem";
$signature = rsa_sha1_sign($policy, $private_key_file);
$signature = url_safe_base64_encode($signature);
$final_url = $download_url.'?Policy='.url_safe_base64_encode($policy).'&Signature='.$signature.'&Key-Pair-Id='.$key_pair_id;
echo $final_url;
In the above, if I use the Cloudfront distribution that requires a signed URL (by passing in $restrict=1) then I get an error, "Video not found". In console I see that the GET request for the video was canceled (Status Text: cancelled... weirdly I see this twice). If I use the Distribution that doe NOT require a signed URL everything works fine and the video loads correctly.
What am I missing? The distributions are identical except for the requiring of the signed URL and they both use the same Amazon S3 bucket source for the video.
The player is flowplayer(HTML5) but since it works fine without the signed url I would assume the player isn't the problem.
Please see my answer here: Amazon S3 signed url not working with flowplayer
Hopefully that will help.
In my case, I needed to remove the "mp4:" prefix before signing the url, and then add it back on again.
This is the function pulled out of an old WP plugin for returning a signed Amazon S3 URL, but I can't get it to work! When I visit the signed URL it returns, I get this:
The request signature we calculated does not match the signature you provided. Check your key and signing method.
function s3Url($text) {
$AWS_S3_KEY = 'KEY';
$AWS_S3_SECRET = 'SECRET';
$tag_pattern = '/(\[S3 bucket\=(.*?)\ text\=(.*?)\](.*?)\[\/S3\])/i';
define("AWS_S3_KEY", $AWS_S3_KEY); // replace this with your AWS S3 key
define("AWS_S3_SECRET", $AWS_S3_SECRET); // replace this with your secret key.
$expires = time()+get_option('expire_seconds');
if (preg_match_all ($tag_pattern, $text, $matches)) {
for ($m=0; $m<count($matches[0]); $m++) {
$bucket = $matches[2][$m];
$link_text = $matches[3][$m];
$resource = $matches[4][$m];
$string_to_sign = "GET\n\n\n$expires\n/".str_replace(".s3.amazonaws.com","",$bucket)."/$resource";
//$string_to_sign = "GET\n\n\n{$expires}\n/{$bucket}/{$resource}";
$signature = urlencode(base64_encode((hash_hmac("sha1", utf8_encode($string_to_sign), AWS_S3_SECRET, TRUE))));
$authentication_params = "AWSAccessKeyId=".AWS_S3_KEY;
$authentication_params.= "&Expires={$expires}";
$authentication_params.= "&Signature={$signature}";
$tag_pattern_match = "/(\[S3 bucket\=(.*?)\ text\={$link_text}\]{$resource}\[\/S3\])/i";
if(strlen($link_text) == 0)
{
$link = "http://{$bucket}/{$resource}?{$authentication_params}";
}
else
{
$link = "<a href='http://{$bucket}/{$resource}?{$authentication_params}'>{$link_text}</a>";
}
$text = preg_replace($tag_pattern_match,$link,$text);
}
}
return $text;
}
The example provided in the Amazon AWS PHP SDK: sdk-latest\sdk-1.3.5\sdk-1.3.5\_samples\cli-s3_get_urls_for_uploads.php the following code works quite well:
/* Execute our queue of batched requests. This may take a few seconds to a
few minutes depending on the size of the files and how fast your upload
speeds are. */
$file_upload_response = $s3->batch()->send();
/* Since a batch of requests will return multiple responses, let's
make sure they ALL came back successfully using `areOK()` (singular
responses use `isOK()`). */
if ($file_upload_response->areOK())
{
// Loop through the individual filenames
foreach ($individual_filenames as $filename)
{
/* Display a URL for each of the files we uploaded. Since uploads default to
private (you can choose to override this setting when uploading), we'll
pre-authenticate the file URL for the next 5 minutes. */
echo $s3->get_object_url($bucket, $filename, '5 minutes') . PHP_EOL . PHP_EOL;
}
}
I was wondering if there is a way, after all the changes in youtube in the last months, to write a script that enables downloading videos?
I'm aware of the fact that the methods that worked a year ago, are no longer relevant.
Yes there is still a way. I'm not going to write the whole script for you, but I'll at least write you the function that provides the download link from YouTube.
Alright, you're going to need CURL installed to do this.
/* Developed by User - WebEntrepreneur # StackOverlow.com */
function ___get_youtube_video($youtube_url)
{
if(eregi('youtube.com', $youtube_url))
{
preg_match('/http:\/\/(.+)youtube.com\/watch(.+)?v=(.+)/', $youtube_url, $youtube_id_regex);
$youtube_id = ($youtube_id_regex[3]) ? $youtube_id_regex[3] : '';
if(!$youtube_id)
{
return INVALID_YOUTUBE_ID;
}
if(eregi('\&', $youtube_id))
{
$youtube_id_m = explode('&', $youtube_id);
foreach($youtube_id_m as $slices)
{
$youtube_id = $slices;
break;
}
}
} else {
$youtube_id = ($youtube_url);
}
$ping = ___get_curl("http://www.youtube.com/watch?v={$youtube_id}&feature=youtu.be");
if(!$ping)
{
return YOUTUBE_UNAVAILABLE;
}
$ping_scan = nl2br($ping);
$ping_scan = explode('<br />', $ping_scan);
if(eregi('= null', $ping_scan[36]) or !$ping_scan[36])
{
return YOUTUBE_TOO_MANY_REQUESTS;
}
$ping_scan = str_replace("\n", "", $ping_scan[36]);
$ping_scan = str_replace(" img.src = '", "", $ping_scan);
$out[1] = str_replace("';", "", $ping_scan);
$sub_ping = ___get_curl("http://gdata.youtube.com/feeds/api/videos/{$youtube_id}");
preg_match('/<title type=\'text\'>(.+)<\/title>/', $sub_ping, $inout);
$inout = $inout[1];
if(!$out[1])
{
return VERSION_EXPIRED;
}
$out[1] = str_replace("generate_204", "videoplayback", $out[1]);
$out[1] = str_replace("\\/", "/", $out[1]);
$out[1] = rawurldecode($out[1]);
if($inout)
{
$out[1] .= "&file=".urlencode($inout).".mp4";
$filename = urlencode($inout).".mp4";
}
header("Content-Disposition: attachment; filename=\"".$filename."\"");
flush();
return ___get_curl($out[1]);
}
function ___get_curl($url)
{
if(!function_exists("curl_setopt"))
{
return CURL_NOT_INSTALLED;
}
$ch=curl_init();
curl_setopt($ch,CURLOPT_USERAGENT, "YouTube Video Downloader");
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,0);
curl_setopt($ch,CURLOPT_HEADER,0);
curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);
curl_setopt($ch,CURLOPT_TIMEOUT,100000);
$curl_output=curl_exec($ch);
$curlstatus=curl_getinfo($ch);
curl_close($ch);
return $curl_output;
}
Now, let me talk about the code. After around 30 minutes of research, I managed to hack their algorithm however it does have very strict boundaries which they've set in now.
As each request made for a video download is only tied to the IP, you would need to stream it out of your bandwidth which is what the script above does. However, sites like keepvid.com use Java to grab the download url and stream it to the user. I've also put in my own YouTube ID grabber which is very handy for these kind of tools.
Please acknowledge that as stated, YouTube does always change their algorithms and usage of this tool is at your own risk. I am not held liable for any damages made to YouTube.
Hope it sets a good boundary for you, spent a while making it.
I'm trying to get the video data from this youtube playlist feed and add the interesting data to an array and use that later, but as you can see from the feed some videolinks are "dead" and that results in problems for my code.
The error I get is "Node no longer exists" when I try to access $attrs['url']. I've tried for hours to find a way to check if the node exists before I access it but I have no luck.
If anyone could help me to either parse the feed some other way with the same result or create a if-node-exists check that works I would be most happy. Thank you in advance
$url = 'http://gdata.youtube.com/feeds/api/playlists/18A7E36C33EF4B5D?v=2';
$sxml = simplexml_load_file($url);
$i = 0;
$videoobj;
foreach ($sxml->entry as $entry) {
// get nodes in media: namespace for media information
$media = $entry->children('http://search.yahoo.com/mrss/');
// get video player URL
$attrs = $media->group->player->attributes();
$videoobj[$i]['url'] = $attrs['url'];
// get video thumbnail
$attrs = $media->group->thumbnail[0]->attributes();
$videoobj[$i]['thumb'] = $attrs['url'];
$videoobj[$i]['title'] = $media->group->title;
$i++;
}
if ($media->group->thumbnail && $media->group->thumbnail[0]->attributes()) {
$attrs = $media->group->thumbnail[0]->attributes();
$videoobj[$i]['thumb'] = strval($attrs['url']);
$videoobj[$i]['title'] = strval($media->group->title);
}
SimpleXML's methods always return objects, which are themselves linked to the original document (some internal thingy related to libxml.) If you want to store that data for later use, cast it as a string, like this:
$videoobj[$i]['url'] = (string) $attrs['url'];
$videoobj[$i]['thumb'] = (string) $attrs['url'];
$videoobj[$i]['title'] = (string) $media->group->title;