Jump to content

Recommended Posts

Ok, I'm rather new to php so sorry for that.

 

I'm trying to use ffmpeg to convert videos to flv, I can get it to work but I want the input and output files to be variables.

 

My current code looks like this:

<?php
class convert {
    var $source;
    var $destination;
    function convert() {
        // this will create a .flv from a video file
        // you can change the other parameters if you are an expert 
        exec("ffmpeg -i -f {$this->source}  {$this->destination}");
    }
}
$jf=new convert();
$jf->source="cat.mpeg";
$jf->destination="cat.flv";
$jf->convert();
?>

 

The problem is the variables don't seem to be read, I'm pretty sure it is the way they are written. I know ffmpeg works because if I replace the $source & $destination the conversion works fine.

 

Any tips on how to correct this?

Link to comment
https://forums.phpfreaks.com/topic/104178-solved-problem-with-simple-ffmpeg-class/
Share on other sites

Still nothing  :(

 

Current code:

 

<?php

class convert {
    var $source;
    var $destination;
    function convert() {
        // this will create a .flv from a video file
        // you can change the other parameters if you are an expert 
        exec("ffmpeg -i -f {$this->source}  {$this->destination}");
    }
}
$jf=new convert;
$jf->source="cat.mpeg";
$jf->destination="cat.flv";
$jf->convert();
?>

Solved it using:

 

<?php
class convert
{

	function convert($source,$destination)
	{
	/* @source=source file including full physical path
		@destination=destination file including full physical path*/
	// this will create a .flv from .wmv
	// you can chnage the other parameters if you are an expert 
	exec("ffmpeg -i '{$source}' -acodec libmp3lame -ar 22050 -ab 32 -f flv {$destination}");
	}
}


$source=$file;
$destination="{$file}.flv";

$jf=new convert($source,$destination);


?>

  • 2 weeks later...

Now it isn't really a class, just a function, but what would work and leave it much as before is taking the variables outside the quotes for the exec function, so just changing the exec line to the following:

 

exec("ffmpeg -i -f {".$this->source."}  {".$this->destination."}");

 

Oh, and leave off the () after "new convert" as we mentioned before.

aha! Just realised what I did was wrong in the other board... try changing this line:

 

$jf=new convert();

 

To this:

 

$jf=new convert;

 

Ehm.. Those are functionally exactly the same.

 

<?php
class convert {
    var $source;
    var $destination;
    function convert() {
        // this will create a .flv from a video file
        // you can change the other parameters if you are an expert 
        exec("ffmpeg -i -f {$this->source}  {$this->destination}");
    }
}
$jf=new convert();
$jf->source="cat.mpeg";
$jf->destination="cat.flv";
$jf->convert();
?>

 

This code calls convert() twice. Once as the constructor (with source and destination still at NULL) and once after construction (source and destination should be populated). So it 'should' work (probably there is some error in the execution of ffmpeg the second time), but not correctly in any case.

 

Try this instead:

 

<?php
//php4
class Conversion 
{
    var $_source;
    var $_destination;
    
    function Conversion($src, $dest)
    {
    	$this->_source = $src;
	$this->_destination = $dest;
    }
    
    function execute($options = '-i -f')
    {
        return exec("ffmpeg $options {$this->_source} {$this->_destination}");
    }
}
$conversion = new Conversion("cat.mpeg", "cat.flv");
$conversion->execute();

/**
* OR:
*
*/

//php5
class Conversion 
{
    private $_source;
    private $_destination;
    
    function __construct($src, $dest)
    {
    	$this->_source = $src;
	$this->_destination = $dest;
    }
    
    function execute($options = '-i -f')
    {
        return exec("ffmpeg $options {$this->_source} {$this->_destination}");
    }
}
$conversion = new Conversion("cat.mpeg", "cat.flv");
$conversion->execute();
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.