Solved

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

Posted on 2008-06-13
19
1,518 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
19 Comments
 
LVL 3

Expert Comment

by:Xorlev
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 13

Expert Comment

by:marchent
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
If you use the YouTube embed URL then it will take care of it.
0
 
LVL 5

Author Comment

by:Devario Johnson
Comment Utility
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
Comment Utility
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 3

Expert Comment

by:Xorlev
Comment Utility
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
Comment Utility
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
Comment Utility
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 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks for all the assistance!
0
 
LVL 5

Author Comment

by:Devario Johnson
Comment Utility
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
Comment Utility
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:daleyjem
Comment Utility
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
Comment Utility
access times are very important
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

743 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now