Cada día la experiencia que uno obtiene de la WEB es más personalizada, al punto que hay sitios que te permiten crear y manipular tus propios videos de manera online. En este pequeño post hablaré de cómo podemos implementar el uso de la librería FFMPEG con PHP para detectar la orientación de un video.
FFMPEG
FFMPEG es un conjunto de software libre orientado a la codificación, decodificación y streaming de audio y video; FFMPEG está disponible para Linux, Windows y OSX, si deseas obtener mayor información, puedes visitar su sitio oficial ffmpeg.org
Una vez instalado, podemos ejecutar el comando ffmpeg desde la terminal y nos debe aparecer la siguiente salida
ffmpeg version N-49844-g1ac0fa5 Copyright (c) 2000-2013 the FFmpeg developers
built on Feb 12 2013 17:42:46 with gcc 4.7.2 (GCC)
configuration:
--enable-gpl --enable-version3 --disable-w32threads --enable-avisynth
--enable-bzlib --enable-fontconfig --enable-frei0r --enable-gnutls
--enable-libass --enable-libbluray --enable-libcaca --enable-libfreetype
--enable-libgsm --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb
--enable-libopenjpeg --enable-libopus --enable-librtmp --enable-libschroedinger
--enable-libsoxr --enable-libspeex --enable-libtheora --enable-libtwolame
--enable-libvo-aacenc --enable-libvo-amrwbenc --enable-libvorbis --enable-libvpx
--enable-libx264 --enable-libxavs --enable-libxvid --enable-zlib
libavutil 52. 17.101 / 52. 17.101
libavcodec 54. 91.103 / 54. 91.103
libavformat 54. 63.100 / 54. 63.100
libavdevice 54. 3.103 / 54. 3.103
libavfilter 3. 37.101 / 3. 37.101
libswscale 2. 2.100 / 2. 2.100
libswresample 0. 17.102 / 0. 17.102
libpostproc 52. 2.100 / 52. 2.100
Hyper fast Audio and Video encoder
usage: ffmpeg [options] [[infile options] -i infile]... {[outfile options] outfile}...Metadatos del Video
Mediante FFMPEG podemos obtener información relevante de un video, como lo son: codificación de video, codificación del audio, bitrate, dimensión, cuados por segundos (fps) entre otras. Para esto usamos la opción –i la cual nos permite especificar el archivo de entrada. En este ejemplo utilizaré el siguiente video: FFMPEG_INPUT_VIDEO.MOV
Salida al ejecutar el comando ffmpeg –i FFMPEG_INPUT_VIDEO.MOV
ffmpeg version N-49844-g1ac0fa5 Copyright (c) 2000-2013 the FFmpeg developers
built on Feb 12 2013 17:42:46 with gcc 4.7.2 (GCC)
configuration:
--enable-gpl --enable-version3 --disable-w32threads --enable-avisynth
--enable-bzlib --enable-fontconfig --enable-frei0r --enable-gnutls
--enable-libass --enable-libbluray --enable-libcaca --enable-libfreetype
--enable-libgsm --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb
--enable-libopenjpeg --enable-libopus --enable-librtmp --enable-libschroedinger
--enable-libsoxr --enable-libspeex --enable-libtheora --enable-libtwolame
--enable-libvo-aacenc --enable-libvo-amrwbenc --enable-libvorbis --enable-libvpx
--enable-libx264 --enable-libxavs --enable-libxvid --enable-zlib
libavutil 52. 17.101 / 52. 17.101
libavcodec 54. 91.103 / 54. 91.103
libavformat 54. 63.100 / 54. 63.100
libavdevice 54. 3.103 / 54. 3.103
libavfilter 3. 37.101 / 3. 37.101
libswscale 2. 2.100 / 2. 2.100
libswresample 0. 17.102 / 0. 17.102
libpostproc 52. 2.100 / 52. 2.100
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'FFMPEG_INPUT_VIDEO.MOV':
Metadata:
major_brand : qt
minor_version : 0
compatible_brands: qt
creation_time : 2013-02-10 08:05:11
make : Apple
make-eng : Apple
encoder : 4.0.1
encoder-eng : 4.0.1
date : 2011-02-28T15:26:23-0800
date-eng : 2011-02-28T15:26:23-0800
location : +34.0977-118.3329+077.026/
location-eng : +34.0977-118.3329+077.026/
model : iPhone
model-eng : iPhone
Duration: 00:00:21.24, start: 0.000000, bitrate: 794 kb/s
Stream #0:0(und): Audio: aac (mp4a / 0x6134706D), 44100 Hz, mono, fltp, 63 k
b/s
Metadata:
creation_time : 2013-02-10 08:05:11
handler_name : Core Media Data Handler
Stream #0:1(und): Video: h264 (Main) (avc1 / 0x31637661), yuv420p, 480x272,
725 kb/s, 30 fps, 30 tbr, 600 tbn, 1200 tbc
Metadata:
rotate : 90
creation_time : 2013-02-10 08:05:11
handler_name : Core Media Data Handler
At least one output file must be specifiedComo verán, FFMPEG nuestra información relevante acerca de este video, de entre los cuales podemos destacar rotate: 90.
PHP y FFMPEG
Utilizando un script PHP podemos combinar el uso de FFMPEG para determinar si un video necesita rotación o no, para esto utilizare expresiones regulares
<?php
// PTYDev FFMPEG Rotation Demo
$rotate_pattern = "/rotate\\s*:\s*([0-9]+)/i";
$has_rotation = false;
$cmd = 'ffmpeg -v verbose -i "FFMPEG_INPUT_VIDEO.MOV" 2>&1';
$video_info = shell_exec($cmd);
//let's check against the rotation pattern
preg_match($rotate_pattern,$video_info,$matches);
if(is_array($matches))
{
if(isset($matches[1]))
{
$has_rotation = true;
}
}
//let's see if the video has rotation or not
if($has_rotation)
{
echo "Video has rotation";
}
else
echo "Video doesn't has rotation";
?>Rotación del Video
Si detectamos que nuestro video tiene rotación, lo podemos cambiar utilizando el filtro de video transpose que ofrece FFMPEG y escribimos el resultado final en otro archivo, en mi ejemplo es el FFMPEG_OUTPUT_VIDEO.MOV, a continuación el script completo
<?php
// PTYDev FFMPEG Rotation Demo
$rotate_pattern = "/rotate\\s*:\s*([0-9]+)/i";
$has_rotation = false;
$cmd = 'ffmpeg -v verbose -i "FFMPEG_INPUT_VIDEO.MOV" 2>&1';
$video_info = shell_exec($cmd);
//let's check against the rotation pattern
preg_match($rotate_pattern,$video_info,$matches);
if(is_array($matches))
{
if(isset($matches[1]))
{
$has_rotation = true;
}
}
//let's see if the video has rotation or not
if($has_rotation)
{
//rotate the video using transpose and write an output file
$cmd = 'ffmpeg -v verbose -i "FFMPEG_INPUT_VIDEO.MOV" -vf "transpose=1" -y "FFMPEG_OUTPUT_VIDEO.MOV" 2>&1';
shell_exec($cmd);
echo "Video has been rotated";
}
else
echo "Video doesn't has rotation, nothing to do";
?>Espero este post les puede ayudar en algún proyecto que requiera del uso de FFMPEG, estaré escribiendo más tips acerca de esta librería, no olivdes en dejar tu comentario, hasta el próximo post!


