Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2008-06-13
19
Medium Priority
?
1,531 Views
Last Modified: 2013-12-13
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
Comment
Question by:Devario Johnson
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
19 Comments
 
LVL 3

Expert Comment

by:Xorlev
ID: 21783906
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
 
LVL 5

Author Comment

by:Devario Johnson
ID: 21784293
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
 
LVL 3

Expert Comment

by:Xorlev
ID: 21784611
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 13

Expert Comment

by:marchent
ID: 21784744
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
 
LVL 5

Author Comment

by:Devario Johnson
ID: 21785537
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
 
LVL 5

Author Comment

by:Devario Johnson
ID: 21785994
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
 
LVL 3

Expert Comment

by:Xorlev
ID: 21786249
If you use the YouTube embed URL then it will take care of it.
0
 
LVL 5

Author Comment

by:Devario Johnson
ID: 21786257
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
 
LVL 3

Expert Comment

by:Xorlev
ID: 21786565
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
 
LVL 3

Expert Comment

by:Xorlev
ID: 21786584
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
 
LVL 5

Author Comment

by:Devario Johnson
ID: 21790961
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
 
LVL 5

Author Comment

by:Devario Johnson
ID: 21794619
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
 
LVL 3

Accepted Solution

by:
Xorlev earned 2000 total points
ID: 21796444
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
 
LVL 3

Expert Comment

by:Xorlev
ID: 21796459
Should probably make this:
([0-9]{2,4})

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

For those freakishly short videos.
0
 
LVL 5

Author Closing Comment

by:Devario Johnson
ID: 31467114
Thanks for all the assistance!
0
 
LVL 5

Author Comment

by:Devario Johnson
ID: 21807982
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
 
LVL 5

Author Comment

by:Devario Johnson
ID: 21808003
Thanks alot for your help with this Xorlev!  Now everyone in ASP world and PHP world can do this :)
0
 
LVL 4

Expert Comment

by:Jeremy Daley
ID: 21907595
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
 
LVL 5

Author Comment

by:Devario Johnson
ID: 22761933
access times are very important
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question