CodeIgniter Forums
FFMPEG Convertion Library... HELP! - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: FFMPEG Convertion Library... HELP! (/thread-2541.html)



FFMPEG Convertion Library... HELP! - El Forum - 08-11-2007

[eluser]Phil Sturgeon[/eluser]
I have a very simple library created purely for one project and a simple convertion.

I have the code:

Code:
<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
* CodeIgniter
*
* An open source application development framework for PHP 4.3.2 or newer
*
* @package        CodeIgniter
* @author        Rick Ellis
* @copyright    Copyright (c) 2006, EllisLab, Inc.
* @license        http://www.codeignitor.com/user_guide/license.html
* @link        http://www.codeigniter.com
* @since        Version 1.0
* @filesource
*/

// ------------------------------------------------------------------------

/**
* Video Class
*
* @package        CodeIgniter
* @subpackage    Libraries
* @category    Libraries
* @author        Philip Sturgeon
*/
class Video {

    var $filename             = '';
    var $videoWidth            = 0;
    var $videoHeight        = 0;
    var $videoFPS            = 0;
    var $audioBitRate        = 0;
    var $audioSampleRate    = 0;
    var $duration            = 0;
    
    var $error_string = '';
    
    var $videoObj;
    var $CI;
    
    /**
     * Constructor - Sets Preferences
     *
     * The constructor can be passed an array of config values
     */    
    function Video()
    {    
        $this->CI =& get_instance();
        $this->CI->config->load('content');
        
        log_message('debug', "Video Class Initialized");
        
        if(extension_loaded('ffmpeg')):
            log_message('debug', "Video Class exited before finishing, missing the FFMPEG extention.");
            $this->error_string  = "ffmpeg extension not loaded";
        endif;
    }

    // --------------------------------------------------------------------

    /**
     * Initialize preferences
     *
     * @access    public
     * @param    array
     * @return    void
     */    

    function load_video($srcFile = '')
    {
        $this->CI=& get_instance();
                        
        // Create our FFMPEG-PHP object
        $this->videoObj = new ffmpeg_movie($srcFile);
        
        // Store the name of the file
        $this->filename = $srcFile;
        
        if(!empty($this->videoObj)):
        
            $this->videoWidth        = $this->_makeMultipleTwo($this->videoObj->getFrameWidth());
            $this->videoHeight        = $this->_makeMultipleTwo($this->videoObj->getFrameHeight());
            $this->videoFPS            = $this->videoObj->getFrameRate();
            $this->audioBitRate        = @intval($this->videoObj->getAudioBitRate()/1000);
            $this->audioSampleRate    = @$this->videoObj->getAudioSampleRate();
            $this->duration            = $this->videoObj->getDuration();
        
        else:
            $this->error_string = 'Unable to initiate video properly.';
            
        endif;
    }
    
    /**
     * Convert the file to FLV
     *
     * @access    public
     * @param    array
     * @return    void
     */    
    function convert_to_flv($destFolder)
    {
        $this->CI=& get_instance();
        
        $ffmpegPath = $this->CI->config->item('ffmpegPath');
        $flvtool2Path = $this->CI->config->item('flvtool2Path');
        
        // Extention of file is...
        list($name, $ext) = explode('.', basename($this->filename));
        $converted_name = $destFolder.$name.'.flv';
        
        // Call our convert using exec()
        exec($ffmpegPath . " -i " . $this->filename . " -ar " . $this->audioSampleRate . " -ab " . $this->audioBitRate . " -f flv -s " . $this->videoWidth . "x" . $this->videoHeight . " " . $converted_name . " | " . $flvtool2Path . " -U stdin " . $converted_name);
                
        // Delete the origional file if the new files size is more than 0
        if(file_exists($this->filename) && filesize($converted_name) > 0)
        {
            unlink($this->filename);
            $this->filename = $converted_name;    
        } else {
            log_message('debug', "Video convert error, new file size is 0");
            return false;
        }
        
        // Check the new file exists, return true or false
        return (file_exists($this->filename));
    
    }
    
    // Take the current video object and creates a thumbail
    function create_thumb($thumbFile)
    {
        if(!is_object($this->videoObj)) echo 'FALSE';
        
        // Pick a frame to use as the thumb, this is 1/8th through it    
        //$captureFrame = round($this->videoObj->getFrameCount() / 3);
        
        $thumb=$this->videoObj->getFrame(20);

        if ($thumb) {

            $thumbImage = $thumb->toGDImage();
            if ($thumbImage) {

                    imagepng($thumbImage,$thumbFile);
                    imagedestroy($thumbImage);
                    
                    return true;
            }
        }
    
        return false;
    }
    

    // --------------------------------------------------------------------

    // Used in the convertion pricess!
    function _makeMultipleTwo ($value)
    {
        $sType = gettype($value/2);
        return ($sType == "integer") ? $value : ($value-1);
    }

}
// END VIDEO Class
?>

