help with php, I'm confused here

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

help with php, I'm confused here

Post by Mysoogal » Sun Apr 18, 2010 2:35 am

hi Neo, can you help me with my latest project to kill you-tube :mrgreen:

my problems are as follows

1. how to start a external php script from a button then have that button gray out like disabled for some time ?
2. how to show a nice error msg when file is not in folder

this is the code i use to show, the video encoding progress, but when there is no file in folder example output.ogv it shows a big error how would user, sorry no video inside folder right now type of msg

Code: Select all

<?php


$filename = '../Processed/output.ogv';
echo $filename . ': ' . filesize($filename) . ' bytes';

?>
    <?php
the error, i think i need to add some type of error checking ? but how would i do that, i mean right now the file encoding is done with a static input.avi here i'm feeling confused do i need to change the encoding script into dynamic input ?

Image

ok that is for the error, now I'm having issue about the mkvmerge usage I'm not sure why its not working i really believe nothing seems wrong in this script

Code: Select all

<?php

$dir = './files'; // 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, $out_file);
            }
            else{
                continue;
            }
        }
    }
    closedir($handle);
}


 
function flv_convert_get_thumb($in, $sub_file, $in_idx, $out_vid){
    
  

    $cmd = 'D:\server\encoder\mkvmerge.exe -o ../Processed/'.$out_vid.' '.$sub_file.' ./files/'.$in.' '; 
    $res = shell_exec($cmd);
}
?>
ok for the last part, this is the static video encoding script using theora cli , the problem with this when i try to move the code below into the dynamic input script you help me with before it doesn't seem to work, I'm really confused here

Code: Select all

 
$cmd = 'D:\server\encoder\ffmpeg2theora-0.26.exe ../Processed/'.$in.' -o ../Processed/'.$out_vid.' --videoquality 4 --audioquality 5 --keyint 60  --framerate 26 --croptop 0 --cropbottom 0 --cropleft 0 --cropright 0 --width 720 --height 404 --title "provided by" --artist "mysoogals" --date "2010-4-14"';
    $res = shell_exec($cmd);
 
you see i'm already confused here, i think my issues are, need to add some type of error check for the folder thing when file is not there,

next I change the static Theora script to dynamic input something like that i think :geek:

Code: Select all

<?php


flv_convert_get_thumb('../files/*.avi', '../Processed/output.jpg', '../Processed/output.ogv');
 
function flv_convert_get_thumb($in, $out_thumb, $out_vid)
{
  
  $cmd = 'D:\server\encoder\ffmpeg.exe -v 0 -y -i ../Processed/'.$in.' -vframes 1 -ss 500 -vcodec mjpeg -f rawvideo -s 286x160 -aspect 16:9 ../Processed/'.$out_thumb;
  $res = shell_exec($cmd);

    $cmd = 'D:\server\encoder\ffmpeg2theora-0.26.exe ../Processed/'.$in.' -o ../Processed/'.$out_vid.' --videoquality 4 --audioquality 5 --keyint 60  --framerate 26 --croptop 0 --cropbottom 0 --cropleft 0 --cropright 0 --width 720 --height 404 --title "provided by" --artist "mysoogals" --date "2010-4-14"';
    $res = shell_exec($cmd);
  
 
}
?>
here are the files i'm working with, if you have time please check them out http://www.mediafire.com/?igtkmjmln34

there are only 4 php files with few lines of code,
Mysoogal
Captain
Captain
Posts: 223
Joined: Thu Dec 17, 2009 7:15 am
Location: Planet VPS

Re: help with php, I'm confused here

Post by Mysoogal » Sun Apr 18, 2010 2:53 am

ok it seems i figured out the mkvmerge with php on windows :D so dont worry about that :geek:

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", ".mkv", $dir.$file);
               flv_convert_get_thumb($dir.$file, $sub_file, $idx_file, $out_file);
            }
            else{
                continue;
            }
        }
    }
    closedir($handle);
}


 
function flv_convert_get_thumb($in, $sub_file, $in_idx, $out_vid){
    
  

    $cmd = 'D:\server\encoder\mkvmerge.exe -o '.$out_vid.' '.$in.' '.$sub_file.' '; 
    $res = shell_exec($cmd);
}
?>


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

