Linux: Cum se creaza imagini GIF animate pentru o galerie video

Imaginile animate sunt o modalitate foarte buna de a oferi vizitatorilor dvs. o previzualizare rapida a clipurilor dintr-o galerie video si de a mari timpul pe care acestia il petrec pe site-ul dvs. Desi exista unele programe care convertesc fisiere video in fisiere GIF animate, efectuarea manuala a acestei operatiuni poate consuma destul de mult timp.

Cum se genereaza imagini GIF animate din clipuri video, folosind PHP

Pentru acest tutorial, a fost folosit un server virtual privat (VPS), cu CentOS Linux 6.4 si cPanel.

1) Instalati ffmpeg si ffmpeg-PHP

Pentru a instala ffmpeg, trebuie sa activati repozitoriul EPEL:

# rpm -Uvh
http://download.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm

Apoi, creati un fisier numit subhosting.repo in /etc/yum.repos.d care sa contina urmatoarele:

[SubHosting]

name=SubHosting Packages CentOS 6 – $basearch

baseurl=http://dl.subhosting.net/yumrepo/centos/6/$basearch/

enabled=1

gpgcheck=0

Acum puteti folosi yum pentru a instala ffmpeg, ffmpeg-devel si mplayer. Pe un sistem de operare pe 32 de biti, utilizati aceasta comanda:

# yum install ffmpeg mplayer

Pe un sistem de operare pe 64 de biti, utilizati aceasta comanda pentru a evita instalarea pachetele pe 32 de biti:

# yum install ffmpeg mplayer –exclude “*.i386”

Instalati ffmpeg-devel:

# yum install ffmpeg-devel

Descarcati si instalati extensia ffmpeg pentru PHP:

# wget http://downloads.sourceforge.net/ffmpeg-php/ffmpeg-php-0.6.0.tbz2

# tar xvjf ffmpeg-php-0.6.0.tbz2

# cd ffmpeg-php-0.6.0

# sed -i ‘s/PIX_FMT_RGBA32/PIX_FMT_RGB32/g’ ffmpeg_frame.c

# phpize

#./configure

# make

# make install

Editati fisierul php.ini al serverului (de obicei situat in /usr/local/lib/) si cautati parametrul “extension_dir”. Adaugati aceasta linie imediat sub “extension_dir”:

extension = “ffmpeg.so”

Daca utilizati un server cPanel, logati-va in WHM si mergeti la Server Configuration > PHP Configuration Editor > Advanced si adaugati “ffmpeg.so” in lista de extensii.

Cand ati terminat, reporniti serviciul HTTP (comanda poate fi usor diferita pe alte servere):

# service httpd restart

Apoi, tastati aceasta comanda pentru a va asigura ca extensia ffmpeg a fost incarcata:

# php -i | grep ffmpeg

Daca totul este ok, ar trebui sa va apara urmatorul rezultat:

ffmpeg
ffmpeg-php version => 0.6.0-svn
ffmpeg-php built on => May 2 2013 15:46:54
ffmpeg-php gd support => enabled
ffmpeg libavcodec version => Lavc52.123.0
ffmpeg libavformat version => Lavf52.111.0
ffmpeg swscaler version => SwS0.14.1
ffmpeg.allow_persistent => 0 => 0
ffmpeg.show_warnings => 0 => 0

Acum puteti crea imagini GIF animate folosind PHP. Pentru asta, va trebui sa descarcati clasa GIFEncoder de aici:

http://www.phpclasses.org/package/3163-PHP-Generate-GIF-animations-from-a-set-of-GIF-images.html

Extrageti fisierele din arhiva si copiati GIFEncoder.class.php in directorul in care va fi scriptul PHP.

De asemenea, este recomandat sa se creeze un subdirector in care vor fi salvate frame-urile extrase, inainte de a fi creat fisierul GIF animat.

<?php

include “GIFEncoder.class.php”;

// PARAMETERS
$movie_file = “movie.mp4”);

$max_frames = 50; // Maximum number of frames used to create the animation
$frames_dir = “frames/”;
$delay = 50; // The duration of each frame

$dest_width = 125;
$dest_height = 70;

// LOADING THE MOVIE FILE

$movie = new ffmpeg_movie($movie_file);

$frame_count = $movie->getFrameCount();
$next_frame = floor($frame_count/$max_frames);

$source_width = $movie->getFrameWidth();
$source_height = $movie->getFrameHeight();

// CREATING THUMBNAIL

$frame = $movie->getFrame(10); // Using frame no. 10 to create the thumbnail
$gd_img = $frame->toGDImage(); // Convert the frame to a GD image

// Create a new canvas to received the resized image
$thumbnail = imagecreatetruecolor($dest_width, $dest_height);

imagecopyresampled($thumbnail, $gd_img, 0, 0, 0, 0, $dest_width, $dest_height,
$source_width, $source_height);

// WRITE THE IMAGE TO FILE
imagegif($thumbnail, “movie-thumbnail.gif”);

imagedestroy($thumbnail);
imagedestroy($img);

// EXTRACTING THE FRAMES FROM THE MOVIE FILE
$frames = array();
$frame_no = 1;

for($i=0;$i<$max_frames;$i++):

$frames[$i] = $movie->getFrame($frame_no);
$gd_img = $frames[$i]->toGDImage();

$frame_img = imagecreatetruecolor($dest_width, $dest_height);

imagecopyresampled($frame_img, $gd_img, 0, 0, 0, 0, $dest_width, $dest_height,
$source_width, $source_height);

imagegif($frame_img, $frames_dir . “frame-” . ($i + 1) . “.gif”);

imagedestroy($gd_img);
imagedestroy($frame_img);

$frame_no += $next_frame;

endfor;

unset($frames);

// CREATING ANIMATED GIF FROM FRAMES
// THIS IS TAKEN FROM THE GIFENCODER EXAMPLE.PHP
if($dh = opendir ($frames_dir)):

while(false !== ($dat = readdir($dh))):

if($dat != “.” && $dat != “..” ):

$frames[] = $frames_dir . $dat;
$framed[] = 50;

endif;

endwhile;

closedir($dh);

endif;

// SEE EXAMPLE.PHP FOR GIFENCODER CLASS PARAMETERS
$gif = new GIFEncoder($frames,$framed,1000,2,0, 0, 0,”url”);

$fh = fopen(“animation.gif”, “wb”);
fwrite($fh, $gif->GetAnimation());

fclose($fh);

?>

Cand rulati acest script, vor rezulta doua fisiere: movie-thumbnail.gif (imaginea thumbnail statica) si animation.gif (imaginea GIF animata).

 

http://www.thewebhostinghero.com/tutorials/how-to-generate-animated-thumbnails-for-your-video-gallery-with-php.html?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+thewebhostinghero+%28TheWebHostingHero.com%29

Leave a Reply

Your email address will not be published. Required fields are marked *