Adding srt support in php for mencoder

Mysoogal
Captain
Captain
Posts: 223
Joined: Thu Dec 17, 2009 7:15 am
Location: Planet VPS

Re: Adding srt support in php for mencoder

Post by Mysoogal » Sat Dec 19, 2009 2:50 am

would i assume this is the where i could change the size of fonts ?

Code: Select all

-subfont-text-scale 3.3 
i find them to be little large, is -subfont-text-scale 3.0 ok for this ? or 2.9
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

Re: Adding srt support in php for mencoder

Post by Neo » Sat Dec 19, 2009 2:55 am

Yes, you can adjust as you want
Mysoogal
Captain
Captain
Posts: 223
Joined: Thu Dec 17, 2009 7:15 am
Location: Planet VPS

Re: Adding srt support in php for mencoder

Post by Mysoogal » Sat Dec 19, 2009 3:11 am

so here it is for anybody thats interested in encoding video with srt support and sub - idx :D

this code has been tested and working on VPS ubuntu 8.04 bare bone server with apache2,ffmpeg-php,ffmpeg,mencoder,php5,php-cli

Supported encoding
avi : yes
srt : yes
sub : yes
idx : yes
Audio : mp3
thumbnails : yes - could be changed to grab more with mplayer
Video : h264 - mencoder output might not work on psp if your wondering

Code: Select all

<?php

$dir = './'; // set to current folder
if ($handle = opendir($dir)) {
    while(false!== ($file = readdir($handle))) {

        if ( is_file($dir.$file) ){
            if (preg_match("'\.(avi)$'", $file) ){
               $sub_file = str_ireplace(".avi", ".srt", $dir.$file);
               $idx_file = str_ireplace(".avi", ".idx", $dir.$file);
               $thumb_file = str_ireplace(".avi", ".jpg", $dir.$file);
               $out_file = str_ireplace(".avi", ".mp4", $dir.$file);
               flv_convert_get_thumb($dir.$file, $sub_file, $idx_file, $thumb_file, $out_file);
            }
            else{
                continue;
            }
        }
    }
    closedir($handle);
}


//flv_convert_get_thumb('input.avi', 'input.srt', 'output.jpg', 'output.ogm');
// code provided and updated by steve of phpsnaps ! thanks
// accepts:
// 1: the input video file
// 2: path to thumb jpg
// 3: path to transcoded mpeg?
function flv_convert_get_thumb($in, $in_sub, $in_idx, $out_thumb, $out_vid){
    // get thumbnail
    $cmd = 'ffmpeg -v 0 -y -i '.$in.' -vframes 1 -ss 250 -vcodec mjpeg -f rawvideo -s 286x160 -aspect 16:9 '.$out_thumb;
    $res = shell_exec($cmd);
    // $res is the output of the command
    // transcode video
    $cmd = 'mencoder '.$in.' -o '.$out_vid.' -sub '.$in_sub.' -subfont-text-scale 3.0 -subpos 99 -idx '.$in_idx.' -af volume=10 -aspect 16:9 -of avi -noodml -ovc x264 -x264encopts bitrate=500:level_idc=41:bframes=3:frameref=2: nopsnr: nossim: pass=1: threads=auto -oac mp3lame';
    $res = shell_exec($cmd);
}
?>
Thanks for your help to complete this code :D
Mysoogal
Captain
Captain
Posts: 223
Joined: Thu Dec 17, 2009 7:15 am
Location: Planet VPS

Re: Adding srt support in php for mencoder

Post by Mysoogal » Fri Jan 08, 2010 3:16 am

hi neo,

i need your advice with this, how can i delete avi file after encoding completed ? i know how to delete file like this

Code: Select all





<?php

$dir = './'; // set to current folder
if ($handle = opendir($dir)) {
    while(false!== ($file = readdir($handle))) {

        if ( is_file($dir.$file) ){
            if (preg_match("'\.(avi)$'", $file) ){
               $sub_file = str_ireplace(".avi", ".srt", $dir.$file);
               $idx_file = str_ireplace(".avi", ".idx", $dir.$file);
               $thumb_file = str_ireplace(".avi", ".jpg", $dir.$file);
               $out_file = str_ireplace(".avi", ".mp4", $dir.$file);
               flv_convert_get_thumb($dir.$file, $sub_file, $idx_file, $thumb_file, $out_file);
            }
            else{
                continue;
            }
        }
    }
    closedir($handle);
}