Re: help with php, I'm confused here

Post by Neo » Sun Apr 18, 2010 7:03 pm

Hello Mysoogal, How are you my friend, after a long time...

So the remaining question is to run a php script on a button click, right?

On your page, you can write the code for button as below (We use $_SERVER['PHP_SELF'] since we are going to write our function in php on the same page).

Code: Select all

<form action="<?=$_SERVER['PHP_SELF'];?>" method="post">
<input type="button" name="submit" value="Submit">
</form>
Now write the following code at the top of your page.

Code: Select all

<?php
if(isset($_POST['submit'])) {
      myFunction();
}
?>
Since php is a server side script, we need to somehow get the function executed in the server. Hope you get the logic.

Also, if you need to know how to call a function written in JavaScript on pressing a button, see How to Call a JavaScript function on button click.
Mysoogal
Captain
Captain
Posts: 223
Joined: Thu Dec 17, 2009 7:15 am
Location: Planet VPS

Re: help with php, I'm confused here

Post by Mysoogal » Sun Apr 18, 2010 8:38 pm

thanks, good starting point i will learn from this :mrgreen:

i'm also reading about web services and xml rpc :mrgreen: maybe i will get all of this automated oneday.
Mysoogal
Captain
Captain
Posts: 223
Joined: Thu Dec 17, 2009 7:15 am
Location: Planet VPS

Re: help with php, I'm confused here

Post by Mysoogal » Mon Apr 19, 2010 3:51 am

hi neo, what is wrong with this code it should be working :cry: i try to list directory and also list the size to show progress of encoding but it just shows double $file

the error

Code: Select all

 ./files/Possessed.II.(1984).DVDRip.XviD-MDCOrient.avi ../files . ': ' . filesize(../files) . ' bytes'.
./files/Possessed.II.(1984).DVDRip.XviD-MDCOrient.srt ../files . ': ' . filesize(../files) . ' bytes'.
 

Code: Select all

<?php

//define the path as relative
$path = "../files";
$filename = '../files';


//using the opendir function
$dir_handle = @opendir($path) or die("Unable to open $path");

echo " ";

while ($file = readdir($dir_handle)) 
{
   if($file!="$file.avi" && $file!="$file.srt")

if($file!="." && $file!="..")

 if($file != "encode_to_mkv.php" && $file != "encode_to_theora.php") 
    
      echo "./files/$file $filename . ': ' . filesize($filename) . ' bytes'</a>.<br>";
}


//closing the directory
closedir($dir_handle);

?> 

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

Re: help with php, I'm confused here

Post by Neo » Mon Apr 19, 2010 7:25 am

Try,

Code: Select all

echo "./files/$file $filename" . ': ' . filesize($filename) . " bytes </a><br>"; 
Mysoogal
Captain
Captain
Posts: 223
Joined: Thu Dec 17, 2009 7:15 am
Location: Planet VPS

Re: help with php, I'm confused here

Post by Mysoogal » Mon Apr 19, 2010 8:17 am

Neo wrote:Try,

Code: Select all

echo "./files/$file $filename" . ': ' . filesize($filename) . " bytes </a><br>"; 
thanks, but it seems I've come to this conclusion a while ago, I'm not sure why its not reporting the right size it gives me the info below , any idea if its actually checking the size of the files or folder size ?

Image

i also have this very annoying thing, why does it not work like this

Code: Select all

<?php

    $dir = './files'; // 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", ".ogv", $dir.$file);
                   flv_convert_get_thumb($dir.$file, $sub_file, $idx_file, $thumb_file, $out_file);
                }
                else{
                    continue;
                }
            }
        }
        closedir($handle);
    }


     
    function flv_convert_get_thumb($in, $in_sub, $in_idx, $out_thumb, $out_vid){
        
        $cmd = 'D:\server\encoder\ffmpeg2theora-0.26.exe '.$in.'  --subtitles '.$sub_file.' --subtitles-language en -o '.$out_vid.' --videoquality 10 --audioquality 5 --keyint 60  --framerate 26 --croptop 0 --cropbottom 0 --cropleft 0 --cropright 0 --width 720 --height 404 --title "'.$in.'" --artist "" --date "2010-4-14"';
    $res = shell_exec($cmd);
    }

    ?>
