• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1560
  • Last Modified:

retrieve Video from Youtube or any video sharing site and play on my site using URL

Hi, I have noticed that many sites have a way for a user to take the URl from a video on You tube or any other video sharing site and then click submit on a form and the video will display on the current site only with the watermark from the previous site.  

How can i do this....I want to input a URL to a video and have it display on my site.   Facebook and many other networks do this .... I would like to do this in Classic ASP...but if I cant...how can I achieve this in PHP???
0
Devario Johnson
Asked:
Devario Johnson
1 Solution
 
XorlevCommented:
Here's some working code in PHP.
<?php
// Need more memory
ini_set('memory_limit', 32*1024*1024);
 
// URL of video
$url = 'http://www.youtube.com/watch?v=qq4VyQDQRDQ';
 
// Grab YouTube page
$page = file_get_contents($url);
 
// Grab video_id and session stuff.
preg_match('!video_id=.*&t=[^\"&]*!', $page, $matches);
 
// Download video
$file = file_get_contents('http://youtube.com/get_video?' . $matches[0]);
 
// Grab title of page
preg_match('!<title>(.*?)</title>!', $page, $matches);
 
// Video filename
$filename = strtolower($matches[1]);
$filename = str_replace(' ', '_', $filename);
 
// Write out video file.
file_put_contents($filename . '.flv', $file);
 
// We're done
echo 'Done';
?>

Open in new window

0
 
Devario JohnsonSoftware EngineerAuthor Commented:
Thanks for the code snipplet....however I got this error at line 25

Fatal error: Call to undefined function: file_put_contents() in /home/content/d/u/t/dutty05/html/test/index.php on line 25



// Write out video file.
file_put_contents($filename . '.flv', $file);

Open in new window

0
 
XorlevCommented:
You're using PHP4 then, you'll have to use fwrite() and the other file functions.

http://us3.php.net/function.fwrite

Make sure the directory you're trying to write into is writable as well.

Instead of file_put_contents, use this. Should work, but not tested.
// Open file handle
$handle = fopen($filename . 'flv', 'w+');
 
// Write data
fwrite($handle, $file);
 
// Close file handle
fclose($handle);

Open in new window

0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
marchentCommented:
http://icfun.blogspot.com/2008/03/perlphp-code-to-download-youtube-video.html

Here is you'll find a PHP function that returns the download link. Also example from where the link extracted.
If you search google, you'll find many more like this.
0
 
Devario JohnsonSoftware EngineerAuthor Commented:
Hi guys thanks for the suggestions...but I dont actuallly want to download the video.

On Facebook if you paste the url in the then click submit it does it within  2 seconds.....

it posts the video to the site but it doesnt look like it actually downloads it to your server to be played....
it looks like it just links to it but actually plays it on your site... more like an over grown EMBED.  

Im sorry for not being clearer before.  

I would like to input a url to a video and have it show up on my site as if i EMBEDED it.  Lots of sites do it...Im trying to figure out how now myself.

Ive been searching google and the only examples I could find are how to download.  which is not what I want.

Thanks again
0
 
Devario JohnsonSoftware EngineerAuthor Commented:
You know what guys, after reading my previous post ... I think I just solved my own question...

all id need to do is to have an embed code pass with the post variable that would be the direct link to a video...and that way it would stay on the server that it was oribinally on and then post to my site ... I guess you can call it embedding????  but after thinking about this ... the only issue would be getting the image for the video...any suggestions???
0
 
XorlevCommented:
If you use the YouTube embed URL then it will take care of it.
0
 
Devario JohnsonSoftware EngineerAuthor Commented:
I dont want to use the Embed code....I want to use the embed link...

I dont want to allow html in input fields....but when u say embed URL if you are referring to the link taht is above the embed code on youtube on the side of the video then yes....that is what I am referring to.


how can i get the video to play on my site with the embed LINK (not embed code) and have a thumbnail
0
 
XorlevCommented:
Okay, so what you do is use the youtube embed code in your application, and just cherrypick the id.