//flv_convert_get_thumb('input.avi', 'input.srt', 'output.jpg', 'output.ogm');
// code provided and updated by steve of phpsnaps ! thanks
// accepts:
// 1: the input video file
// 2: path to thumb jpg
// 3: path to transcoded mpeg?
function flv_convert_get_thumb($in, $in_sub, $in_idx, $out_thumb, $out_vid){
    // get thumbnail
    $cmd = 'ffmpeg -v 0 -y -i '.$in.' -vframes 1 -ss 250 -vcodec mjpeg -f rawvideo -s 286x160 -aspect 16:9 '.$out_thumb;
    $res = shell_exec($cmd);
    // $res is the output of the command
    // transcode video
    $cmd = 'mencoder '.$in.' -o '.$out_vid.' -sub '.$in_sub.' -subfont-text-scale 3.0 -subpos 99 -idx '.$in_idx.' -af volume=10 -aspect 16:9 -of avi -noodml -ovc x264 -x264encopts bitrate=500:level_idc=41:bframes=3:frameref=2: nopsnr: nossim: pass=1: threads=auto -oac mp3lame';
    $res = shell_exec($cmd);
}

}
// is there way to wait until encoding completes ? so this code starts ? 
 chdir('$in.avi'); 
    $do = unlink($fileToDel); 
    if($do=="1"){ 
        echo "The file was encoded successfully. <br> removing original file"; 
    } else { echo "There was an error trying to delete the file."; } 

?>

i've tried to add it, does it look ok neo
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

Re: Adding srt support in php for mencoder

Post by Neo » Fri Jan 08, 2010 5:50 am

Few points to note. Here we encode few files, so you need to address that. (You just tried to delete a single file?).

You could try something as below.

Code: Select all

<?php

$dir = './'; // set to current folder
if ($handle = opendir($dir)) {
    while(false!== ($file = readdir($handle))) {

        if ( is_file($dir.$file) ){
            if (preg_match("'\.(avi)$'", $file) ){
               $sub_file = str_ireplace(".avi", ".srt", $dir.$file);
               $idx_file = str_ireplace(".avi", ".idx", $dir.$file);
               $thumb_file = str_ireplace(".avi", ".jpg", $dir.$file);
               $out_file = str_ireplace(".avi", ".mp4", $dir.$file);
               flv_convert_get_thumb($dir.$file, $sub_file, $idx_file, $thumb_file, $out_file);
            }
            else{
                continue;
            }
        }
    }
    closedir($handle);
}

sleep(5); // wait for few seconds until the mencoder.exe releases the final encoded file


if ($handle = opendir($dir)) {
    while(false!== ($file = readdir($handle))) {

        if ( is_file($dir.$file) ){
            if (preg_match("'\.(avi)$'", $file) ){
               $sub_file = str_ireplace(".avi", ".srt", $dir.$file);
               $idx_file = str_ireplace(".avi", ".idx", $dir.$file);

               deleteFileWithRetry($file);
               deleteFileWithRetry($sub_file);
               deleteFileWithRetry($idx_file);
            }
            else{
                continue;
            }
        }
    }
    closedir($handle);
}


function deleteFileWithRetry($file){

    $ret = 0;
    $retry = 5; // we retry to delete a file 5 times
    while ($ret == 0 && $retry){
        $ret = unlink($file);
        
        if ($ret == 0){
            sleep(5); // failed to delete -> wait 5 seconds
        }
        
        $retry--;
    }
    
    if($ret == 1){ 
        echo "The file ". $file . " was encoded successfully. <br> removing original file"; 
    }
    else {
        echo "There was an error trying to delete the file " . $file;
    }    
    
    return ret;
}



//flv_convert_get_thumb('input.avi', 'input.srt', 'output.jpg', 'output.ogm');
// code provided and updated by steve of phpsnaps ! thanks
// accepts:
// 1: the input video file
// 2: path to thumb jpg
// 3: path to transcoded mpeg?
function flv_convert_get_thumb($in, $in_sub, $in_idx, $out_thumb, $out_vid){
    // get thumbnail
    $cmd = 'ffmpeg -v 0 -y -i '.$in.' -vframes 1 -ss 250 -vcodec mjpeg -f rawvideo -s 286x160 -aspect 16:9 '.$out_thumb;
    $res = shell_exec($cmd);
    // $res is the output of the command
    // transcode video
    $cmd = 'mencoder '.$in.' -o '.$out_vid.' -sub '.$in_sub.' -subfont-text-scale 3.0 -subpos 99 -idx '.$in_idx.' -af volume=10 -aspect 16:9 -of avi -noodml -ovc x264 -x264encopts bitrate=500:level_idc=41:bframes=3:frameref=2: nopsnr: nossim: pass=1: threads=auto -oac mp3lame';
    $res = shell_exec($cmd);
}


?>
Mysoogal
Captain
Captain
Posts: 223
Joined: Thu Dec 17, 2009 7:15 am
Location: Planet VPS