i try to change it so many times its driving me insane, i change this

Code: Select all

  $dir = './files'; // set to current folder    

to

Code: Select all

$dir = '../files'; // set to current folder    
still nothing

so i change it to full path

Code: Select all

$dir = 'D:\server\xampp\htdocs\files'; // set to current folder    
still not pickup files to encode from that folder,

on my htdoc, root i have directory structure like this

htdoc ------ root
files ----- PHP encoding Profiles,mp4,theora,mpeg2,xvid etc the problem I'm having is ../files not picking up files to encode in the above theora script I've tried to clean up some folder structure

status - video encoding progress info and other hard-disk info

sorry to be asking to much from you neo, this is driving me crazy i do read the php manual here http://php.net/manual/en/function.filesize.php

Code: Select all

<?php

// outputs e.g.  somefile.txt: 1024 bytes

$filename = 'somefile.txt';
echo $filename . ': ' . filesize($filename) . ' bytes';

?>
i know this works but i don't know how to change it so it becomes dynamic to show list of files with file size as in the earlier script above i tried to change by my self i just don't get this thing :cry:

i hope you can clear this for me, i mean explain where I'm going wrong here what i need to do

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

Re: help with php, I'm confused here

Post by Neo » Wed Apr 21, 2010 5:56 am

Sorry, I was extremely busy with office work these days. Find time quite hard.

It seems like a confusion in paths to me.

I'll help you in making the relative path, if you can tell me the absolute path to both php file and the media file.
(Absolute path is something like D:\abc\xyz\yourfile.php)
User avatar
Saman
Lieutenant Colonel
Lieutenant Colonel
Posts: 828
Joined: Fri Jul 31, 2009 10:32 pm
Location: Mount Lavinia

Re: help with php, I'm confused here

Post by Saman » Wed Apr 21, 2010 6:13 am

For some information about Relative and Absolute urls, see What are Relative and Absolute urls (paths).
Mysoogal
Captain
Captain
Posts: 223
Joined: Thu Dec 17, 2009 7:15 am
Location: Planet VPS

Re: help with php, I'm confused here

Post by Mysoogal » Thu Apr 22, 2010 12:00 am

Neo wrote:Sorry, I was extremely busy with office work these days. Find time quite hard.

It seems like a confusion in paths to me.

I'll help you in making the relative path, if you can tell me the absolute path to both php file and the media file.
(Absolute path is something like D:\abc\xyz\yourfile.php)

sorry to bother you guys, but i fixed this issue right now, even thu changing the paths to ../../files or full path to files example D:\server\xampp\htdocs\files, i changed the code back to ./files and added the file encode_to_etc.php into the same directory as avi files and now i'm using a genuis jquery code to pull the external php functions, here is the example script i made with lots of errors but got it working after whilee :mrgreen:

Code: Select all

<?

//define the path as relative
$path = "../files";
 

//using the opendir function
$dir_handle = @opendir($path) or die("Unable to open $path");

echo " ";

while ($file = readdir($dir_handle)) 
{
   if($file!="$file.avi" && $file!="$file.srt")

if($file!="." && $file!="..")
if($file != "encode_to_mpeg2.php") 
if($file != "encode_to_mkv.php") 
if($file != "encode_to_theora.php") 
if($file != "encode_to_svcd.php")
if($file != "encode_to_thumbsnailer.php")
if($file != "encode_to_wmv.php")
if($file != "encode_to_dirac.php")
if($file != "encode_to_xvid.php")
if($file != "divx2pass.log")
if($file != "encode_to_dvd.php")
if($file != "encode_to_dv.php")
if($file != "encode_to_psp.php")
if($file != "progress.php")
if($file != "remote_uploader.php")
 

 


      echo "$file</a><br/>";
}


//closing the directory
closedir($dir_handle);

?> 
you now i can call this script from processing.php page where i fire this script via mootools

example,

Code: Select all

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script> 
 

   <script> 