<?php
// URL of video
$url = 'http://www.youtube.com/watch?v=lhoPxMIQJA8<script>alert("hi");</script>&feature=related';
 
// Grab video_id, we only want a-zA-Z0-9
preg_match('!v=([a-zA-Z0-9]*)!', $url, $matches);
 
echo '<object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/' . $matches[1] . '&hl=en"></param><embed src="http://www.youtube.com/v/' . $matches[1] . '&hl=en" type="application/x-shockwave-flash" width="425" height="344"></embed></object>';
?>

Open in new window

0
 
XorlevCommented:
I made a mistake, change this line:
preg_match('!v=([a-zA-Z0-9]*)!', $url, $matches);
 to:
preg_match('!v=([a-zA-Z0-9-_]*)!', $url, $matches);

I had forgotten that YouTube video ids included dashes and underscores.
0
 
Devario JohnsonSoftware EngineerAuthor Commented:
ok thanks I will try this and get back to you in the morning...If this works (as im sure it wil)  i will modify it to work with all sites
0
 
Devario JohnsonSoftware EngineerAuthor Commented:
Awsome....your snipplet works to perfection...here is a working link

http://www.duttymedia.com/php_youtubetest/index.html

Now here is my Follow Up Question...is there a way i can get the video Information?  Like the Name, duration and Thumbnail?

Ultimately i will be posting the urls to a database and then calling them up to my site from there thus having a really neat app.

Any Ideas?

Oh and for anyone else who may be wanting to do this here are my files
(Index.html)
--------------------------
<html>
<head>
<title>
Paste the video URL here
</title>
<LINK href="yt.css" rel="stylesheet" type="text/css">
</head>
<body>
<div class="case">
	<form action="post.php" method="post">
	<div class="container">
	<div class="left">You Tube Video Link:</div>
	<div class="right"><input type="text/css" size="30" name="vidlink" id="vidlink"></div>
</div>
<div class="container2">
	<div class="left"></div>
	<div class="right"><input type="submit" value="Submit"</div>
</div>
</div>
</form>
</body>
</html>
--------------------------
(yt.css)
--------------------------
body {
	text-align:center;
	vertical-align:middle;
	color:#999999;
	font-family:arial, helvetica, impact;	
	margin:0 auto;
}
.case {
	vertical-align:middle;
	text-align:center;
	overflow:auto;	
	width:800px;	
	margin:0 auto;
	margin-top:200px;
}
.container {
	padding:10px;
	width:600px;	
	background-color:#eeeeee;
	border-color:#cccccc;
	border-style:solid;
	border-width:thin;
	vertical-align:middle;
	border-bottom:none;
	height:40px;
}
 
.container2 {
	padding:10px;
	width:600px;	
	background-color:#eeeeee;
	border-color:#cccccc;
	border-style:solid;
	border-width:thin;
	vertical-align:middle;
	border-top:none;
	height:40px;
}
 
.container2 .left {
	border-color:#eeeeee;
}
 
.left {
	float:left;
	width:250px;
	text-align:right;
	/*border-color:#cccccc;
	border-style:solid;
	border-width:thin;*/
	width:240px;
	padding:10px;	
	vertical-align:middle;
}
 
.right {
	float:right;width:250px;
	text-align:left;
	/*border-color:#cccccc;
	border-style:solid;
	border-width:thin;*/
	width:240px;
	padding:10px;	
	vertical-align:middle;
}
--------------------------
(post.php)
--------------------------
<?php
// URL of video
$url = $_POST["vidlink"];
$url = $url . '<script>alert("hi");</script>&feature=related';
 
// Grab video_id, we only want a-zA-Z0-9
preg_match('!v=([a-zA-Z0-9-_]*)!', $url, $matches); 
echo '<object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/' . $matches[1] . '&hl=en"></param><embed src="http://www.youtube.com/v/' . $matches[1] . '&hl=en" type="application/x-shockwave-flash" width="425" height="344"></embed></object>';
?>
 
--------------------------

Open in new window

0
 
XorlevCommented:
The thumbnail (for Youtube at least) appears to be http://i.ytimg.com/vi/video_id/default.jpg.

