Thursday, December 30, 2010

FFMPEG

FFmpeg is a free software / open source project that produces libraries and programs for handling multimedia data. The most notable parts of FFmpeg are libavcodec, an audio/video codec library used by several other projects, libavformat, an audio/video container mux and demux library, and the ffmpeg command line program for transcoding multimedia files. FFmpeg is published under the GNU Lesser General Public License 2.1+ or GNU General Public License 2+ (depending on which options are enabled).[2]




History

The project was started by Fabrice Bellard[2] (using the pseudonym “Gerard Lantau”), and is maintained by Michael Niedermayer since 2004. Many FFmpeg developers are also part of the MPlayer project, and FFmpeg is hosted at the MPlayer project server. The name of the project comes from the MPEG video standards group, together with "FF" for "fast forward".[3] The logo uses a zigzag pattern that shows how MPEG video codecs handle entropy encoding.
FFmpeg is developed under GNU/Linux, but it can be compiled under most operating systems, including Mac OS X, Microsoft Windows, AmigaOS and its heir MorphOS. Most computing platforms and microprocessor instruction set architectures are also supported, like x86 (IA-32 and x86-64), PPC (PowerPC), ARM, DEC Alpha, SPARC, and MIPS architecture.[4]
FFmpeg version 0.5 appeared after a long time without formal releases. FFmpeg developers still always recommend using the latest neutral build from their source code Subversion version control system as development attempts to maintain a stable trunk.[5]
There are two video codecs and one video container invented in the FFmpeg project during its development. The two video codecs are the lossless "FFV1", and the lossless and lossy "Snow codec", which is still in a sporadic development, but its bitstream format hasn't been finalized yet, making it experimental for now (January 2010), and the multimedia container is "NUT" which is not being actively developed anymore.
On June 17, 2010, with version 0.6 FFmpeg also supports WebM and VP8.[6]
On July 23, 2010 Jason Garrett-Glaser, Ronald Bultje, and David Conrad of the FFmpeg Team announced the ffvp8 decoder. Through testing they determined that ffvp8 was faster than Google's own libvpx decoder.[7][8]
[edit]Components



Diagram showing different applications (VDR, MPlayer, Xine and VideoLAN) using FFmpeg
The project is made of several components:
ffmpeg is a command line tool to convert one video file format to another. It can also grab and encode in real time from a TV card.
ffserver is an HTTP and RTSP multimedia streaming server for live broadcasts. It can also time shift live broadcast.
ffplay is a simple media player based on SDL and on the FFmpeg libraries.
ffprobe is a command line tool to show media information.
libavcodec is a library containing all the FFmpeg audio/video encoders and decoders. Most codecs were developed from scratch to ensure best performance and high code reusability.
libavformat is a library containing demuxers and muxers for audio/video container formats.
libavutil is a helper library containing routines common to different parts of FFmpeg. This library includes adler32, crc, md5, sha1, lzo decompressor, Base64 encoder/decoder, des encrypter/decrypter, rc4 encrypter/decrypter and aes encrypter/decrypter.
libpostproc is a library containing video postprocessing routines.
libswscale is a library containing video image scaling and colorspace/pixelformat conversion routines.
libavfilter is the substitute for vhook which allows the video/audio to be modified or examined between the decoder and the encoder.
[edit]Codecs, formats and protocols supported