var auto_refresh = setInterval(
function()
{
$('#loaddiv').fadeOut('slow').load('status/progressive.php').fadeIn("slow");
}, 2000);
</script>


 <script type="text/javascript" language="javascript">
   $(document).ready(function() {
      /* Global variable */
      var count = 0;


      $("#driver").click(function(event){
          $('#driver').load('files/encode_to_mkv.php');
      });
      /* Gets called when request starts */

  $("#drivers").click(function(event){
          $('#stage2').load('files/encode_to_mpeg2.php');
      });
      /* Gets called when request starts */

  $("#driverss").click(function(event){
          $('#stage3').load('files/encode_to_theora.php');
      });
      /* Gets called when request starts */


  $("#driverssss").click(function(event){
          $('#stage5').load('files/encode_to_thumbsnailer.php');
      });
      /* Gets called when request starts */


  $("#driversssss").click(function(event){
          $('#stage6').load('files/encode_to_wmv.php');
      });
      /* Gets called when request starts */

  $("#driverssssss").click(function(event){
          $('#stage7').load('files/encode_to_dirac.php');
      });
      /* Gets called when request starts */


 $("#SVCD").click(function(event){
          $('#stage8').load('files/encode_to_svcd.php');
      });
      /* Gets called when request starts */

 $("#xvidi").click(function(event){
          $('#stage9').load('files/encode_to_xvid.php');
      });
      /* Gets called when request starts */

$("#dvd").click(function(event){
          $('#stage9').load('files/encode_to_dvd.php');
      });
      /* Gets called when request starts */

$("#dv").click(function(event){
          $('#stage9').load('files/encode_to_dv.php');
      });
      /* Gets called when request starts */

$("#psp").click(function(event){
          $('#stage10').load('files/encode_to_psp.php');
      });
      /* Gets called when request starts */

     
   });
   </script>  

Code: Select all

<div id="footer">

<!-- Encoding Block Start -->
 
   <input type="button" id="driver" value="MKV" />
 
   <input type="button" id="drivers" value="MPEG2 ntsc-vcd VCD" />

  <input type="button" id="SVCD" value="MPEG2 ntsc-svcd SVCD" />

   <input type="button" id="driverss" value="Theora" />

   <input type="button" id="driversssss" value="WMV2" />

   <input type="button" id="driverssssss" value="Dirac" />

   <input type="button" id="xvidi" value="Xvid" />

   <input type="button" id="dvd" value="DVD MPEG" />

    <input type="button" id="dv" value="DV" />

  <input type="button" id="psp" value="PSP" />

   <input type="button" id="driverssss" value="Thumbnails" />

<!-- Encoding block end ! -->
  


</div>

so now when i press example

Code: Select all

   <input type="button" id="driverssss" value="Thumbnails" />
it fires the div

Code: Select all

id="driverssss"



and here how it looks like, Image

so right now my only issue is the filesize :geek: I've been trying to figure it out for 3 days now I've been on freenode irc, #php no helpers just bunch or weirdos so rude :(


just few more questions, how would i expand to include more inputs,

Code: Select all

<?php

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

            if ( is_file($dir.$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", ".drc", $dir.$file);
                   flv_convert_get_thumb($dir.$file, $sub_file, $idx_file, $thumb_file, $out_file);
                }
                else{
                    continue;
                }
            }
        }
        closedir($handle);
    }


    
    function flv_convert_get_thumb($in, $in_sub, $in_idx, $out_thumb, $out_vid){
        
        $cmd = 'D:\server\encoder\ffmpeg.exe -i '.$in.' -vcodec libschroedinger '.$out_vid.'';
        $res = shell_exec($cmd);
    }
    ?>
i know the part of the thumbnail is missing i taken that part out as i only want to encode video :D

can i add more inputs to accept i have different videos container in the folder, such as mpg,dv,ogv,mkv,wmv,ogm,nsv

can i do this ?

Code: Select all

 if (preg_match("'\.(avi)$' ' \.(ogv)$' ' ' \.(mkv)$' '  ' \.(wmv)$' '", $file) ){  
as to add more container support ? :roll:

thanks for your help
Post Reply

Return to “PHP & MySQL”