Accueil > > > CLASSE FFMPEG DE GESTION DE VIDÉOS
CLASSE FFMPEG DE GESTION DE VIDÉOS
Information sur la source
Description
Suite à une ancienne source j'ai souhaité reprendre complètement celle ci et la transformer en classe permettant de gérer des vidéos grâce à ffmpeg. Qu'est ce que ffmpeg ? C'est une bibliothèque linux (développée depuis aussi sur windows) servant à gérer les flux audio et vidéo. Cela permet notamment l'encodage de vidéos au format flv, le redimensionnement de vidéos... La documentation officielle de ffmpeg se trouve ici : http://ffmpeg.mplayerhq.hu/ Afin de vous aider dans votre manipulation de ffmpeg, je vous conseil ce blog : http://www.jcartier.net/spip.php?article36 Pour les fanas de windows, ma source demandera quelques adaptation, mais vous pourrez trouver ffmpeg pour windows à cet endroit : http://chroniques-lunaires.com/rsc/ffmpeg.zip et n'hésitez pas à vous reporter à cette documentation : http://urzhiata.emoc.org/comment-installer-ffmpeg- avec-windows-xp. Comment utiliser cette source? Voici quelques petits exemples : - Créer un fichier au format flv à partir d'une vidéo $Video = new Video("test.avi"); $Video->set_encoding_vformat("f lv"); $Video->set_encoding_afreq("44100"); $Video- >encode("test.flv"); - Fixer la durée d'une vidéo $Video = new Video("test.avi"); $Video->set_encoding_duration(" 00:00:10"); // fixe la durée maximum à 10 secondes $Video->encode("test2.avi"); - Fixer les dimensions d'une vidéo $Video = new Video("test.avi"); $Video->set_encoding_vsize("300 x200"); // Redimensionne en 300px par 200px $Video->encode("test2.avi"); - Extraire une image de la vidéo $Video = new Video("test.avi"); $Video->get_image("10","vignett e.jpg","300x200");
Source
- <?php
- /*
- * @ name : video.php
- * @ description : a class used to encode, decode, resize and reformat videos...
- * @ author : Yaug - Manuel Esteban
- * @ contact : yaug@caramail.com
- * @ date : 03/01/2008
- */
-
- class Video{
-
- public $video_file, $duration, $bitrate, $video_format, $audio_format,
- $video_size, $video_fps, $audio_freq, $audio_bitrate,
- $encoding_vformat, $encoding_aformat, $encoding_vcodec, $encoding_acodec,
- $encoding_vsize, $encoding_duration, $encoding_afreq, $encoding_target,
- $encoding_packet_size, $encoding_aspect, $encoding_ac, $encoding_bitrate,
- $encoding_abitrate, $encoding_fps, $encoding_time_position, $encoding_nosound;
- private $video_id;
-
- /*
- * function : __construct
- * @description : Constructor
- * @param : $video
- */
- public function __construct($video){
- $this->video_file = $video;
- $this->video_id = md5($video);
-
- //On récupère les infos de la vidéo
- exec("ffmpeg -i ".$this->video_file." &> ".$this->video_id.".info");
- $this->get_video_info();
- }
-
- /*
- * function : __destruct
- * @description : Destructor
- * @param :
- */
- public function __destruct(){
- if( is_file($this->video_id.".info") ) unlink($this->video_id.".info");
- }
-
- /*
- * function : getextension
- * @description : return the extension of any file
- * @param : $file - name of the file (video.avi)
- */
- public function getextension($file){
- return substr($file,strrpos($file,'.')+1);
- }
-
-
- /*
- * function : get_video_info
- * @description : Extract all the video information with ffmpeg
- */
- private function get_video_info(){
- $handle = fopen($this->video_id.".info","r");
- if($handle){
- while (!feof($handle)) {
- $buffer = fgets($handle, 4096);
- $Line=explode(" ",$buffer);
- //print_r($Line);
- //print("\n");
- switch($Line[2]){
- case 'Duration:':
- $this->duration = $Line[3];
- $this->bitrate = $Line[7];
- case 'Stream':
- if($Line[4]=="Video:"){
- $this->video_format = $Line[5];
- $this->video_size = $Line[7];
- $this->video_fps = $Line[8];
- }elseif($Line[4]=="Audio:"){
- $this->audio_format = $Line[5];
- $this->audio_freq = $Line[6];
- $this->audio_bitrate= $Line[9];
- }
- }
- }
- fclose($handle);
- return true;
- }
- return false;
- }
-
-
- /*
- * function : set_encoding_vformat()
- * @description : Set the format for video encoding
- * @param : $format (the format of the output video)
- */
- public function set_encoding_vformat($format){
- $this->encoding_vformat = $format;
- }
-
-
- /*
- * function : set_encoding_aformat()
- * @description : Set the format for audio encoding
- * @param : $format (the format of the output audio)
- */
- public function set_encoding_aformat($format){
- $this->encoding_aformat = $format;
- }
-
-
- /*
- * function : set_encoding_vcodec()
- * @description : Set the codec for video encoding, be carefull with the codec name
- * @param : $codec (the codec of the output video)
- */
- public function set_encoding_vcodec($codec){
- $this->encoding_vcodec = $codec;
- }
-
-
- /*
- * function : set_encoding_acodec()
- * @description : Set the codec for audio encoding, be carefull with the codec name
- * @param : $codec (the codec of the output audio)
- */
- public function set_encoding_acodec($codec){
- $this->encoding_acodec = $codec;
- }
-
-
- /*
- * function : set_encoding_vsize()
- * @description : Set the size of the output video
- * @param : $width (width of the output video)
- * @param : $height (height of the output video)
- */
- public function set_encoding_vsize($size){
- $this->encoding_vsize = $size;
- }
-
-
- /*
- * function : set_encoding_duration()
- * @description : Set the size of the output video
- * @param : $duration (duration of the output like 00:00:10)
- */
- public function set_encoding_duration($duration){
- $this->encoding_duration = $duration;
- }
-
-
- /*
- * function : set_encoding_afreq()
- * @description : Set the audio frequence of the output video
- * @param : $freq (audio frequence of the output like 00:00:10)
- */
- public function set_encoding_afreq($freq){
- $this->encoding_afreq = $freq;
- }
-
-
- /*
- * function : set_encoding_target()
- * @description : Specify target file type
- * @param : $target (Specified target file type)
- */
- public function set_encoding_target($target){
- $this->encoding_target = $target;
- }
-
-
- /*
- * function : set_encoding_packet_size()
- * @description : Set packet size in bits.
- * @param : $weight (Size)
- */
- public function set_encoding_packet_size($weight){
- $this->encoding_packet_size = $weight;
- }
-
-
- /*
- * function : set_encoding_aspect()
- * @description : Set aspect ratio (4:3, 16:9 or 1.3333, 1.7777).
- * @param : $ratio (Ratio of the generated video)
- */
- public function set_encoding_aspect($ratio){
- $this->encoding_aspect = $ratio;
- }
-
-
- /*
- * function : set_encoding_ac()
- * @description : Set the number of audio channels
- * @param : $nb (Number of channels)
- */
- public function set_encoding_ac($nb){
- $this->encoding_ac = $nb;
- }
-
-
- /*
- * function : set_encoding_bitrate()
- * @description : Set the video bitrate in bit/s
- * @param : $bitrate
- */
- public function set_encoding_bitrate($bitrate){
- $this->encoding_bitrate = $bitrate;
- }
-
-
- /*
- * function : set_encoding_abitrate()
- * @description : Set the audio bitrate in bit/s
- * @param : $abitrate
- */
- public function set_encoding_abitrate($abitrate){
- $this->encoding_abitrate = $abitrate;
- }
-
-
- /*
- * function : set_encoding_fps()
- * @description : Set frame rate
- * @param : $fps
- */
- public function set_encoding_fps($fps){
- $this->encoding_fps = $fps;
- }
-
-
- /*
- * function : set_encoding_time_position()
- * @description : time position in seconds. hh:mm:ss[.xxx] syntax is also supported.
- * @param : $position
- */
- public function set_encoding_time_position($position){
- $this->encoding_time_position = $position;
- }
-
-
- /*
- * function : set_encoding_nosound()
- * @description : time position in seconds. hh:mm:ss[.xxx] syntax is also supported.
- * @param : $position
- */
- public function set_encoding_nosound(){
- if($this->encoding_nosound)$this->encoding_nosound=false;
- else $this->encoding_nosound=true;
- }
-
-
- /*
- * function : encode()
- * @description : Encode video with defined params
- * @param : $file_name (name of the file created.)
- */
- public function encode($file_name){
- $command = "ffmpeg -y -i ".$this->video_file;
- if($this->encoding_vformat) $command.=" -f ".$this->encoding_vformat;
- if($this->encoding_vcodec) $command.=" -vcodec ".$this->encoding_vcodec;
- if($this->encoding_acodec) $command.=" -acodec ".$this->encoding_acodec;
- if($this->encoding_vsize) $command.=" -s ".$this->encoding_vsize;
- if($this->encoding_duration) $command.=" -t ".$this->encoding_duration;
- if($this->encoding_fps) $command.=" -r ".$this->encoding_fps;
- if($this->encoding_bitrate) $command.=" -b ".$this->encoding_bitrate;
- if($this->encoding_nosound) $command.=" -an ";
- if($this->encoding_abitrate) $command.=" -ab ".$this->encoding_abitrate;
- if($this->encoding_afreq) $command.=" -ar ".$this->encoding_afreq;
- if($this->encoding_ac) $command.=" -ac ".$this->encoding_ac;
- if($this->encoding_target) $command.=" -target ".$this->encoding_target;
- if($this->encoding_packet_size) $command.=" -ps ".$this->encoding_packet_size;
- if($this->encoding_aspect) $command.=" -aspect ".$this->encoding_aspect;
- if($this->encoding_time_position) $command.=" -ss ".$this->encoding_time_position;
- $command.=" $file_name";
-
- print("commande executée : $command");
-
- shell_exec($command);
- }
-
-
- /*
- * function : get_image()
- * @description : Get an image for a specific frame of a video
- * @param : $frame (00:00:10.0002)
- * @param : $image_name (name of this image)
- * @param : $size
- */
- public function get_image($frame,$image_name,$size){
- $this->encoding_vformat = "mjpeg";
- $this->encoding_duration = "001";
- $this->encoding_time_position = $frame;
- $this->encoding_vsize = $size;
-
- //We build the image
- $this->encode($image_name);
- }
- }
-
-
- ?>
<?php
/*
* @ name : video.php
* @ description : a class used to encode, decode, resize and reformat videos...
* @ author : Yaug - Manuel Esteban
* @ contact : yaug@caramail.com
* @ date : 03/01/2008
*/
class Video{
public $video_file, $duration, $bitrate, $video_format, $audio_format,
$video_size, $video_fps, $audio_freq, $audio_bitrate,
$encoding_vformat, $encoding_aformat, $encoding_vcodec, $encoding_acodec,
$encoding_vsize, $encoding_duration, $encoding_afreq, $encoding_target,
$encoding_packet_size, $encoding_aspect, $encoding_ac, $encoding_bitrate,
$encoding_abitrate, $encoding_fps, $encoding_time_position, $encoding_nosound;
private $video_id;
/*
* function : __construct
* @description : Constructor
* @param : $video
*/
public function __construct($video){
$this->video_file = $video;
$this->video_id = md5($video);
//On récupère les infos de la vidéo
exec("ffmpeg -i ".$this->video_file." &> ".$this->video_id.".info");
$this->get_video_info();
}
/*
* function : __destruct
* @description : Destructor
* @param :
*/
public function __destruct(){
if( is_file($this->video_id.".info") ) unlink($this->video_id.".info");
}
/*
* function : getextension
* @description : return the extension of any file
* @param : $file - name of the file (video.avi)
*/
public function getextension($file){
return substr($file,strrpos($file,'.')+1);
}
/*
* function : get_video_info
* @description : Extract all the video information with ffmpeg
*/
private function get_video_info(){
$handle = fopen($this->video_id.".info","r");
if($handle){
while (!feof($handle)) {
$buffer = fgets($handle, 4096);
$Line=explode(" ",$buffer);
//print_r($Line);
//print("\n");
switch($Line[2]){
case 'Duration:':
$this->duration = $Line[3];
$this->bitrate = $Line[7];
case 'Stream':
if($Line[4]=="Video:"){
$this->video_format = $Line[5];
$this->video_size = $Line[7];
$this->video_fps = $Line[8];
}elseif($Line[4]=="Audio:"){
$this->audio_format = $Line[5];
$this->audio_freq = $Line[6];
$this->audio_bitrate= $Line[9];
}
}
}
fclose($handle);
return true;
}
return false;
}
/*
* function : set_encoding_vformat()
* @description : Set the format for video encoding
* @param : $format (the format of the output video)
*/
public function set_encoding_vformat($format){
$this->encoding_vformat = $format;
}
/*
* function : set_encoding_aformat()
* @description : Set the format for audio encoding
* @param : $format (the format of the output audio)
*/
public function set_encoding_aformat($format){
$this->encoding_aformat = $format;
}
/*
* function : set_encoding_vcodec()
* @description : Set the codec for video encoding, be carefull with the codec name
* @param : $codec (the codec of the output video)
*/
public function set_encoding_vcodec($codec){
$this->encoding_vcodec = $codec;
}
/*
* function : set_encoding_acodec()
* @description : Set the codec for audio encoding, be carefull with the codec name
* @param : $codec (the codec of the output audio)
*/
public function set_encoding_acodec($codec){
$this->encoding_acodec = $codec;
}
/*
* function : set_encoding_vsize()
* @description : Set the size of the output video
* @param : $width (width of the output video)
* @param : $height (height of the output video)
*/
public function set_encoding_vsize($size){
$this->encoding_vsize = $size;
}
/*
* function : set_encoding_duration()
* @description : Set the size of the output video
* @param : $duration (duration of the output like 00:00:10)
*/
public function set_encoding_duration($duration){
$this->encoding_duration = $duration;
}
/*
* function : set_encoding_afreq()
* @description : Set the audio frequence of the output video
* @param : $freq (audio frequence of the output like 00:00:10)
*/
public function set_encoding_afreq($freq){
$this->encoding_afreq = $freq;
}
/*
* function : set_encoding_target()
* @description : Specify target file type
* @param : $target (Specified target file type)
*/
public function set_encoding_target($target){
$this->encoding_target = $target;
}
/*
* function : set_encoding_packet_size()
* @description : Set packet size in bits.
* @param : $weight (Size)
*/
public function set_encoding_packet_size($weight){
$this->encoding_packet_size = $weight;
}
/*
* function : set_encoding_aspect()
* @description : Set aspect ratio (4:3, 16:9 or 1.3333, 1.7777).
* @param : $ratio (Ratio of the generated video)
*/
public function set_encoding_aspect($ratio){
$this->encoding_aspect = $ratio;
}
/*
* function : set_encoding_ac()
* @description : Set the number of audio channels
* @param : $nb (Number of channels)
*/
public function set_encoding_ac($nb){
$this->encoding_ac = $nb;
}
/*
* function : set_encoding_bitrate()
* @description : Set the video bitrate in bit/s
* @param : $bitrate
*/
public function set_encoding_bitrate($bitrate){
$this->encoding_bitrate = $bitrate;
}
/*
* function : set_encoding_abitrate()
* @description : Set the audio bitrate in bit/s
* @param : $abitrate
*/
public function set_encoding_abitrate($abitrate){
$this->encoding_abitrate = $abitrate;
}
/*
* function : set_encoding_fps()
* @description : Set frame rate
* @param : $fps
*/
public function set_encoding_fps($fps){
$this->encoding_fps = $fps;
}
/*
* function : set_encoding_time_position()
* @description : time position in seconds. hh:mm:ss[.xxx] syntax is also supported.
* @param : $position
*/
public function set_encoding_time_position($position){
$this->encoding_time_position = $position;
}
/*
* function : set_encoding_nosound()
* @description : time position in seconds. hh:mm:ss[.xxx] syntax is also supported.
* @param : $position
*/
public function set_encoding_nosound(){
if($this->encoding_nosound)$this->encoding_nosound=false;
else $this->encoding_nosound=true;
}
/*
* function : encode()
* @description : Encode video with defined params
* @param : $file_name (name of the file created.)
*/
public function encode($file_name){
$command = "ffmpeg -y -i ".$this->video_file;
if($this->encoding_vformat) $command.=" -f ".$this->encoding_vformat;
if($this->encoding_vcodec) $command.=" -vcodec ".$this->encoding_vcodec;
if($this->encoding_acodec) $command.=" -acodec ".$this->encoding_acodec;
if($this->encoding_vsize) $command.=" -s ".$this->encoding_vsize;
if($this->encoding_duration) $command.=" -t ".$this->encoding_duration;
if($this->encoding_fps) $command.=" -r ".$this->encoding_fps;
if($this->encoding_bitrate) $command.=" -b ".$this->encoding_bitrate;
if($this->encoding_nosound) $command.=" -an ";
if($this->encoding_abitrate) $command.=" -ab ".$this->encoding_abitrate;
if($this->encoding_afreq) $command.=" -ar ".$this->encoding_afreq;
if($this->encoding_ac) $command.=" -ac ".$this->encoding_ac;
if($this->encoding_target) $command.=" -target ".$this->encoding_target;
if($this->encoding_packet_size) $command.=" -ps ".$this->encoding_packet_size;
if($this->encoding_aspect) $command.=" -aspect ".$this->encoding_aspect;
if($this->encoding_time_position) $command.=" -ss ".$this->encoding_time_position;
$command.=" $file_name";
print("commande executée : $command");
shell_exec($command);
}
/*
* function : get_image()
* @description : Get an image for a specific frame of a video
* @param : $frame (00:00:10.0002)
* @param : $image_name (name of this image)
* @param : $size
*/
public function get_image($frame,$image_name,$size){
$this->encoding_vformat = "mjpeg";
$this->encoding_duration = "001";
$this->encoding_time_position = $frame;
$this->encoding_vsize = $size;
//We build the image
$this->encode($image_name);
}
}
?>
Conclusion
Cette source est toute neuve et est amenée à évoluer, n'hésitez pas à me signaler tout problème ou à me faire part de vos remarques et de vos suggestions.
Merci.
Sources de la même categorie
Commentaires et avis
Discussions en rapport avec ce code source dans le forum
ffmpeg-php [ par buno ]
Bonjour,Je suis actuellement en train de développer un site de gestion de fichiers vidéo. Sur ce site, j'ai besoin d'extraire des informatio
taille max d'un tableau [ par Urukai1 ]
petite question toute bete - jusqu'à combien d'elements de type 'ma cle' => 'ma valeur' peut-on mettre dans un tableau en php sans que c
GEstion dynamique vidéo [ par zzzzzz ]
Bonjour, Alors voila j'aimerai pouvoir laisser libre access à mes membres pour qu'ils puissent ajouter leurs propre vidéos, seulement voi
heritage en php [ par fredericmaill ]
Bonjour, lors de la cration d'un heritage en php 4, classe mere et classe fille, les 2 classes doivent etres l'un en dessous de l'autre ou
Vidéo URL Caché [ par Viper_ ]
Bonjour,j'aimerais savoir si il y a un moyen de "caché" le URL d'un vidéo (par exemple) de manière à ce qu'une page php serve de t
fonction Autoload.... [ par Yemanjah ]
Bonjour , Après de multiples essais , je désespère et viens demander de l'aide ici.J'essaye de met
mails avec pièces jointes [ par refkaben ]
Bonjour!j'ai trouvé dans les codes de phpcs une classe qui permet l'envoi des emails avec des pièces jointes.J'ai adapté la classe 
Problème d'encodage !! [ par Zebra1928 ]
bonne année à tous !!nous avon un problème pour écrire en arabe et latin dans des pages PHP, dans le fichier PHP.INI nous avons ut
objet sqlite [ par jackrichard ]
bonjour a tousdésolé si la question est con mais bon la je pige pas sur un livre de php on me dit que php5 propose la classe sqlite_db
Lecture de Vidéo [ par bbmaster ]
Bonjour, Je suis actuellement en train de construire mon site de mon équipe de jeux vidéo et je voudrais y inséré un script
|
Derniers Blogs
CSS CONTENT STATE SELECTORS (PERSONNAL DRAFT)CSS CONTENT STATE SELECTORS (PERSONNAL DRAFT) par FREMYCOMPANY
Bonjour à tous, Je viens de publier une proposition comprenant 5 pseudo-classes pour le CSS Working Group ayant trait à l'état de chargement d'un élément (ex: IMG,VIDEO,AUDIO,OBJECT pour l'HTML.). Si le c½ur vous en dit, vous pouvez retrouver cette p...
Cliquez pour lire la suite de l'article par FREMYCOMPANY MBA : POURQUOI FAIRE ET COMMENT LE CHOISIR ?MBA : POURQUOI FAIRE ET COMMENT LE CHOISIR ? par ROMELARD Fabrice
Formation initiale Durant la formation, le découpage classique est le suivant (je donnerai les équivalences Suisse lorsque je les connaîtrais) : Ecole primaire jusqu'au Collège : Formation générale permettant d'obtenir les méthodes...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice Y'A DES ERREURS QUI PEUVENT RENDRE LE DéVELOPPEUR VIOLENTY'A DES ERREURS QUI PEUVENT RENDRE LE DéVELOPPEUR VIOLENT par Aleks
Quand on a ce genre d'erreur sans log :
Et bas on a juste envie de choper le gas de Microsoft qu'a développé ça et lui foutre des baffes de Coboye ! ...
Cliquez pour lire la suite de l'article par Aleks [HYPER-V 3] PRéSENTATION DES COMMANDLETS POWERSHELL[HYPER-V 3] PRéSENTATION DES COMMANDLETS POWERSHELL par Pierrick CATRO-BROUILLET
Avec la sortie prochaine de la Beta Consumer Preview de Windows 8, j'avais envie de revenir sur une des fonctionnalités que j'attends le plus et que, en bon geek que je suis, j'utilise déjà : Hyper-V 3 ainsi son module PowerShell.
Il y a déjà pléthor...
Cliquez pour lire la suite de l'article par Pierrick CATRO-BROUILLET IIS7 - COMPRESSION GZIPIIS7 - COMPRESSION GZIP par cyril
La compression GZIP permet d'améliorer les performances de navigation en compressant ce qu'envoie le serveur à un client. Pour comprendre comment cela fonctionne, regardons ce qu'il se passe au niveau HTTP lorsqu'un client tente d'accéder à une ress...
Cliquez pour lire la suite de l'article par cyril
Logiciels
Easy-Planning (1.0.0.1)EASY-PLANNING (1.0.0.1)Basé sur les mêmes principes que MyPlanning, Easy-Planning permet de créer des plannings sous la ... Cliquez pour télécharger Easy-Planning Academy System (17.1.3.0)ACADEMY SYSTEM (17.1.3.0)Logiciel de gestion des établissements.
- élèves/étudiants (inscription, dossier, absence...)
-... Cliquez pour télécharger Academy System COLLECTOR PLUS (3.00B)COLLECTOR PLUS (3.00B)COLLECTOR PLUS version 3.00B est un logiciel utilisant une base de données alimentée par :
- L... Cliquez pour télécharger COLLECTOR PLUS PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO (V7.4)PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO (V7.4)PONAMEDIA TV DEVIENS HELLLOOO FLASH
LA TV SUR VOTRE ORDINATEUR.
Toute une plateforme Multi... Cliquez pour télécharger PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO LettresFaciles 2011 (8.0.0.1)LETTRESFACILES 2011 (8.0.0.1)LettresFaciles est un logiciel facilitant la création et la rédaction de lettres types.
Son inte... Cliquez pour télécharger LettresFaciles 2011
|