[edit]Codecs
For more details on this topic, see libavcodec.
Codecs with the origin in the project:
Snow
FFV1
The FFmpeg developers have implemented among others:
ITU-T video standards: H.261,[9] H.262/MPEG-2 Part 2, H.263[9] and H.264/MPEG-4 AVC[9]
ITU-T vocoder standards: G.711 ยต-law, G.711 A-law, G.721 (aka G.726 32k), G.722, G.722.2 (aka AMR-WB), G.723 (aka G.726 24k and 40k) and G.726
ISO/IEC MPEG video standards: MPEG-1 Part 2, H.262/MPEG-2 Part 2, MPEG-4 Part 2 and H.264/MPEG-4 AVC
ISO/IEC MPEG audio standards: MP1, MP2, MP3, AAC, HE-AAC and MPEG-4 ALS
ISO/IEC/ITU-T JPEG image standards: JPEG and JPEG-LS
SMPTE video standards: VC-1 (aka WMV3), VC-3 (aka AVID DNxHD) and DPX image
DVD Forum standards related / Dolby audio codecs: MLP (aka TrueHD) and AC-3
3GPP vocoder standards: AMR-NB, AMR-WB (aka G.722.2)
GSM related voice codecs: Full Rate
Windows Media Player related video codecs: Microsoft RLE, Microsoft Video 1, Cinepak, Indeo 2, 3 and 5[9], Motion JPEG, Microsoft MPEG-4 v1, v2 and v3, WMV1, WMV2 and WMV3 (aka VC-1)
Windows Media Player related audio codecs: WMA1, WMA2 and WMA Pro
Windows Media Player related voice codecs: WMA Voice and MS-GSM
RealPlayer related video codecs: RealVideo 1, 2, 3 and 4
RealPlayer related audio codecs: RealAudio 3, 6, 7, 8, 9 and 10
RealPlayer related voice codecs: RealAudio 1, 2 (variant of G.728), 4 and 5
QuickTime related video codecs: Cinepak, Motion JPEG and Sorenson 3 Codec
QuickTime related audio codecs: QDesign Music Codec 2 and ALAC
Adobe Flash Player related video codecs: Sorenson 3 Codec, VP6 and Flash Video (FLV)
Adobe Flash Player related audio codecs: Adobe SWF ADPCM and Nellymoser Asao
Xiph.Org: Theora, Speex (via libspeex), Vorbis and FLAC
Sony: ATRAC1 and ATRAC3[9]
NTT: TwinVQ
On2: Duck TrueMotion 1, Duck TrueMotion 2, VP3, VP5[9], VP6[9] and VP8
RAD Game Tools: Smacker video and Bink video
Truespeech
RenderWare: TXD[10]
The default MPEG-4 codec used by FFmpeg for encoding has the FourCC of FMP4.
[edit]Formats
ASF
AVI
BFI[11]
FLV
GXF, General eXchange Format, SMPTE 360M
IFF[12]
RL2[13]
ISO base media file format (including QuickTime, 3GP and MP4)
Matroska (including WebM)
Maxis XA[14]
MPEG program stream
MPEG transport stream (including AVCHD)
MXF, Material eXchange Format, SMPTE 377M
MSN Webcam stream[15]
Ogg
OMA[16]
TXD[10]
[edit]Protocols
IETF standards: TCP, UDP, Gopher, HTTP, RTP, RTSP and SDP
Apple related protocols: HTTP Live Streaming
RealMedia related protocols: RealMedia RTSP/RDT
Adobe related protocols: RTMP, RTMPT (via librtmp), RTMPE (via librtmp), RTMPTE (via librtmp) and RTMPS (via librtmp)
Microsoft related protocols: MMS over TCP and MMS over HTTP
[edit]Projects using FFmpeg

FFmpeg is used by many open source (and proprietary) projects, including ffmpeg2theora, VLC, MPlayer, HandBrake, Blender, Google Chrome, and others.[17]
FFmpeg handles the HTML5 Video and Audio media content in the Origyn Web Browser for MorphOS Operating System.[18][19]
[edit]Multimedia frameworks using FFmpeg

DirectShow/VFW
ffdshow (external project)
QuickTime
Perian (external project)
GStreamer
GStreamer FFmpeg plug-in (external project)
OpenMAX
[edit]Legal status of codecs

FFmpeg contains more than 100 codecs [20] most of which do not just store uncompressed data. At least all codecs that compress information could be claimed by patent holders.[21] Such claims may be enforceable in countries like the United States which have implemented software patents, but are considered unenforceable or void in countries that have not implemented software patents. Furthermore, many of these codecs are only released under terms that forbid reverse engineering, even for purposes of interoperability. These terms of use are forbidden in certain countries. For example, some European Union nations have not implemented software patents and have laws expressly allowing reverse engineering for purposes of interoperability.[22]
Most GNU/Linux distributions include a stripped version of FFmpeg only including certain codecs to avoid legal complications, instead contributors maintain unofficial repositories containing such software. [23][24]

ffmpeg

how to upload video and create thumbnail and store in database with ffmpeg

