views:

9745

answers:

10

Can anyone give me an idea, how can we show or embed a YouTube video if we just have the URL or the Embed code.

Thanks for the help

A: 

You mean you want to grad a video from youtube and upload it to your server OR you just intend to embed a youtube video in your pages?

Akshay
Should be asked as a comment on the original question
Click Upvote
downvote removed then :)
Click Upvote
+2  A: 

If you want to upload videos programatically, check the YouTube Data API for PHP

CMS
Yeah this what i am looking for...thanx for the help
luvboy
A: 

Akshay,

I want to give the usability to users, they can give the URL/Embed code in the respective textbox, then the respective video should come onto page...

Hope i'm clear now

luvboy
If you have the Embed code, you just have to show, print or echo it!
CMS
A: 

luvboy,

If i understand clearly, user provides the URL/code of the Youtube video and then that video is displayed on the page.

For that, just write a simple page, with layout etc.. Copy video embed code from youtube and paste it in your page. Replace embed code with some field, say VideoID. Set this VideoId to code provided by your user.

edit: see answer by Alec Smart.

Akshay
+19  A: 

Hi,

You have to ask users to store the 11 character code from the youtube video.

For e.g. http://www.youtube.com/watch?v=Ahg6qcgoay4

The eleven character code is : Ahg6qcgoay4

You then take this code and place it in your database. Then wherever you want to place the youtube video in your page, load the character from the database and put the following code:-

e.g. for Ahg6qcgoay4 it will be :

<object width="425" height="350" data="http://www.youtube.com/v/Ahg6qcgoay4" type="application/x-shockwave-flash"><param name="src" value="http://www.youtube.com/v/Ahg6qcgoay4" /></object>
Alec Smart
This is very much easy...thanx dude
luvboy
asking the user to extract code from the url is a bad idea, it's very easy to write code to extract it using regex! -1
hasen j
done and was working gr8..thanx Alec
luvboy
@hasen it was an example for luvboy. Ofcourse we can use regex to do it.....
Alec Smart
BTW the video is awesome...
OscarRyz
Thank you this is such a useful solution
Teerasej
+1 for the awesome vid.
ign
thanks everyone :)
Alec Smart
+4  A: 

Use a regex to extract the "video id" after watch?v=

Store the video id in a variable, let's call this variable vid

Get the embed code from a random video, remove the video id from the embed code and replace it with the vid you got.

I don't know how to deal with regex in php, but it shouldn't be too hard

Here's example code in python:

>>> ytlink = 'http://www.youtube.com/watch?v=7-dXUEbBz70'
>>> import re
>>> vid = re.findall( r'v\=([\-\w]+)', ytlink )[0]
>>> vid
'7-dXUEbBz70'
>>> print '''<object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/%s&amp;hl=en&amp;fs=1"&gt;&lt;/param&gt;&lt;param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/%s&amp;hl=en&amp;fs=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed></object>''' % (vid,vid)
<object width="425" height="344"><param name="movie" value="http://www.youtube.com/v/7-dXUEbBz70&amp;hl=en&amp;fs=1"&gt;&lt;/param&gt;&lt;param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/7-dXUEbBz70&amp;hl=en&amp;fs=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"></embed></object>
>>>

The regular expression v\=([\-\w]+) captures a (sub)string of characters and dashes that comes after v=

hasen j
A: 

U CAN simply create a php input form for Varchat date,give it a varchar length of lets say 300. then ask the users to copy and paste the Embed code.,when u view the record, u will view the streamed video.

+3  A: 

If you plan to store YouTube video ids in your database, I suggest that you look at these articles:

To create your own YouTube video player:

PHP example for extracting the parameter is as follows:

<?php
    preg_match(
        '/[\\?\\&]v=([^\\?\\&]+)/',
        'http://www.youtube.com/watch?v=OzHvVoUGTOM&amp;feature=channel',
        $matches
    );
    // $matches[ 1 ] should contain the youtube id
?>
Salman A
+1  A: 

Searching for this same topic I found another method using Javascript an Youtube API's

Directly from: http://code.google.com/apis/ajax/playground/#simple_embed

Loading the API

<script src="http://www.google.com/jsapi" type="text/javascript"></script>

And executing the following javascript code:

  google.load("swfobject", "2.1");
  function _run() {

    var videoID = "ylLzyHk54Z0"
    var params = { allowScriptAccess: "always" };
    var atts = { id: "ytPlayer" };
    // All of the magic handled by SWFObject (http://code.google.com/p/swfobject/)
    swfobject.embedSWF("http://www.youtube.com/v/" + videoID + "&enablejsapi=1&playerapiid=player1",
                       "videoDiv", "480", "295", "8", null, null, params, atts);


  }
  google.setOnLoadCallback(_run);

The complete sample is in the previously referred page http://code.google.com/apis/ajax/playground

OscarRyz
A: 

Here is a way how to upload videos from your website before to embed them: http://www.webdryver.com/php-tutorials/youtube-gdata-php-script-for-video-upload.html Thanks and keep the grate job.