home
/*
$hostpage doit être l'adresse complète de la page Youtube où est diffusée la vidéo désirée.
e.g. http://www.youtube.com/watch?v=i4h02CaJMxw
$output doit être une adresse à laquelle PHP a accès en écriture (voir la documentation de file_put_contents() pour plus d'information).
Les vidéos sur Youtube sont au format flash, l'extension de fichier correspondante est donc .flv
e.g. /var/www/ytvid1.flv
*/
function getYoutubeVideo($hostpage, $output)
{
$hostpage .= "&";
// Obtention de l'identifiant du video
preg_match('/v=(.+?)&+/', $hostpage, $match);
$var_id = $match[1];
// Lecture de la page hôte et extraction de l'élément "t"
$hostcontent = file_get_contents($hostpage);
preg_match('/"t": "(.*?)"/', $hostcontent, $match);
$var_t = $match[1];
// Lecture du fichier distant
$input = file_get_contents("http://www.youtube.com/get_video?video_id=" . $var_id . "&t=" . $var_t);
if (!$input)
{
// Si une erreur survient
echo "Fichier introuvable";
}
else
{
// Écriture du fichier local
file_put_contents($output, $input);
}
}