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
TECHDAYS PARIS 2010 : CONNECTEZ VOS DONNéES à SHAREPOINT 2010 AVEC LES BUSINESS CONNECTIVITY SERVICESTECHDAYS PARIS 2010 : CONNECTEZ VOS DONNéES à SHAREPOINT 2010 AVEC LES BUSINESS CONNECTIVITY SERVICES par ROMELARD Fabrice
Animé par: Gaetan Bouveret et Julien Chomarat Business Connectivity Services (BCS) est dans SharePoint 2010 la version 2 de Business Data Catalog (BDC dans SharePoint 2007). Il s'agit de la solution permettant de visualiser des données provenan...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice [DIVERS] SUIVRE VOS SéRIES PRéFéRéS SUR LA TOILE[DIVERS] SUIVRE VOS SéRIES PRéFéRéS SUR LA TOILE par orion
Comme de nombreux geek, je suis un grand amateur de série TV et je rate régulièrement des épisodes de mes séries préférés. Une solution s'offre à vous avec ce merveilleux site : Tv Gorge - www.tvgorge.com Moteur de recherche à l'appui, vous pouvez ...
Cliquez pour lire la suite de l'article par orion TECHDAYS PARIS 2010 : LA BI DANS SHAREPOINT 2010TECHDAYS PARIS 2010 : LA BI DANS SHAREPOINT 2010 par ROMELARD Fabrice
Animé par: Vincent Bellet et Baptiste Giraudier La BI dans SharePoint 2010, Les nouveaux services d'application dans SP2010 et SQL Server Reporting services 2008 R2. La BI dans SharePoint est généralisée pour tous afin de permettre à tous les coll...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice TECHDAYS PARIS 2010 : PLAN DE MIGRATION VERS SHAREPOINT 2010TECHDAYS PARIS 2010 : PLAN DE MIGRATION VERS SHAREPOINT 2010 par ROMELARD Fabrice
Animé par: Arnault Nouvel et Antoine Dongois Le processus à prendre : Apprendre (découvrir la plateforme) Préparer (documenter l'historique et choisir la méthode de MAJ) Test (Test de MAJ) Implémenter (Effectuer la MAJ) Valid...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice
Logiciels
DB-MAIN (9.1.0)DB-MAIN (9.1.0)DB-MAIN is a data-modeling and data-architecture tool. It is designed to help developers and anal... Cliquez pour télécharger DB-MAIN Xilisoft DPG Convertisseur (5.1.37.0120)XILISOFT DPG CONVERTISSEUR (5.1.37.0120)Xilisoft DPG Convertisseur offre aux fans de Nintendo DS une bonne solution leur permettant de dé... Cliquez pour télécharger Xilisoft DPG Convertisseur GraphicsGale (2.01.01)GRAPHICSGALE (2.01.01)GraphicsGale est un logiciel de PixelArt avec de nombreuse fonctionnalités permettant de réalisé ... Cliquez pour télécharger GraphicsGale Architecte 3D (Platinum 2010)ARCHITECTE 3D (PLATINUM 2010)Architecte 3D Platinium vous permet de concevoir facilement les plans votre future maison, de l'é... Cliquez pour télécharger Architecte 3D TeamViewer 5 (TeamViewer 5)TEAMVIEWER 5 (TEAMVIEWER 5)Dépanner un ami,expliquer une manipulation devient un jeu d'enfant.
Prise en main d'un autre ord... Cliquez pour télécharger TeamViewer 5
|