Re: Adding srt support in php for mencoder

Post by Mysoogal » Fri Jan 08, 2010 6:20 am

thank you neo, i'm learning many things from robot.lk

i understand little by little i hope someday i can code everything by my self it will be the day i will remember forever :D

from my understanding there is no error echo for idx srt, etc so if exampe.srt or example.idx are not in directory it will through error but that is ok since this page will only be seen by cron job requests i like to think i know what im doing and thinking :mrgreen:

thanks neo, robot.lk is really a great website for php newbi :D


neo can you tell me why i can't direct output to folder like this

Code: Select all

 $cmd = 'mencoder '.$in.' -o './dataStorageflv/.$out_vid.' -sub '.$in_sub.  
i've try to set full path even that doesnt work for output

Code: Select all

 $cmd = 'mencoder '.$in.' -o '/var/www/dataStorageflv/.$out_vid.' -sub '.$in_sub.  
having same issue with ffmpeg sending output jpg to thumbs folder

Code: Select all

$cmd = 'ffmpeg -v 0 -y -i '.$in.' -vframes 1 -ss 250 -vcodec mjpeg -f rawvideo -s 286x160 -aspect 16:9 '/var/www/dataStoragethumbs/.$out_thumb; 

im not sure why it is not outputing file into that folder i try even without / like this
var/www/dataStoragethumbs/$out_thumb
but even that not work my testing server is ubuntu 8.04

thank you for your help
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

Re: Adding srt support in php for mencoder

Post by Neo » Fri Jan 08, 2010 1:04 pm

Syntax error...

Try this one...

Code: Select all


$cmd = 'mencoder '.$in.' -o /dataStorageflv/' . $out_vid . ' -sub '.$in_sub.

$cmd = 'ffmpeg -v 0 -y -i '.$in.' -vframes 1 -ss 250 -vcodec mjpeg -f rawvideo -s 286x160 -aspect 16:9 /var/www/dataStoragethumbs/' . $out_thumb;
 
If you get an error try without starting / as you did before.
Mysoogal
Captain
Captain
Posts: 223
Joined: Thu Dec 17, 2009 7:15 am
Location: Planet VPS

Re: Adding srt support in php for mencoder

Post by Mysoogal » Fri Jan 08, 2010 6:24 pm

thanks output is ok now

i see the problem it should be like this

Code: Select all

 /var/www/dataStorageflv/'.$out_vid.'   
:lol: i get it now
Mysoogal
Captain
Captain
Posts: 223
Joined: Thu Dec 17, 2009 7:15 am
Location: Planet VPS

Re: Adding srt support in php for mencoder

Post by Mysoogal » Wed Jan 13, 2010 12:17 am

hi neo, can you tell me if this is right

i got my ffmpeg rebuilt with libx264 now i can encode to mp4 from ffmpeg i would like to change mencoder to ffmpeg and srt support im not sure what i need to remove so i will try first here :D

is this right ? the flvtool2 just testing to see if it works :D

Code: Select all


ffmpeg -i '.$in.' -f mp4 -vcodec -newsubtitle '.$in_sub.'  mpeg4 -r 25 -b 560000 -s 610x340 -acodec aac -ac 2 -ab 64 -ar 44100 '.$out_vid.' | flvtool2 -U '.$out_vid.'  '.$in.' 

 
this is confusing me, i think i need to use -newsubtitle example.srt what is slang code ?

Code: Select all

3.7 Subtitle options:
`-scodec codec' 
Force subtitle codec ('copy' to copy stream). 
`-newsubtitle' 
Add a new subtitle stream to the current output stream. 
`-slang code' 
Set the ISO 639 language code (3 letters) of the current subtitle stream. 
`-sn' 
Disable subtitle recording. 
`-sbsf bitstream_filter' 
Bitstream filters available are "mov2textsub", "text2movsub". 
ffmpeg -i file.mov -an -vn -sbsf mov2textsub -scodec copy -f rawvideo sub.txt



mencoder

Code: Select all


$cmd = 'mencoder '.$in.' -o '.$out_vid.' -sub '.$in_sub. -subfont-text-scale 3.0 -subpos 99 -af volume=10 -aspect 16:9 -of avi -noodml -ovc x264 -x264encop$


 
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

Re: Adding srt support in php for mencoder

Post by Neo » Wed Jan 13, 2010 12:31 am

For ISO 639 codes, have a look at this link.
For English, this is eng. So you could add the switch as -slang eng

Is it required to call flvtool2 with a pipe? Can we just call it after ffmpeg ?

In mencoder, you miss to put a quote before -subfont.
Post Reply

Return to “PHP & MySQL”