Encoding with ffmpeg AND two-pass

2 replies [Last post]
Joined: 02/14/2011
Points: 53

Hi, has anyone of you guys encoded a video using the ffmpeg engine included with the Kaltura CE 5.0, and with the param "<twoPass>" set to true (or 1)?
I've seen that no matter if "twoPass" is on or off, the command launched for ffmpeg does not include the "-pass" in it.

Is there any reason for this?

Joined: 02/14/2011
Points: 53

Well, I was right. <twoPass> does not change the behavior of the encoding when the conversionEngine is 2, ffmpeg (this was tested with the help of the admin_console's Test Console, and the logs).
Actually, I've found another malfunction related with ffmpeg. I was trying to encode a video built with multistream (several video streams inside the file), but ffmpeg was only taking the first two streams (audio and video), and the video one was the worst quality stream of the file. The problem was ffmpeg (version 0.6, btw) was not treating all the streams of the file, and that's why I was only seeing videos in the KMC as a result of encoding the poor quality part of my videos instead of selecting the best quality.

Detailed below are the fixes that I made in my Kaltura CE 5.0:

1.- Update ffmpeg.

0.6 release branch isn't maintained anymore by the guys of ffmpeg, so IMO, it needs to be updated. I've done many tests with the last stable version of the 1.0 release, so it worked for me.

You can download the code and compile it, or you can download an already built version. In the website of the project ffmpeg you can find many options and documentation.

Place the new ffmpeg in a new folder inside the path /opt/kaltura/bin. I've placed mine in a folder named "ffmpeg-1.0-dir".

Change the link the executable file points inside the bin folder (and don't forget to check the permissions of the new executable):

cd /opt/kaltura/bin
mv ffmpeg ffmpeg.old
ln -s /opt/kaltura/bin/ffmpeg-1.0-dir/ffmpeg

2.- Update parameters of ffmpeg - Modify code of Kaltura.

The method used to build the string with the parameters that are passed to the ffmpeg is located inside the file: /opt/kaltura/app/infra/cdl/kdl/KDLTranscoderCommand.php
Locate the class KDLTranscoderCommand, and its method FFMpeg() (lines #232 and so on). Here you can see how the command line is built.

There are some of this parameters that, for different reasons, need to be changed (these are guiding number lines, they could not be the same in your case):

line #288:

$cmdStr = $cmdStr." -b ".$this->_vidBr."k";

has to be changed for this:

$cmdStr = $cmdStr." -b:v ".$this->_vidBr."k";

line #326:

case KDLAudioTarget::AAC:
        $acodec = "libfaac";
        break;

needs to be changed for the new codec used by the build of ffmpeg. In my case was "libvo_aacenc", so:

case KDLAudioTarget::AAC:
        $acodec = "libvo_aacenc";
        break;

(If you use another codec like MP3, VORBIS, etc. you should check what codec is using the new version of the ffmpeg and if it's necessary, change the line in its "case" section)

line #346:

if($this->_audBr!==null && $this->_audBr>0){
        $cmdStr = $cmdStr." -ab ".$this->_audBr."k";
}

it's not used anymore. You must change it for this one:

if($this->_audBr!==null && $this->_audBr>0){
        $cmdStr = $cmdStr." -b:a ".$this->_audBr."k";
}

Optionally, you can change how the video codec profile is passed to ffmpeg. In my case, I'm encoding videos with the H.264 baseline profile, but Kaltura writes a command used for Akamai, calling the method "generateH264params()" and putting parameters that I don't use for encoding. I reccomend to take a look at the lines #252 ( "switch($this->_vidId){" ) and so on.

3.- Two Pass

Here comes the tricky part. I've been lost in the code for a couple of days, just looking where Kaltura uses the attribute "_vid2pass" and builds the command line with the parameter "-pass 1". Then relaunch ffmpeg with the same command line and the same parameter "-pass" but this time with the value "2". I didn't find anything, so I gave up. This is what I've made in order to do the two pass encoding with ffmpeg:

Knowing the executable file is in "/opt/kaltura/bin", modify the KDLTranscoderCommand.php file by changing the lines #404 and #405:

if($this->_outFileName)
        $cmdStr = $cmdStr." -y ".$this->_outFileName;

with these lines:

if($this->_vid2pass)
        $cmdStr = $cmdStr." -pass 1";
               
if($this->_outFileName)
        $cmdStr = $cmdStr." -y ".$this->_outFileName;

if($this->_vid2pass){
        $cmdStr2Pass = str_replace("-pass 1", "-pass 2", $cmdStr);
        $cmdStr = $cmdStr." && /opt/kaltura/bin/ffmpeg ".$cmdStr2Pass;
}

I hope these changes help you.

Regards.

Joined: 01/05/2009
Points: 1697

Hey there,

In latest (v6) 2pass was fixed, including added upgrade to ffmpeg 0.10, while also keeping 0.6 for backward compatibility.
Thanks!