/Vendor/phpvideotoolkit/phpvideotoolkit.php4.php
PHP | 3295 lines | 1735 code | 168 blank | 1392 comment | 259 complexity | c07fc240df96d0353b02f1aafe1cf221 MD5 | raw file
Possible License(s): GPL-2.0, AGPL-1.0, LGPL-2.1, GPL-3.0
Large files files are truncated, but you can click here to view the full file
- <?php
- /* SVN FILE: $Id$ */
- /**
- * @author Oliver Lillie (aka buggedcom) <publicmail@buggedcom.co.uk>
- *
- * @license BSD
- * @copyright Copyright (c) 2008 Oliver Lillie <http://www.buggedcom.co.uk>
- * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation
- * files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy,
- * modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software
- * is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be
- * included in all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
- * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
- * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
- * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- *
- * @package PHPVideoToolkit (was called ffmpeg)
- * @version 0.1.5
- * @changelog SEE CHANGELOG
- * @abstract This class can be used in conjunction with several server binary libraries to manipulate video and audio
- * through PHP. It is not intended to solve any particular problems, however you may find it useful. This php class
- * is in no way associated with the actual FFmpeg releases. Any mistakes contained in this php class are mine and mine
- * alone.
- *
- * Please Note: There are several prerequisites that are required before this class can be used as an aid to manipulate
- * video and audio. You must at the very least have FFMPEG compiled on your server. If you wish to use this class for FLV
- * manipulation you must compile FFMPEG with LAME and Ruby's FLVTOOL2. I cannot answer questions regarding the install of
- * the server binaries needed by this class. I had too learn the hard way and it isn't easy, however it is a good learning
- * experience. For those of you who do need help read the install.txt file supplied along side this class. It wasn't written
- * by me however I found it useful when installing ffmpeg for the first time. The original source for the install.txt file
- * is located http://www.luar.com.hk/blog/?p=669 and the author is Lunar.
- *
- * @see install.txt
- *
- * @uses ffmpeg http://ffmpeg.sourceforge.net/
- * @uses lame http://lame.sourceforge.net/
- * @uses flvtool2 http://www.inlet-media.de/flvtool2 (and ruby http://www.ruby-lang.org/en/)
- *
- * @config examples/example-config.php Please edit this files in order for the examples to work.
- * @example examples/example01.php Converts video to Flash Video (ie FLV).
- * @example examples/example02.php Screen grabs video frames.
- * @example examples/example03.php Compile a movie from multiple jpegs
- * @example examples/example04.php Watermark a video.
- * @example examples/example05.php Access media metadata without using the ffmpeg-php library.
- * @example examples/example06.php Extract audio from video.
- * @example examples/example07.php Join multiple videos together.
- * @example examples/example08.php Easy video conversion to common formats using the adapters.
- * @example examples/example09.php Shows you how to access the information about your ffmpeg installation.
- * @example examples/example10.php Shows you how to extract a specific frame from a movie.
- * @example examples/example11.php Shows you how to use the ffmpeg-php adapters to provide a pure php emulation of ffmpeg-php.
- * @example examples/example12.php Shows you how to manipulate/format timecode strings.
- * @example examples/example13.php This demonstrates how to simply create a FLV stream script.
- */
- /**
- * Set the ffmpeg binary path
- */
- if(!defined('PHPVIDEOTOOLKIT_FFMPEG_BINARY'))
- {
- define('PHPVIDEOTOOLKIT_FFMPEG_BINARY', '/usr/local/bin/ffmpeg');
- }
- /**
- * Set the flvtool2 binary path
- */
- if(!defined('PHPVIDEOTOOLKIT_FLVTOOLS_BINARY'))
- {
- define('PHPVIDEOTOOLKIT_FLVTOOLS_BINARY', '/usr/bin/flvtool2');
- }
- /**
- * Set the watermark vhook path
- */
- if(!defined('PHPVIDEOTOOLKIT_FFMPEG_WATERMARK_VHOOK'))
- {
- define('PHPVIDEOTOOLKIT_FFMPEG_WATERMARK_VHOOK', '/usr/local/lib/vhook/watermark.so');
- }
- /**
- * Set the memcoder path
- */
- if(!defined('PHPVIDEOTOOLKIT_MENCODER_BINARY'))
- {
- define('PHPVIDEOTOOLKIT_MENCODER_BINARY', '/usr/local/bin/mencoder');
- }
-
- /**
- * Codec support constants
- */
- define('PHPVIDEOTOOLKIT_ENCODE', 'encode');
- define('PHPVIDEOTOOLKIT_DECODE', 'decode');
-
- /**
- * Process Results from PHPVideoToolkit::execute
- */
- // any return value with this means everything is ok
- define('PHPVIDEOTOOLKIT_RESULT_OK', true);
- // any return value with this means the file has been processed/converted ok however it was
- // not able to be written to the output address. If this occurs you will need to move the
- // processed file manually from the temp location
- define('PHPVIDEOTOOLKIT_RESULT_OK_BUT_UNWRITABLE', -1);
-
- /**
- * Overwrite constants used in setOutput
- */
- define('PHPVIDEOTOOLKIT_OVERWRITE_FAIL', 'fail');
- define('PHPVIDEOTOOLKIT_OVERWRITE_PRESERVE', 'preserve');
- define('PHPVIDEOTOOLKIT_OVERWRITE_EXISTING', 'existing');
- define('PHPVIDEOTOOLKIT_OVERWRITE_UNIQUE', 'unique');
-
- /**
- * Formats supported
- * 3g2 3gp2 format
- * 3gp 3gp format
- * aac ADTS AAC
- * aiff Audio IFF
- * amr 3gpp amr file format
- * asf asf format
- * avi avi format
- * flv flv format
- * gif GIF Animation
- * mov mov format
- * mov,mp4,m4a,3gp,3g2,mj2 QuickTime/MPEG4/Motion JPEG 2000 format
- * mp2 MPEG audio layer 2
- * mp3 MPEG audio layer 3
- * mp4 mp4 format
- * mpeg MPEG1 System format
- * mpeg1video MPEG video
- * mpeg2video MPEG2 video
- * mpegvideo MPEG video
- * psp psp mp4 format
- * rm rm format
- * swf Flash format
- * vob MPEG2 PS format (VOB)
- * wav wav format
- * jpeg mjpeg format
- * yuv4mpegpipe yuv4mpegpipe format
- */
- define('PHPVIDEOTOOLKIT_FORMAT_3GP2', '3g2');
- define('PHPVIDEOTOOLKIT_FORMAT_3GP', '3gp');
- define('PHPVIDEOTOOLKIT_FORMAT_AAC', 'aac');
- define('PHPVIDEOTOOLKIT_FORMAT_AIFF', 'aiff');
- define('PHPVIDEOTOOLKIT_FORMAT_AMR', 'amr');
- define('PHPVIDEOTOOLKIT_FORMAT_ASF', 'asf');
- define('PHPVIDEOTOOLKIT_FORMAT_AVI', 'avi');
- define('PHPVIDEOTOOLKIT_FORMAT_FLV', 'flv');
- define('PHPVIDEOTOOLKIT_FORMAT_GIF', 'gif');
- define('PHPVIDEOTOOLKIT_FORMAT_MJ2', 'mj2');
- define('PHPVIDEOTOOLKIT_FORMAT_MP2', 'mp2');
- define('PHPVIDEOTOOLKIT_FORMAT_MP3', 'mp3');
- define('PHPVIDEOTOOLKIT_FORMAT_MP4', 'mp4');
- define('PHPVIDEOTOOLKIT_FORMAT_MPEG4', 'mpeg4');
- define('PHPVIDEOTOOLKIT_FORMAT_M4A', 'm4a');
- define('PHPVIDEOTOOLKIT_FORMAT_MPEG', 'mpeg');
- define('PHPVIDEOTOOLKIT_FORMAT_MPEG1', 'mpeg1video');
- define('PHPVIDEOTOOLKIT_FORMAT_MPEG2', 'mpeg2video');
- define('PHPVIDEOTOOLKIT_FORMAT_MPEGVIDEO', 'mpegvideo');
- define('PHPVIDEOTOOLKIT_FORMAT_PSP', 'psp');
- define('PHPVIDEOTOOLKIT_FORMAT_RM', 'rm');
- define('PHPVIDEOTOOLKIT_FORMAT_SWF', 'swf');
- define('PHPVIDEOTOOLKIT_FORMAT_VOB', 'vob');
- define('PHPVIDEOTOOLKIT_FORMAT_WAV', 'wav');
- define('PHPVIDEOTOOLKIT_FORMAT_JPG', 'mjpeg');
- define('PHPVIDEOTOOLKIT_FORMAT_Y4MP', 'yuv4mpegpipe');
-
- /**
- * Size Presets
- */
- define('PHPVIDEOTOOLKIT_SIZE_SAS', 'SameAsSource');
- define('PHPVIDEOTOOLKIT_SIZE_SQCIF', '128x96');
- define('PHPVIDEOTOOLKIT_SIZE_QCIF', '176x144');
- define('PHPVIDEOTOOLKIT_SIZE_CIF', '352x288');
- define('PHPVIDEOTOOLKIT_SIZE_4CIF', '704x576');
- define('PHPVIDEOTOOLKIT_SIZE_QQVGA', '160x120');
- define('PHPVIDEOTOOLKIT_SIZE_QVGA', '320x240');
- define('PHPVIDEOTOOLKIT_SIZE_VGA', '640x480');
- define('PHPVIDEOTOOLKIT_SIZE_SVGA', '800x600');
- define('PHPVIDEOTOOLKIT_SIZE_XGA', '1024x768');
- define('PHPVIDEOTOOLKIT_SIZE_UXGA', '1600x1200');
- define('PHPVIDEOTOOLKIT_SIZE_QXGA', '2048x1536');
- define('PHPVIDEOTOOLKIT_SIZE_SXGA', '1280x1024');
- define('PHPVIDEOTOOLKIT_SIZE_QSXGA', '2560x2048');
- define('PHPVIDEOTOOLKIT_SIZE_HSXGA', '5120x4096');
- define('PHPVIDEOTOOLKIT_SIZE_WVGA', '852x480');
- define('PHPVIDEOTOOLKIT_SIZE_WXGA', '1366x768');
- define('PHPVIDEOTOOLKIT_SIZE_WSXGA', '1600x1024');
- define('PHPVIDEOTOOLKIT_SIZE_WUXGA', '1920x1200');
- define('PHPVIDEOTOOLKIT_SIZE_WOXGA', '2560x1600');
- define('PHPVIDEOTOOLKIT_SIZE_WQSXGA', '3200x2048');
- define('PHPVIDEOTOOLKIT_SIZE_WQUXGA', '3840x2400');
- define('PHPVIDEOTOOLKIT_SIZE_WHSXGA', '6400x4096');
- define('PHPVIDEOTOOLKIT_SIZE_WHUXGA', '7680x4800');
- define('PHPVIDEOTOOLKIT_SIZE_CGA', '320x200');
- define('PHPVIDEOTOOLKIT_SIZE_EGA', '640x350');
- define('PHPVIDEOTOOLKIT_SIZE_HD480', '852x480');
- define('PHPVIDEOTOOLKIT_SIZE_HD720', '1280x720');
- define('PHPVIDEOTOOLKIT_SIZE_HD1080', '1920x1080');
-
- /**
- * Ratio Presets
- */
- define('PHPVIDEOTOOLKIT_RATIO_STANDARD', '4:3');
- define('PHPVIDEOTOOLKIT_RATIO_WIDE', '16:9');
- define('PHPVIDEOTOOLKIT_RATIO_CINEMATIC', '1.85');
- /**
- * @author Oliver Lillie (aka buggedcom) <publicmail@buggedcom.co.uk>
- * @license BSD
- * @package PHPVideoToolkit (was called ffmpeg)
- */
- class PHPVideoToolkit
- {
-
- var $version = '0.1.5';
-
- /**
- * Error strings
- */
- var $_messages = array(
-
- 'generic_temp_404' => 'The temporary directory does not exist.',
- 'generic_temp_writable' => 'The temporary directory is not write-able by the web server.',
-
- 'getFileInfo_no_input' => 'Input file does not exist so no information can be retrieved.',
- 'setInputFile_file_existence' => 'Input file "#file" does not exist',
- 'extractAudio_valid_format' => 'Value "#format" set from $toolkit->extractAudio, is not a valid audio format. Valid values ffmpeg PHPVIDEOTOOLKIT_FORMAT_AAC, PHPVIDEOTOOLKIT_FORMAT_AIFF, PHPVIDEOTOOLKIT_FORMAT_MP2, PHPVIDEOTOOLKIT_FORMAT_MP3, PHPVIDEOTOOLKIT_FORMAT_MP4, PHPVIDEOTOOLKIT_FORMAT_MPEG4, PHPVIDEOTOOLKIT_FORMAT_M4A or PHPVIDEOTOOLKIT_FORMAT_WAV. If you wish to specifically try to set another format you should use the advanced function $toolkit->addCommand. Set $command to "-f" and $argument to your required value.',
- 'extractFrame_video_frame_rate_404' => 'You have attempted to extract a thumbnail from a video while automagically guessing the framerate of the video, but the framerate could not be accessed. You can remove this error by manually setting the frame rate of the video.',
- 'extractFrame_video_info_404' => 'You have attempted to extract a thumbnail from a video and check to see if the thumbnail exists, however it was not possible to access the video information. Please check your temporary directory permissions for read/write access by the webserver.',
- 'extractFrame_video_frame_count' => 'You have attempted to extract a thumbnail from a video but the thumbnail you are trying to extract does not exist in the video.',
- 'extractFrames_video_begin_frame_count' => 'You have attempted to extract thumbnails from a video but the thumbnail you are trying to start the extraction from does not exist in the video.',
- 'extractFrames_video_end_frame_count' => 'You have attempted to extract thumbnails from a video but the thumbnail you are trying to end the extraction at does not exist in the video.',
- 'setFormat_valid_format' => 'Value "#format" set from $toolkit->setFormat, is not a valid format. Valid values are PHPVIDEOTOOLKIT_FORMAT_3GP2, PHPVIDEOTOOLKIT_FORMAT_3GP, PHPVIDEOTOOLKIT_FORMAT_AAC, PHPVIDEOTOOLKIT_FORMAT_AIFF, PHPVIDEOTOOLKIT_FORMAT_AMR, PHPVIDEOTOOLKIT_FORMAT_ASF, PHPVIDEOTOOLKIT_FORMAT_AVI, PHPVIDEOTOOLKIT_FORMAT_FLV, PHPVIDEOTOOLKIT_FORMAT_GIF, PHPVIDEOTOOLKIT_FORMAT_MJ2, PHPVIDEOTOOLKIT_FORMAT_MP2, PHPVIDEOTOOLKIT_FORMAT_MP3, PHPVIDEOTOOLKIT_FORMAT_MP4, PHPVIDEOTOOLKIT_FORMAT_MPEG4, PHPVIDEOTOOLKIT_FORMAT_M4A, PHPVIDEOTOOLKIT_FORMAT_MPEG, PHPVIDEOTOOLKIT_FORMAT_MPEG1, PHPVIDEOTOOLKIT_FORMAT_MPEG2, PHPVIDEOTOOLKIT_FORMAT_MPEGVIDEO, PHPVIDEOTOOLKIT_FORMAT_PSP, PHPVIDEOTOOLKIT_FORMAT_RM, PHPVIDEOTOOLKIT_FORMAT_SWF, PHPVIDEOTOOLKIT_FORMAT_VOB, PHPVIDEOTOOLKIT_FORMAT_WAV, PHPVIDEOTOOLKIT_FORMAT_JPG. If you wish to specifically try to set another format you should use the advanced function $toolkit->addCommand. Set $command to "-f" and $argument to your required value.',
- 'setAudioSampleFrequency_valid_frequency' => 'Value "#frequency" set from $toolkit->setAudioSampleFrequency, is not a valid integer. Valid values are 11025, 22050, 44100. If you wish to specifically try to set another frequency you should use the advanced function $toolkit->addCommand. Set $command to "-ar" and $argument to your required value.',
- 'setAudioFormat_valid_format' => 'Value "#format" set from $toolkit->setAudioFormat, is not a valid format. Valid values are PHPVIDEOTOOLKIT_FORMAT_AAC, PHPVIDEOTOOLKIT_FORMAT_AIFF, PHPVIDEOTOOLKIT_FORMAT_AMR, PHPVIDEOTOOLKIT_FORMAT_ASF, PHPVIDEOTOOLKIT_FORMAT_MP2, PHPVIDEOTOOLKIT_FORMAT_MP3, PHPVIDEOTOOLKIT_FORMAT_MP4, PHPVIDEOTOOLKIT_FORMAT_MPEG2, PHPVIDEOTOOLKIT_FORMAT_RM, PHPVIDEOTOOLKIT_FORMAT_WAV. If you wish to specifically try to set another format you should use the advanced function $toolkit->addCommand. Set $command to "-acodec" and $argument to your required value.',
- 'setVideoFormat_valid_format' => 'Value "#format" set from $toolkit->setAudioFormat, is not a valid format. Valid values are PHPVIDEOTOOLKIT_FORMAT_3GP2, PHPVIDEOTOOLKIT_FORMAT_3GP, PHPVIDEOTOOLKIT_FORMAT_AVI, PHPVIDEOTOOLKIT_FORMAT_FLV, PHPVIDEOTOOLKIT_FORMAT_GIF, PHPVIDEOTOOLKIT_FORMAT_MJ2, PHPVIDEOTOOLKIT_FORMAT_MP4, PHPVIDEOTOOLKIT_FORMAT_MPEG4, PHPVIDEOTOOLKIT_FORMAT_M4A, PHPVIDEOTOOLKIT_FORMAT_MPEG, PHPVIDEOTOOLKIT_FORMAT_MPEG1, PHPVIDEOTOOLKIT_FORMAT_MPEG2, PHPVIDEOTOOLKIT_FORMAT_MPEGVIDEO. If you wish to specifically try to set another format you should use the advanced function $toolkit->addCommand. Set $command to "-vcodec" and $argument to your required value.',
- 'setAudioBitRate_valid_bitrate' => 'Value "#bitrate" set from $toolkit->setAudioBitRate, is not a valid integer. Valid values are 16, 32, 64, 128. If you wish to specifically try to set another bitrate you should use the advanced function $toolkit->addCommand. Set $command to "-ab" and $argument to your required value.',
- 'prepareImagesForConversionToVideo_one_img' => 'When compiling a movie from a series of images, you must include at least one image.',
- 'prepareImagesForConversionToVideo_img_404' => '"#img" does not exist.',
- 'prepareImagesForConversionToVideo_img_copy' => '"#img" can not be copied to "#tmpfile"',
- 'prepareImagesForConversionToVideo_img_type' => 'The images can not be prepared for conversion to video. Please make sure all images are of the same type, ie gif, png, jpeg and then try again.',
- 'setVideoOutputDimensions_valid_format' => 'Value "#format" set from $toolkit->setVideoOutputDimensions, is not a valid preset dimension. Valid values are PHPVIDEOTOOLKIT_SIZE_SQCIF, PHPVIDEOTOOLKIT_SIZE_SAS, PHPVIDEOTOOLKIT_SIZE_QCIF, PHPVIDEOTOOLKIT_SIZE_CIF, PHPVIDEOTOOLKIT_SIZE_4CIF, PHPVIDEOTOOLKIT_SIZE_QQVGA, PHPVIDEOTOOLKIT_SIZE_QVGA, PHPVIDEOTOOLKIT_SIZE_VGA, PHPVIDEOTOOLKIT_SIZE_SVGA, PHPVIDEOTOOLKIT_SIZE_XGA, PHPVIDEOTOOLKIT_SIZE_UXGA, PHPVIDEOTOOLKIT_SIZE_QXGA, PHPVIDEOTOOLKIT_SIZE_SXGA, PHPVIDEOTOOLKIT_SIZE_QSXGA, PHPVIDEOTOOLKIT_SIZE_HSXGA, PHPVIDEOTOOLKIT_SIZE_WVGA, PHPVIDEOTOOLKIT_SIZE_WXGA, PHPVIDEOTOOLKIT_SIZE_WSXGA, PHPVIDEOTOOLKIT_SIZE_WUXGA, PHPVIDEOTOOLKIT_SIZE_WOXGA, PHPVIDEOTOOLKIT_SIZE_WQSXGA, PHPVIDEOTOOLKIT_SIZE_WQUXGA, PHPVIDEOTOOLKIT_SIZE_WHSXGA, PHPVIDEOTOOLKIT_SIZE_WHUXGA, PHPVIDEOTOOLKIT_SIZE_CGA, PHPVIDEOTOOLKIT_SIZE_EGA, PHPVIDEOTOOLKIT_SIZE_HD480, PHPVIDEOTOOLKIT_SIZE_HD720, PHPVIDEOTOOLKIT_SIZE_HD1080. You can also manually set the width and height.',
- 'setVideoOutputDimensions_sas_dim' => 'It was not possible to determine the input video dimensions so it was not possible to continue. If you wish to override this error please change the call to setVideoOutputDimensions and add a true argument to the arguments list... setVideoOutputDimensions(PHPVIDEOTOOLKIT_SIZE_SAS, true);',
- 'setVideoOutputDimensions_valid_integer' => 'You tried to set the video output dimensions to an odd number. FFmpeg requires that the video output dimensions are of event value and divisible by 2. ie 2, 4, 6,... etc',
- 'setVideoAspectRatio_valid_ratio' => 'Value "#ratio" set from $toolkit->setVideoOutputDimensions, is not a valid preset dimension. Valid values are PHPVIDEOTOOLKIT_RATIO_STANDARD, PHPVIDEOTOOLKIT_RATIO_WIDE, PHPVIDEOTOOLKIT_RATIO_CINEMATIC. If you wish to specifically try to set another video aspect ratio you should use the advanced function $toolkit->addCommand. Set $command to "-aspect" and $argument to your required value.',
- 'addWatermark_img_404' => 'Watermark file "#watermark" does not exist.',
- 'addWatermark_vhook_disabled' => 'Vhooking is not enabled in your FFmpeg binary. In order to allow video watermarking you must have FFmpeg compiled with --enable-vhook set. You can however watermark any extracted images using GD. To enable frame watermarking, call $toolkit->addGDWatermark($file) before you execute the extraction.',
- 'addVideo_file_404' => 'File "#file" does not exist.',
- 'setOutput_output_dir_404' => 'Output directory "#dir" does not exist!',
- 'setOutput_output_dir_writable' => 'Output directory "#dir" is not writable!',
- 'setOutput_%_missing' => 'The output of this command will be images yet you have not included the "%index" or "%timecode" in the $output_name.',
- 'setOutput_%d_depreciated' => 'The use of %d in the output file name is now depreciated. Please use %index. Number padding is still supported. You may also use %timecode instead to add a timecode to the filename.',
- 'execute_input_404' => 'Execute error. Input file missing.',
- 'execute_output_not_set' => 'Execute error. Output not set.',
- 'execute_overwrite_process' => 'Execute error. A file exists in the temp directory and is of the same name as this process file. It will conflict with this conversion. Conversion stopped.',
- 'execute_overwrite_fail' => 'Execute error. Output file exists. Process halted. If you wish to automatically overwrite files set the third argument in "PHPVideoToolkit::setOutput();" to "PHPVIDEOTOOLKIT_OVERWRITE_EXISTING".',
- 'execute_partial_error' => 'Execute error. Output for file "#input" encountered a partial error. Files were generated, however one or more of them were empty.',
- 'execute_image_error' => 'Execute error. Output for file "#input" was not found. No images were generated.',
- 'execute_output_404' => 'Execute error. Output for file "#input" was not found. Please check server write permissions and/or available codecs compiled with FFmpeg. You can check the encode decode availability by inspecting the output array from PHPVideoToolkit::getFFmpegInfo().',
- 'execute_output_empty' => 'Execute error. Output for file "#input" was found, but the file contained no data. Please check the available codecs compiled with FFmpeg can support this type of conversion. You can check the encode decode availability by inspecting the output array from PHPVideoToolkit::getFFmpegInfo().',
- 'execute_image_file_exists' => 'Execute error. There is a file name conflict. The file "#file" already exists in the filesystem. If you wish to automatically overwrite files set the third argument in "PHPVideoToolkit::setOutput();" to "PHPVIDEOTOOLKIT_OVERWRITE_EXISTING".',
- 'execute_result_ok_but_unwritable' => 'Process Partially Completed. The process successfully completed however it was not possible to output to "#output". The output was left in the temp directory "#process" for a manual file movement.',
- 'execute_result_ok' => 'Process Completed. The process successfully completed. Output was generated to "#output".',
-
- 'ffmpeg_log_ffmpeg_output' => 'OUTPUT',
- 'ffmpeg_log_ffmpeg_result' => 'RESULT',
- 'ffmpeg_log_ffmpeg_command' => 'COMMAND',
- 'ffmpeg_log_ffmpeg_join_gunk' => 'FFMPEG JOIN OUTPUT',
- 'ffmpeg_log_ffmpeg_gunk' => 'FFMPEG OUTPUT',
- 'ffmpeg_log_separator' => '-------------------------------'
-
- );
-
- /**
- * A public var that is to the information available about
- * the current ffmpeg compiled binary.
- * @var mixed
- * @access public
- */
- var $ffmpeg_info = false;
- /**
- * A private var that contains the info of any file that is accessed by PHPVideoToolkit::getFileInfo();
- * @var array
- * @access private
- */
- var $_file_info = array();
- /**
- * Determines what happens when an error occurs
- * @var boolean If true then the script will die, if not false is return by the error
- * @access public
- */
- var $on_error_die = false;
- /**
- * Holds the log file name
- * @var string
- * @access private
- */
- var $_log_file = null;
- /**
- * Determines if when outputting image frames if the outputted files should have the %d number
- * replaced with the frames timecode.
- * @var boolean If true then the files will be renamed.
- * @access public
- */
- var $image_output_timecode = true;
- /**
- * Holds the timecode separator for when using $image_output_timecode = true
- * Not all systems allow ':' in filenames.
- * @var string
- * @access public
- */
- var $timecode_seperator_output = '-';
- /**
- * Holds the starting time code when outputting image frames.
- * @var string The timecode hh(n):mm:ss:ff
- * @access private
- */
- var $_image_output_timecode_start = '00:00:00.00';
- /**
- * The format in which the image %timecode placeholder string is outputted.
- * - %hh (hours) representative of hours
- * - %mm (minutes) representative of minutes
- * - %ss (seconds) representative of seconds
- * - %fn (frame number) representative of frames (of the current second, not total frames)
- * - %ms (milliseconds) representative of milliseconds (of the current second, not total milliseconds) (rounded to 3 decimal places)
- * - %ft (frames total) representative of total frames (ie frame number)
- * - %st (seconds total) representative of total seconds (rounded).
- * - %sf (seconds floored) representative of total seconds (floored).
- * - %mt (milliseconds total) representative of total milliseconds. (rounded to 3 decimal places)
- * NOTE; there are special characters that will be replace by PHPVideoToolkit::$timecode_seperator_output, these characters are
- * - :
- * - .
- * @var string
- * @access public
- */
- var $image_output_timecode_format = '%hh-%mm-%ss-%fn';
- /**
- * Holds the fps of image extracts
- * @var integer
- * @access private
- */
- var $_image_output_timecode_fps = 1;
- /**
- * Holds the current execute commands that will need to be combined
- * @var array
- * @access private
- */
- var $_commands = array();
- /**
- * Holds the commands executed
- * @var array
- * @access private
- */
- var $_processed = array();
- /**
- * Holds the file references to those that have been processed
- * @var array
- * @access private
- */
- var $_files = array();
- /**
- * Holds the errors encountered
- * @access private
- * @var array
- */
- var $_errors = array();
- /**
- * Holds the input file / input file sequence
- * @access private
- * @var string
- */
- var $_input_file = null;
- /**
- * Holds the output file / output file sequence
- * @access private
- * @var string
- */
- var $_output_address = null;
- /**
- * Holds the process file / process file sequence
- * @access private
- * @var string
- */
- var $_process_address = null;
- /**
- * Temporary filename prefix
- * @access private
- * @var string
- */
- var $_tmp_file_prefix = 'tmp_';
- /**
- * Holds the temporary directory name
- * @access private
- * @var string
- */
- var $_tmp_directory = null;
- /**
- * Holds the directory paths that need to be removed by the ___destruct function
- * @access private
- * @var array
- */
- var $_unlink_dirs = array();
- /**
- * Holds the file paths that need to be deleted by the ___destruct function
- * @access private
- * @var array
- */
- var $_unlink_files = array();
- /**
- * Holds the timer start micro-float.
- * @access private
- * @var integer
- */
- var $_timer_start = 0;
-
- /**
- * Holds the times taken to process each file.
- * @access private
- * @var array
- */
- var $_timers = array();
- /**
- * Holds the times taken to process each file.
- * @access private
- * @var constant
- */
- var $_overwrite_mode = null;
- /**
- * Holds a integer value that flags if the image extraction is just a single frame.
- * @access private
- * @var integer
- */
- var $_single_frame_extraction = null;
- /**
- * Holds the watermark file that is used to watermark any outputted images via GD.
- * @access private
- * @var string
- */
- var $_watermark_url = null;
- /**
- * Holds the watermark options used to watermark any outputted images via GD.
- * @access private
- * @var array
- */
- var $_watermark_options = null;
- /**
- * Holds the number of files processed per run.
- * @access private
- * @var integer
- */
- var $_process_file_count = 0;
- /**
- * Holds the times taken to process each file.
- * @access private
- * @var array
- */
- var $_post_processes = array();
-
- /**
- * Holds commands should be sent added to the exec before the input file, this is by no means a definitive list
- * of all the ffmpeg commands, as it only utilizes the ones in use by this class. Also only commands that have
- * specific required places are entered in the arrays below. Anything not in these arrays will be treated as an
- * after-input item.
- * @access private
- * @var array
- */
- // var $_cmds_before_input = array();
- var $_cmds_before_input = array('-inputr');
- // var $_cmds_before_input = array('-r', '-f');
- /**
- * Constructs the class and sets the temporary directory.
- *
- * @access private
- * @param string $tmp_directory A full absolute path to you temporary directory
- */
- function PHPVideoToolkit($tmp_directory='/tmp/')
- {
- $this->_tmp_directory = $tmp_directory;
- // emulate php5 destructors
- register_shutdown_function(array(&$this, '__destruct'));
- }
-
- function microtimeFloat()
- {
- list($usec, $sec) = explode(" ", microtime());
- return ((float) $usec + (float) $sec);
- }
- /**
- * Resets the class
- *
- * @access public
- * @param boolean $keep_input_file Determines whether or not to reset the input file currently set.
- */
- function reset($keep_input_file=false, $keep_processes=false)
- {
- if($keep_input_file === false)
- {
- $this->_input_file = null;
- }
- if($keep_processes === false)
- {
- $this->_post_processes = array();
- }
- $this->_single_frame_extraction = null;
- $this->_output_address = null;
- $this->_process_address = null;
- $this->_log_file = null;
- $this->_commands = array();
- $this->_timer_start = 0;
- $this->_process_file_count = 0;
- $this->__destruct();
- }
- /**
- * Returns information about the specified file without having to use ffmpeg-php
- * as it consults the ffmpeg binary directly.
- *
- * @access public
- * @param string $file The absolute path of the file that is required to be manipulated.
- * @return mixed false on error encountered, true otherwise
- **/
- function getFFmpegInfo()
- {
- // check to see if this is a static call
- if(!$this)
- {
- $toolkit = new PHPVideoToolkit($tmp_directory);
- return $toolkit->getFFmpegInfo();
- }
- // check to see if the info has already been cached
- if($this->ffmpeg_info !== false)
- {
- return $this->ffmpeg_info;
- }
- // check to see if this is a static call
- $format = '';
- $info_file = $this->_tmp_directory.$this->unique('ffinfo').'.info';
- // execute the ffmpeg lookup
- // exec(PHPVIDEOTOOLKIT_FFMPEG_BINARY.' -formats &> '.$info_file);
- exec(PHPVIDEOTOOLKIT_FFMPEG_BINARY.' -formats 2>&1', $buffer);
- $buffer = implode("\r\n", $buffer);
-
- // $data = false;
- // try to open the file
- // $handle = fopen($info_file, 'r');
- // if($handle)
- // {
- // $data = array();
- // $buffer = '';
- // // loop through the lines of data and collect the buffer
- // while (!feof($handle))
- // {
- // $buffer .= fgets($handle, 4096);
- // }
- // echo($buffer);
- $data['compiler'] = array();
- $look_ups = array('configuration'=>'configuration: ', 'formats'=>'File formats:', 'codecs'=>'Codecs:', 'filters'=>'Bitstream filters:', 'protocols'=>'Supported file protocols:', 'abbreviations'=>'Frame size, frame rate abbreviations:', 'Note:');
- $total_lookups = count($look_ups);
- $pregs = array();
- $indexs = array();
- foreach($look_ups as $key=>$reg)
- {
- if(strpos($buffer, $reg) !== false)
- {
- $index = array_push($pregs, $reg);
- $indexs[$key] = $index;
- }
- }
- preg_match('/'.implode('(.*)', $pregs).'/s', $buffer, $matches);
- $configuration = trim($matches[$indexs['configuration']]);
- // grab the ffmpeg configuration flags
- preg_match_all('/--[a-zA-Z0-9\-]+/', $configuration, $config_flags);
- $data['compiler']['configuration'] = $config_flags[0];
- $data['compiler']['vhook-support'] = in_array('--enable-vhook', $config_flags[0]) && !in_array('--disable-vhook', $config_flags[0]);
- // grab the versions
- $data['compiler']['versions'] = array();
- preg_match_all('/([a-zA-Z0-9\-]+) version: ([0-9\.]+)/', $configuration, $versions);
- for($i=0, $a=count($versions[0]); $i<$a; $i++)
- {
- $data['compiler']['versions'][strtolower(trim($versions[1][$i]))] = $versions[2][$i];
- }
- // grab the ffmpeg compile info
- preg_match('/built on (.*), gcc: (.*)/', $configuration, $conf);
- if(count($conf) > 0)
- {
- $data['compiler']['gcc'] = $conf[2];
- $data['compiler']['build_date'] = $conf[1];
- $data['compiler']['build_date_timestamp'] = strtotime($conf[1]);
- }
- // grab the file formats available to ffmpeg
- preg_match_all('/ (DE|D|E) (.*) {1,} (.*)/', trim($matches[$indexs['formats']]), $formats);
- $data['formats'] = array();
- // loop and clean
- for($i=0, $a=count($formats[0]); $i<$a; $i++)
- {
- $data['formats'][strtolower(trim($formats[2][$i]))] = array(
- 'encode' => $formats[1][$i] == 'DE' || $formats[1][$i] == 'E',
- 'decode' => $formats[1][$i] == 'DE' || $formats[1][$i] == 'D',
- 'fullname' => $formats[3][$i]
- );
- }
- // grab the bitstream filters available to ffmpeg
- $data['filters'] = array();
- if(isset($indexs['filters']) && isset($matches[$indexs['filters']]))
- {
- $filters = trim($matches[$indexs['filters']]);
- if(empty($filters))
- {
- $data['filters'] = explode(' ', $filters);
- }
- }
- // grab the file prototcols available to ffmpeg
- $data['filters'] = array();
- if(isset($indexs['protocols']) && isset($matches[$indexs['protocols']]))
- {
- $protocols = trim($matches[$indexs['protocols']]);
- if(empty($protocols))
- {
- $data['protocols'] = explode(' ', str_replace(':', '', $protocols));
- }
- }
- // grab the abbreviations available to ffmpeg
- $data['abbreviations'] = array();
- if(isset($indexs['abbreviations']) && isset($matches[$indexs['abbreviations']]))
- {
- $abbreviations = trim($matches[$indexs['abbreviations']]);
- if(empty($abbreviations))
- {
- $data['abbreviations'] = explode(' ', $abbreviations);
- }
- }
- $this->ffmpeg_info = $data;
- }
- // fclose($handle);
- // if(is_file($info_file))
- // {
- // // if the info file exists remove it
- // unlink($info_file);
- // }
- $data['ffmpeg-php-support'] = $this->hasFFmpegPHPSupport();
- return $data;
-
- }
-
- /**
- * Determines the type of support that exists for the FFmpeg-PHP module.
- *
- * @access public
- * @return mixed. Boolean false if there is no support, String 'module' if the actuall
- * FFmpeg-PHP module is loaded, or String 'emulated' if the FFmpeg-PHP classes
- * can be emulated through the adapter classes.
- */
- function hasFFmpegPHPSupport()
- {
- return extension_loaded('ffmpeg') ? 'module' : (is_file(dirname(__FILE__).DIRECTORY_SEPARATOR.'adapters'.DIRECTORY_SEPARATOR.'ffmpeg-php'.DIRECTORY_SEPARATOR.'ffmpeg_movie.php') && is_file(dirname(__FILE__).DIRECTORY_SEPARATOR.'adapters'.DIRECTORY_SEPARATOR.'ffmpeg-php'.DIRECTORY_SEPARATOR.'ffmpeg_frame.php') && is_file(dirname(__FILE__).DIRECTORY_SEPARATOR.'adapters'.DIRECTORY_SEPARATOR.'ffmpeg-php'.DIRECTORY_SEPARATOR.'ffmpeg_animated_gif.php') ? 'emulated' : false);
- }
-
- /**
- * Determines if the ffmpeg binary has been compiled with vhook support.
- *
- * @access public
- * @return mixed. Boolean false if there is no support, true there is support.
- */
- function hasVHookSupport()
- {
- $info = $this->getFFmpegInfo();
- return $info['compiler']['vhook-support'];
- }
-
- /**
- * Returns information about the specified file without having to use ffmpeg-php
- * as it consults the ffmpeg binary directly. This idea for this function has been borrowed from
- * a French ffmpeg class located: http://www.phpcs.com/codesource.aspx?ID=45279
- *
- * @access public
- * @todo Change the search from string explode to a regex based search
- * @param string $file The absolute path of the file that is required to be manipulated.
- * @return mixed false on error encountered, true otherwise
- **/
- function getFileInfo($file=false, $tmp_directory='/tmp/')
- {
- // check to see if this is a static call
- if($file !== false && !$this)
- {
- $toolkit = new PHPVideoToolkit($tmp_directory);
- return $toolkit->getFileInfo($file);
- }
- // if the file has not been specified check to see if an input file has been specified
- if($file === false)
- {
- if(!$this->_input_file)
- {
- // input file not valid
- return $this->_raiseError('getFileInfo_no_input');
- //<- exits
- }
- $file = $this->_input_file;
- }
- $file = escapeshellarg($file);
- // die($file);
- // create a hash of the filename
- $hash = md5($file);
- // check to see if the info has already been generated
- if(isset($this->_file_info[$hash]))
- {
- return $this->_file_info[$hash];
- }
- // generate a random filename
- $info_file = $this->_tmp_directory.$this->unique($hash).'.info';
-
- // execute the ffmpeg lookup
- // exec(PHPVIDEOTOOLKIT_FFMPEG_BINARY.' -i '.$file.' &> '.$info_file);
- exec(PHPVIDEOTOOLKIT_FFMPEG_BINARY.' -i '.$file.' 2>&1', $buffer);
- $buffer = implode("\r\n", $buffer);
-
- // $data = false;
- // // try to open the file
- // $handle = fopen($info_file, 'r');
- // if($handle)
- // {
- // $data = array();
- // $buffer = '';
- // // loop through the lines of data and collect the buffer
- // while (!feof($handle))
- // {
- // $buffer .= fgets($handle, 4096);
- // }
- // die($buffer);
- // grab the duration and bitrate data
- preg_match_all('/Duration: (.*)/', $buffer, $matches);
- if(count($matches) > 0)
- {
- $parts = explode(', ', trim($matches[1][0]));
- $data['duration'] = array();
- $timecode = $parts[0];
- $data['duration']['seconds'] = $this->timecodeToSeconds($timecode);
- $data['bitrate'] = intval(ltrim($parts[2], 'bitrate: '));
- $data['duration']['start'] = ltrim($parts[1], 'start: ');
- $data['duration']['timecode'] = array();
- $data['duration']['timecode']['rounded'] = substr($timecode, 0, 8);
- $data['duration']['timecode']['seconds'] = array();
- $data['duration']['timecode']['seconds']['exact'] = $timecode;
- $data['duration']['timecode']['seconds']['excess'] = intval(substr($timecode, 9));
- }
-
- // match the video stream info
- preg_match('/Stream(.*): Video: (.*)/', $buffer, $matches);
- if(count($matches) > 0)
- {
- $data['video'] = array();
- // get the dimension parts
- // print_r($matches);
- preg_match('/([0-9]{1,5})x([0-9]{1,5})/', $matches[2], $dimensions_matches);
- // print_r($dimensions_matches);
- $dimensions_value = $dimensions_matches[0];
- $data['video']['dimensions'] = array(
- 'width' => floatval($dimensions_matches[1]),
- 'height' => floatval($dimensions_matches[2])
- );
- // get the framerate
- preg_match('/([0-9\.]+) (fps|tb)\(r\)/', $matches[0], $fps_matches);
- $data['video']['frame_rate'] = floatval($fps_matches[1]);
- $fps_value = $fps_matches[0];
- // get the ratios
- preg_match('/\[PAR ([0-9\:\.]+) DAR ([0-9\:\.]+)\]/', $matches[0], $ratio_matches);
- if(count($ratio_matches))
- {
- $data['video']['pixel_aspect_ratio'] = $ratio_matches[1];
- $data['video']['display_aspect_ratio'] = $ratio_matches[2];
- }
- // work out the number of frames
- if(isset($data['duration']) && isset($data['video']))
- {
- // set the total frame count for the video
- $data['video']['frame_count'] = ceil($data['duration']['seconds'] * $data['video']['frame_rate']);
- // set the framecode
- $frames = ceil($data['video']['frame_rate']*($data['duration']['timecode']['seconds']['excess']/10));
- $data['duration']['timecode']['frames'] = array();
- $data['duration']['timecode']['frames']['exact'] = $data['duration']['timecode']['rounded'].'.'.$frames;
- $data['duration']['timecode']['frames']['excess'] = $frames;
- $data['duration']['timecode']['frames']['total'] = $data['video']['frame_count'];
- }
- // formats should be anything left over, let me know if anything else exists
- $parts = explode(',', $matches[2]);
- $other_parts = array($dimensions_value, $fps_value);
- $formats = array();
- foreach($parts as $key=>$part)
- {
- $part = trim($part);
- if(!in_array($part, $other_parts))
- {
- array_push($formats, $part);
- }
- }
- $data['video']['pixel_format'] = $formats[1];
- $data['video']['codec'] = $formats[0];
- }
-
- // match the audio stream info
- preg_match('/Stream(.*): Audio: (.*)/', $buffer, $matches);
- if(count($matches) > 0)
- {
- // setup audio values
- $data['audio'] = array(
- 'stereo' => -1,
- 'sample_rate' => -1,
- 'sample_rate' => -1
- );
- $other_parts = array();
- // get the stereo value
- preg_match('/(stereo|mono)/i', $matches[0], $stereo_matches);
- if(count($stereo_matches))
- {
- $data['audio']['stereo'] = $stereo_matches[0];
- array_push($other_parts, $stereo_matches[0]);
- }
- // get the sample_rate
- preg_match('/([0-9]{3,6}) Hz/', $matches[0], $sample_matches);
- if(count($sample_matches))
- {
- $data['audio']['sample_rate'] = count($sample_matches) ? floatval($sample_matches[1]) : -1;
- array_push($other_parts, $sample_matches[0]);
- }
- // get the bit rate
- preg_match('/([0-9]{1,3}) kb\/s/', $matches[0], $bitrate_matches);
- if(count($bitrate_matches))
- {
- $data['audio']['bitrate'] = count($bitrate_matches) ? floatval($bitrate_matches[1]) : -1;
- array_push($other_parts, $bitrate_matches[0]);
- }
- // formats should be anything left over, let me know if anything else exists
- $parts = explode(',', $matches[2]);
- $formats = array();
- foreach($parts as $key=>$part)
- {
- $part = trim($part);
- if(!in_array($part, $other_parts))
- {
- array_push($formats, $part);
- }
- }
- $data['audio']['codec'] = $formats[0];
- }
- // check that some data has been obtained
- if(!count($data))
- {
- $data = false;
- }
- else
- {
- $data['_raw_info'] = $buffer;
- }
-
- // fclose($handle);
- // }
- // if(is_file($info_file))
- // {
- // // if the info file exists remove it
- // unlink($info_file);
- // }
- // cache info and return
- return $this->_file_info[$hash] = $data;
- }
- /**
- * Determines if your ffmpeg has particular codec support for encode or decode.
- *
- * @access public
- * @param string $codec The name of the codec you are checking for.
- * @param const $support PHPVideoToolkit::ENCODE or PHPVideoToolkit::DECODE, depending on which functionality is desired.
- * @return mixed. Boolean false if there is no support, true if there is support.
- */
- public function hasCodecSupport($codec, $support=PHPVideoToolkit::ENCODE)
- {
- $codec = strtolower($codec);
- $data = $this->getFFmpegInfo();
- return isset($data['formats'][$codec]) ? $data['formats'][$codec][$support] : false;
- }
- /**
- * Sets the input file that is going to be manipulated.
- *
- * @access public
- * @param string $file The absolute path of the file that is required to be manipulated.
- * @param mixed $input_frame_rate If 0 (default) then no input frame rate is set, if false it is automatically retreived, otherwise
- * any other integer will be set as the incoming frame rate.
- * @return boolean false on error encountered, true otherwise
- */
- function setInputFile($file, $input_frame_rate=0)
- {
- $files_length = count($file);
- // if the total number of files entered is 1 then only one file is being processed
- if($files_length == 1)
- {
- // check the input file, if there is a %d in there or a similar %03d then the file inputted is a sequence, if neither of those is found
- // then qheck to see if the file exists
- if(!preg_match('/\%([0-9]+)d/', $file) && strpos($file, '%d') === false && !is_file($file))
- {
- // input file not valid
- return $this->_raiseError('setInputFile_file_existence', array('file'=>$file));
- //<- exits
- }
- $escaped_name = $file;
- // $escaped_name = escapeshellarg($files[0]);
- $this->_input_file = $escaped_name;
- $this->_input_file_id = md5($escaped_name);
-
- // the -inputr is a hack for -r to come before the input
- if($input_frame_rate !== 0)
- {
- $info = $this->getFileInfo();
- if(isset($info['video']))
- {
- if($input_frame_rate === false)
- {
- $input_frame_rate = $info['video']['frame_rate'];
- }
- // input frame rate is a command hack
- $this->addCommand('-inputr', $input_frame_rate);
- }
- }
- }
- else
- {
- // more than one video is being added as input so we must join them all
- call_user_func_array(array(&$this, 'addVideo'), array($file, $input_frame_rate));
- }
- return true;
- }
-
- /**
- * A shortcut for converting video to FLV.
- *
- * @access public
- * @param integer $audio_sample_frequency
- * @param integer $audio_bitrate
- */
- function setFormatToFLV($audio_sample_frequency=44100, $audio_bitrate=64)
- {
- $this->addCommand('-sameq');
- $this->setAudioFormat(PHPVIDEOTOOLKIT_FORMAT_MP3);
- // adjust the audio rates
- $this->setAudioBitRate($audio_bitrate);
- $this->setAudioSampleFrequency($audio_sample_frequency);
- // set the video format
- $this->setFormat(PHPVIDEOTOOLKIT_FORMAT_FLV);
- // flag that the flv has to have meta data added after the excecution of this command
- // register the post tidy process
- $this->registerPostProcess('_addMetaToFLV', $this);
- }
-
- /**
- * When converting video to FLV the meta data has to be added by a ruby program called FLVTools2.
- * This is a second exec call only after the video has been converted to FLV
- * http://inlet-media.de/flvtool2
- *
- * @access private
- */
- function _addMetaToFLV($files)
- {
- $file = array_pop($files);
- // prepare the command suitable for exec
- $exec_string = $this->_prepareCommand(PHPVIDEOTOOLKIT_FLVTOOLS_BINARY, '-U '.$file);
- // execute the command
- exec($exec_string);
- if(is_array($this->_processed[0]))
- {
- array_push($this->_processed[0], $exec_string);
- }
- else
- {
- $this->_processed[0] = array($this->_processed[0], $exec_string);
- }
- return true;
- }
- /**
- * Streams a FLV file from a given point. You can control bandwidth, cache and session options.
- * Inspired by xmoov-php
- * @see xmoov-php,
- * - @link http://xmoov.com/
- * - @author Eric Lorenzo Benjamin jr
- * @access public
- * @param integer $seek_pos The position in the file to seek to.
- * @param array|boolean $bandwidth_options If a boolean value, false then no bandwidth limiting will take place.
- * If true then bandwidth limiting will take place with packet_size = 90 and packet_interval = 0.3.
- * If an array the following values are default packet_size = 90 and packet_interval = 0.3, you will also
- * have to set active = true, ie array('active'=>true, 'packet_size'=>90, 'packet_interval'=>0.3)
- * @param boolean $allow_cache If true the file will be allowed to cache in the browser, if false then it won't
- * @return boolean
- */
- public function flvStreamSeek($seek_pos=0, $bandwidth_options=array(), $allow_cache=true)
- {
- // check for input file
- if(!$this->_input_file)
- {
- // input file not valid
- return $this->_raiseError('streamFLV_no_input');
- //<- exits
- }
- // make the pos safe
- $seek_pos = intval($seek_pos);
- // absorb the bandwidth options
- $bandwidth_options = is_array($bandwidth_options) ? array_merge(array('active'=>false, 'packet_size'=>90, 'packet_interval'=>0.3), $bandwidth_options) : array('active'=>$bandwidth_options, 'packet_size'=>90, 'packet_interval'=>0.3);
- // if this file is not allowed to be cached send cache headers for all browsers.
- if(!$allow_cache)
- {
- session_cache_limiter('nocache');
- header('Expires: Thu, 19 Nov 1981 08:52:00 GMT');
- header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
- header('Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0');
- header('Pragma: no-cache');
- }
- // open file
- $handle = fopen($this->_input_file, 'rb');
- $file_size = filesize($this->_input_file) - (($seek_pos > 0) ? $seek_pos + 1 : 0);
- // send the flv headers
- header('Content-Type: video/x-flv');
- header('Content-Disposition: attachment; filename="'.basename($this->_input_file).'"');
- header('Content-Length: '.$file_size);
- // flv format header
- if($seek_pos != 0)
- {
- print('FLV');
- print(pack('C', 1));
- print(pack('C', 1));
- print(pack('N', 9));
- print(pack('N', 9));
- }
- // seek to the required point
- if(fseek($handle, $seek_pos) === -1)
- {
- // input file not valid
- return $this->_raiseError('streamFLV_passed_eof');
- //<- exits
- }
- // if bandwidth control is active then workout the options
- if($bandwidth_options['active'])
- {
- $packet_interval = intval($bandwidth_options['packet_interval']);
- $packet_size = intval($bandwidth_options['packet_size']) * 1042;
- }
- // output the file
- while(!feof($handle))
- {
- // limit the bandwidth
- if($bandwidth_options['active'] && $packet_interval > 0)
- {
- // output the required packet
- $time_start = $this->microtimeFloat();
- echo fread($handle, $packet_size);
- $time_stop = $this->microtimeFloat();
- // delay the output
- $time_difference = $time_stop - $time_start;
- if($time_difference < $packet_interval)
- {
- usleep(($packet_interval * 1000000) - ($time_difference * 1000000));
- }
- }
- // no bandwidth limiting
- else
- {
- echo fread($handle, $file_size);
- }
- }
- // close the file
- fclose($handle);
- return true;
- }
- /**
- * This is an alias for setFormat, but restricts it to audio only formats.
- *
- * @access public
- * @param integer $format A supported audio format.
- * @param integer $audio_sample_frequency
- * @param integer $audio_bitrate
- **/
- function extractAudio($format=PHPVIDEOTOOLKIT_FORMAT_MP3, $audio_sample_frequency=44100, $audio_bitrate=64)
- {
- // check the format is one of the audio formats
- if(!in_array($format, array(PHPVIDEOTOOLKIT_FORMAT_AAC, PHPVIDEOTOOLKIT_FORMAT_AIFF, PHPVIDEOTOOLKIT_FORMAT_MP2, PHPVIDEOTOOLKIT_FORMAT_MP3, PHPVIDEOTOOLKIT_FORMAT_MP4, PHPVIDEOTOOLKIT_FORMAT_MPEG4, PHPVIDEOTOOLKIT_FORMAT_M4A, PHPVIDEOTOOLKIT_FORMAT_WAV)))
- {
- return $this->_raiseError('extractAudio_valid_format', array('format'=>$format));
- //<- exits
- }
- $this->setFormat($format);
- // adjust the audio rates
- $this->setAudioBitRate($audio_bitrate);
- $this->setAudioSampleFrequency($audio_sample_frequency);
- }
- /**
- * Sets the new video format.
- *
- * @access public
- * @param defined $format The format should use one of the defined variables stated below.
- * PHPVIDEOTOOLKIT_FORMAT_3GP2 - 3g2
- * PHPVIDEOTOOLKIT_FORMAT_3GP - 3gp
- * PHPVIDEOTOOLKIT_FORMAT_AAC - aac
- * PHPVIDEOTOOLKIT_FORMAT_AIFF - aiff
- * PHPVIDEOTOOLKIT_FORMAT_AMR - amr
- * PHPVIDEOTOOLKIT_FORMAT_ASF - asf
- * PHPVIDEOTOOLKIT_FORMAT_AVI - avi
- * PHPVIDEOTOOLKIT_FORMAT_FLV - flv
- * PHPVIDEOTOOLKIT_FORMAT_GIF - gif
- * PHPVIDEOTOOLKIT_FORMAT_MJ2 - mj2
- * PHPVIDEOTOOLKIT_FORMAT_MP2 - mp2
- * PHPVIDEOTOOLKIT_FORMAT_MP3 - mp3
- * PHPVIDEOTOOLKIT_FORMAT_MP4 - mp4
- * PHPVIDEOTOOLKIT_FORMAT_MPEG4 - mpeg4
- * PHPVIDEOTOOLKIT_FORMAT_M4A - m4a
- * PHPVIDEOTOOLKIT_FORMAT_MPEG - mpeg
- * PHPVIDEOTOOLKIT_FORMAT_MPEG1 - mpeg1video
- * PHPVIDEOTOOLKIT_FORMAT_MPEG2 - mpeg2video
- * PHPVIDEOTOOLKIT_FORMAT_MPEGVIDEO - mpegvideo
- * PHPVIDEOTOOLKIT_FORMAT_PSP - psp
- * PHPVIDEOTOOLKIT_FORMAT_RM - rm
- * PHPV…
Large files files are truncated, but you can click here to view the full file