As for the video length, it's in the JS. Here's a new improved code with both. I'm not sure if the thumbnail url will stick to that URL, but it seems like it.
<?php
// URL of video
$url = 'http://youtube.com/watch?v=Mgoy4S7fFmc';
 
// Get page
$page = file_get_contents($url);
 
// Find time (in seconds)
preg_match('/"l": ([0-9]{2,4}),/', $page, $time);
$time = $time[1];
 
// Grab clean video id
preg_match('/"video_id": "([a-zA-Z0-9-_]*)",/', $page, $video_id);
$video_id = $video_id[1];
 
// Convert to mins:seconds
$newtime = floor($time / 60);
$time -= $newtime*60;
$newtime = $newtime . ':' . $time;
 
echo $newtime;
 
// Thumbnail URL
$thumburl = 'http://i.ytimg.com/vi/' . $video_id . '/default.jpg';
 
echo '<img src="' . $thumburl . '" alt="' . $video_id . '" />';
 
echo '<object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/' . $video_id . '&hl=en"></param><embed src="http://www.youtube.com/v/' . $video_id . '&hl=en" type="application/x-shockwave-flash" width="425" height="344"></embed></object>';
?>

Open in new window

0
 
XorlevCommented:
Should probably make this:
([0-9]{2,4})

([0-9]{1,4})

For those freakishly short videos.
0
 
Devario JohnsonSoftware EngineerAuthor Commented:
Thanks for all the assistance!
0
 
Devario JohnsonSoftware EngineerAuthor Commented:
For those who may be wanting to do this in Classic ASP / VBScript here is the code



---------------------
(yourform.html)
---------------------
<html>
<head>
<title>
Paste the video URL here
</title>
<LINK href="yt.css" rel="stylesheet" type="text/css">
</head>
<body>
<div class="case">
	<form action="post.asp" method="post">
	<div class="container">
	<div class="left">You Tube Video Link:</div>
	<div class="right"><input type="text/css" size="30" name="vidlink" id="vidlink"></div>
</div>
<div class="container2">
	<div class="left"></div>
	<div class="right"><input type="submit" value="Submit"</div>
</div>
</div>
</form>
</body>
</html>
---------------------
post.asp
---------------------
<%@language = "VBScript"%>
<% OPTION EXPLICIT %>
<%
 
Dim URL, strVidLink, flaVid, imgThumb, amps
 
strVidLink = Request.Form("vidlink")
URL = strVidLink
amps = inStr(URL, "&")
amps = amps - 1
Response.Write "amps: " & amps & "<br/>"
URL = Left(URL, amps)
URL = Replace(URL, "http://www.youtube.com/watch?v=", "")
URL = Replace(URL, "http://youtube.com/watch?v=", "")
Response.Write "URL: " & URL
 
'Video 
flaVid = "<object width=" & """425""" & "height=" & """344""" & "><param name=" & """movie""" & "value=" & """http://www.youtube.com/v/" & URL & "&hl=en""" & "></param><embed src=" & """http://www.youtube.com/v/" & URL & "&hl=en""" & "type=" & """application/x-shockwave-flash""" & "width=" & """425""" & "height=" & """344""" & "></embed></object>"
 
'Thumbnail
imgThumb = "<img src=http://i.ytimg.com/vi/" & URL & "/default.jpg />"
 
%>
 
<table>
<tr><td>
 
<%=flaVid%>
<%=imgThumb%>
 
</td></tr>
</table>

Open in new window

0
 
Devario JohnsonSoftware EngineerAuthor Commented:
Thanks alot for your help with this Xorlev!  Now everyone in ASP world and PHP world can do this :)
0
 
Jeremy DaleySenior DeveloperCommented:
just to throw this out there, i'm writing my own utility for something similar to this, and while i needed this code for getting the flv, i plan on rewriting this using cURL. look at the performance comparisons here:
http://us.php.net/manual/en/ref.curl.php#75126
0
 
Devario JohnsonSoftware EngineerAuthor Commented:
access times are very important
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now