Esta é uma função do PHP que retorna um vídeo do YouTube ou MetaCafe. E uma outra função que retorna a imagem miniatura do vídeo do YouTube (120×90 px)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
function embedVideo($url,$width,$height){
   /*
    * RETORNA VIDEOS DO YOUTUBE E METACAFE
    *
    * É POSSÍVEL IMPLEMENTAR MAIS EXPRESSÕES REGULARES
    *
    * é possível adaptar um retorno em string também,
    * aí fica a critério de quem usar a função
    *
    */
 
   if(preg_match("#http://(.*)\.youtube\.com/watch\?v=(.*)(&(.*))?#", $url, $matches)){
      echo '
            <object width="'.$width.'" height="'.$height.'">
               <param name="movie" value="http://www.youtube.com/v/'.$matches[2].'&hl=pt-br&fs=1"></param>
               <param name="allowFullScreen" value="true"></param>
               <param name="allowscriptaccess" value="always"></param>
               <embed src="http://www.youtube.com/v/'.$matches[2].'&hl=pt-br&fs=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="'.$width.'" height="'.$height.'"></embed>
            </object>
            ';
   }elseif(preg_match("#http://www\.metacafe\.com/watch/(([^/].*)/([^/].*))/?#", $url, $matches)){
      echo '<embed flashVars="playerVars=showStats=no|autoPlay=no|videoTitle="  src="http://www.metacafe.com/fplayer/'.$matches[1].'.swf" width="'.$width.'" height="'.$height.'" wmode="transparent" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash"></embed>';
   }
}
 
$youtubeVideo1 = 'http://in.youtube.com/watch?v=Km7PcdMzaN4';
$youtubeVideo2 = 'http://in.youtube.com/watch?v=xNi7QwAL3XY&feature=dir';
$metacafeVideo1 = 'http://www.metacafe.com/watch/2215104/amazing_video/';
$metacafeVideo2 = 'http://www.metacafe.com/watch/2204556/sensational_cars_of_the_future/';
 
embedVideo($youtubeVideo1,425,344);
embedVideo($youtubeVideo2,425,344);
embedVideo($metacafeVideo1,400,348);
embedVideo($metacafeVideo2,400,348);
 
function youtubeImage($url,$tipo='src') {
   $img = '';
 
   // O TAMANHO PADRAO DA IMAGEM DO YOUTUBE É 120x90
 
   if(preg_match("#http://(.*)\.youtube\.com/watch\?v=(.*)(&(.*))?#", $url, $matches)){
      if(isset($matches[2]) && $matches[2]!=''){
         $img = 'http://s2.ytimg.com/vi/'.$matches[2].'/default.jpg';
      }
   }
 
   return $tipo=='src' ? $img : '<img src="'.$img.'" />';
}
 
echo youtubeImage($youtubeVideo1,'img');