This mod requires ffmpeg pre-installed and working on your server. Please DO NOT ask for support on installing or configuring ffmpeg. If you don't have ffmpeg or can't install ffmpeg on your server then this mod is not meant for you.

What will this mod do?
This mod will create thumbnail from the uploaded video file automatically on the fly. Thumbnails will be placed in the directory where the videos are uploaded/batch added. Though ffmpeg supports many video formats but i am unsure about wmv. File formats which i tested were avi, mpg, asf, flv.

Edit include/picmgmt.inc.php

Add

Code:
/**
* Function to create video thumbnail using ffmpeg
*/
function create_movie_thumb($src_file)
{
global $CONFIG, $ERROR;

$CONFIG['ffmpeg_path'] = '/usr/local/bin/'; // Change the path according to your server.

$src_file = $src_file;
$dest_file = $CONFIG['fullpath']."edit/".md5(uniqid(time()))."%d.jpg";

if (preg_match("#[A-Z]:|\\\\#Ai", __FILE__)) {
// get the basedir, remove '/include'
$cur_dir = substr(dirname(__FILE__), 0, -8);
$src_file = '"' . $cur_dir . '\\' . strtr($src_file, '/', '\\') . '"';
$ff_dest_file = '"' . $cur_dir . '\\' . strtr($dest_file, '/', '\\') . '"';
} else {
$src_file = escapeshellarg($src_file);
$ff_dest_file = escapeshellarg($dest_file);
}

$output = array();

if (eregi("win",$_ENV['OS'])) {
// Command to create video thumb
$cmd = "\"".str_replace("\\","/", $CONFIG['ffmpeg_path'])."ffmpeg\" -i ".str_replace("\\","/" ,$src_file )." -an -ss 00:00:05 -r 1 -vframes 1 -y ".str_replace("\\","/" ,$ff_dest_file);
exec ("\"$cmd\"", $output, $retval);
} else {
// Command to create video thumb
$cmd = "{$CONFIG['ffmpeg_path']}ffmpeg -i $src_file -an -ss 00:00:05 -r 1 -vframes 1 -y $ff_dest_file";
exec ($cmd, $output, $retval);
}


if ($retval) {
$ERROR = "Error executing FFmpeg - Return value: $retval";
if ($CONFIG['debug_mode']) {
// Re-execute the command with the backtick operator in order to get all outputs
// will not work if safe mode is enabled
$output = `$cmd 2>&1`;
$ERROR .= "

Cmd line :
" . nl2br(htmlspecialchars($cmd)) . "
";
$ERROR .= "

The ffmpeg program said:
";
$ERROR .= nl2br(htmlspecialchars($output));
$ERROR .= "
";
}
@unlink($dest_file);
return false;
}

$return = str_replace("%d", "1", $dest_file);
@chmod($return, octdec($CONFIG['default_file_mode'])); //silence the output in case chmod is disabled
return $return;
}

just before

Code:
// Add a picture to an album
function add_picture($aid, $filepath, $filename, $position = 0, $title = '', $caption = '', $keywords = '', $user1 = '', $user2 = '', $user3 = '', $user4 = '', $category = 0, $raw_ip = '', $hdr_ip = '', $iwidth = 0, $iheight = 0)

In above code change $CONFIG['ffmpeg_path'] value to where ffmpeg is installed on your server.

Add

Code:
} elseif (is_movie($image)) {
preg_match("/(.+)\.(.*?)\Z/", $filename, $matches);
$thumb = $CONFIG['fullpath'] . $filepath . $CONFIG['thumb_pfx'] . $matches[1].".jpg";
$videoThumb = create_movie_thumb($image);
if ($videoThumb) {
if (!resize_image($videoThumb, $thumb, $CONFIG['thumb_width'], $CONFIG['thumb_method'], $CONFIG['thumb_use'])) {
return false;
}
@unlink($videoThumb);
$imagesize = getimagesize($thumb);
} else {
return false;
}

just before

Code:
} else {
$imagesize[0] = $iwidth;
$imagesize[1] = $iheight;
}

That's it, only one file to be modified.


Few Notes
The mod works best on linux servers
If you have Dreamhost as your webhost then this might help you
The dreamhost installation way might work on other shared servers too but not sure. Give it a try.
If you have a dedicated server then ffmpeg will not be that difficult for your webhost to install