This doesnt seem to work on Safari... only a problem safari!! Instead of converting the file it just makes a 0byte flv file then deletes the origional.

Im wondering if I should have a loop that waits for it to convert somehow... or something... somebody pls help on this, its a very odd error and I need it sorted soon.


FFMPEG Convertion Library... HELP! - El Forum - 02-24-2009

[eluser]phpserver[/eluser]
Hi,a friend of mine and i are developing a class that will show how you can upload,convert to .flv and store a video link to mysql.

Its 2 years since you posted the class above,have you succeded in making a flv video class that works?


FFMPEG Convertion Library... HELP! - El Forum - 02-24-2009

[eluser]darkhouse[/eluser]
I had the same problem, except it happened in all browsers, but I fixed it just last week. I even had the same exec command. Unfortunately the server I have the fix on is down right now or I'd grab you the new exec line. I'm working on this project later today (after the server restarts) and I'll give you the code I used. I didn't even need flvtool2, or the audio settings from ffmpeg_movie. I'll get back to you later, sorry for the delay.


FFMPEG Convertion Library... HELP! - El Forum - 02-24-2009

[eluser]phpserver[/eluser]
Macro,i will be waiting.Thanks.

[quote author="darkhouse" date="1235502648"]I had the same problem, except it happened in all browsers, but I fixed it just last week. I even had the same exec command. Unfortunately the server I have the fix on is down right now or I'd grab you the new exec line. I'm working on this project later today (after the server restarts) and I'll give you the code I used. I didn't even need flvtool2, or the audio settings from ffmpeg_movie. I'll get back to you later, sorry for the delay.[/quote]


FFMPEG Convertion Library... HELP! - El Forum - 02-24-2009

[eluser]Phil Sturgeon[/eluser]
Forgot about this class. Wrote it in a time before I understood OOP or good class creation. If I remember rightly the issue was more in the uploading than in the converting. When the file is on the server it would be independant of browser problems.

Let me know how it goes!


FFMPEG Convertion Library... HELP! - El Forum - 02-24-2009

[eluser]darkhouse[/eluser]
Here's the conversion command I'm using, and I'll show you how I get the video dimensions using ffmpeg_movie:

Code:
$srcFile = 'someFile.mpg';
$destFile = 'someFile.flv';

$ffmpegObj = new ffmpeg_movie($srcFile);
        
$srcWidth = $ffmpegObj->getFrameWidth();
$srcWidth -= $srcWidth & 1;
            
$srcHeight = $ffmpegObj->getFrameHeight();
$srcHeight -= $srcHeight & 1;

$srcSize = $srcWidth.'x'.$srcHeight;

exec("ffmpeg -i {$srcFile} -sameq -acodec libmp3lame -ar 22050 -ab 32 -f flv -s {$srcSize} {$destFile}");

This code works great for me, hopefully it works for you as well.


FFMPEG Convertion Library... HELP! - El Forum - 03-09-2009

[eluser]Milind[/eluser]
Hello friend

I am installing FFMpeg successfully also verified ffmpeg is successfully installed by executing php.inf file but how to verified flvtool2 is on my machine or installed on machine and also tell its source/link where i found it.

Sorry about my english


FFMPEG Convertion Library... HELP! - El Forum - 03-19-2009

[eluser]Milind[/eluser]
hello friends

I am trying to upload vedio and creating its thumbnail so follow above code but i got error "Can't open movie file funny dogs.wmv"
my code is
$srcFile=$_FILES['upload_file']['name'];
$destFile="out.flv";
$ffmpegPath="c:/wamp/www/codeigniter/system/plugins/ffmpeg.exe";
$ffmpegTool="c:/wamp/www/codeigniter/system/plugins/flvtool2.exe";

$ffmpegobj = new ffmpeg_movie($srcFile);
$srcwidth = $ffmpegobj->getFrameWidth();
echo $srcwidth;
//$srcwidth -= $srcwidth & 1;
//$srcheight = $ffmpegobj->getFrameHeight();
//$srcheight -= $srcwidth & 1;
//$srcsize=$srcwidth.'x'.$srcheight;


// $config['upload_path'] = 'c:/wamp/www/CodeIgniter/system/application/ffmpeg/';
// $config['allowed_types'] = 'avi|dat|flv|mp3|mp4|MPEG|rm|wmv';
// $config['max_size'] = '112400 ';


$this->load->library('upload', $config);



if ( ! $this->upload->do_upload('upload_file'))
{
$error = array('error' => $this->upload->display_errors());
echo $error['error'];
$this->load->view('upload_video', $error);
}
else
{ exec("$ffmpegPath -i {$srcFile} -sameq -acodec libmp3lame -ar 22050 -ab 32 -f flv -s {$srcSize} {$destFile}");
$data = array('upload_data' => $this->upload->data());
$this->load->view('upload_suceess');
//printf("frame height = %d pixels\n", $ffmpegobj->getFrameHeight());
//printf("frame width = %d pixels\n", $ffmpegobj->getFrameWidth());
//echo "hi ";

}