views:

32

answers:

1

Hello everyone, I have what I suspect to be a logic problem with an algorithm I am using to work with Video timecode in PHP. All help is appreciated.

The Objective

Well basically I want to work with timecode and perform calculations

For those not familiar with timecode it looks like this

01:10:58:12 or HH:MM:SS:FF 'AKA' HOURS:MINUTES:SECONDS:FRAMES

I have used the script from HERE to help me with working with this format.

The Problem

Now can i just say that this script works!!! Timecode calculations (in this case additions) are being performed correctly. However this script continually throws the following errors, yet still produces the correct output when I try and do the following calculation

00:01:26:00 + 00:02:00:12

The errors from this calculation are shown below

A PHP Error was encountered

Severity: Notice

Message: Undefined index: key

Filename: staff/tools.php

Line Number: 169

A PHP Error was encountered

Severity: Notice

Message: Undefined index: key

Filename: staff/tools.php

Line Number: 169

Line Number 169 is in the parseInput() function

// feed it into the tc array
$i=0;
foreach ($tc AS $key=>$value) {
    if ( is_numeric($array["$i"]) ) {
        $tc["$key"]= $array["$i"];
        if ($tc["$key"] < 10 && $tc["$key"] > 0 && strlen($tc['key'])==1 ) $tc["$key"]= "0".$tc["$key"];
        }
    $i++;
    }

return $tc;

Now I should also mention that the number of times the above error is thrown depends on what I am calculating

00:00:00:00 + 00:00:00:00

returns no errors.

01:01:01:01 + 02:02:02:02

produces 8 of the above errors.


For your reference, here is the code in it's entirety

function add_cue_sheet_clips_process()
{

$sheetID = $_POST['sheet_id'];
$clipName = $_POST['clip_name'];
$tcIn = $_POST['tc_in'];
$tcOut = $_POST['tc_out'];

// string $input
// returns an associative array of hours, minutes, seconds, and frames
//
function parseInput ($input) {
// timecode should look something like hh:mm:ss;ff
// allowed separators are : ; . ,
// values may be single or double digits
// hours are least-significant -- 5.4 == 00:00:05;04
$tc= array("frames"=>"00", "seconds"=>"00", "minutes"=>"00", "hours"=>"00");
$punct= array(":", ";", ".", ",");

// too big? too small?
$input= trim($input);
if (strlen($input)>11 || $input=="") {
    // invalid input, too long -- bzzt
    return $tc;
    }

// normalize punctuation
$input= str_replace( $punct, ":", $input);

// blow it up and reverse it so frames come first
$array= explode(":", $input);
$array= array_reverse($array);

// feed it into the tc array
$i=0;
foreach ($tc AS $key=>$value) {
    if ( is_numeric($array["$i"]) ) {
        $tc["$key"]= $array["$i"];
        if ($tc["$key"] < 10 && $tc["$key"] > 0 && strlen($tc['key'])==1 ) $tc["$key"]= "0".$tc["$key"];
        }
    $i++;
    }

return $tc;
}

// array $tc
// returns a float number of seconds
//
function tcToSec($tc) {
    $wholeseconds= ($tc['hours']*3600) + ($tc['minutes'] * 60) + ($tc['seconds']);
    $partseconds= ( $tc['frames']  / 25 );
    $seconds= $wholeseconds + $partseconds;
    return $seconds;
    }

// float $seconds
// bool $subtract
// returns a timecode array
//
function secToTc ($seconds=0, $subtract=0) {
    $tc= array("frames"=>"00", "seconds"=>"00", "minutes"=>"00", "hours"=>"00");

    $partseconds= fmod($seconds, 1);
    $wholeseconds= $seconds - $partseconds;

    // frames
    if ($subtract==1) $tc['frames']= floor( $partseconds * 25 );
    else $tc['frames']= floor( $partseconds * 25 );

    // hours
    $tc['hours']= floor( $wholeseconds / 3600 );
    $minsec= ($wholeseconds - ($tc['hours'] * 3600));

    // minutes
    $tc['minutes']= floor( $minsec / 60 );

    // seconds
    $tc['seconds']= ( $minsec - ($tc['minutes'] * 60) );

    // padding
    foreach ( $tc AS $key=>$value ) {
        if ($value > 0 && $value < 10) $tc["$key"]= "0".$value;
        if ($value=="0") $tc["$key"]= "00";
        }
    return $tc;
    }

// array $tc
// returns string of well-formed timecode
//
function tcToString (&$tc) {
    return $tc['hours'].":".$tc['minutes'].":".$tc['seconds'].";".$tc['frames'];
    }


$timecodeIN = parseInput($tcIn);
$timecodeOUT = parseInput($tcOut); 

// normalized inputs...
$tc1 = tcToString($timecodeIN);
$tc2 = tcToString($timecodeOUT);

// get seconds
$seconds1 = tcToSec($timecodeIN);
$seconds2 = tcToSec($timecodeOUT);

$result = $seconds1 + $seconds2;

$timecode3 = secToTc($result, 0);
$timecodeDUR = tcToString($timecode3);

$clipArray = array('clip_name' => $clipName, 'tc_in' => $tcIn, 'tc_out' => $tcOut, 'tc_duration' => $timecodeDUR);

$this->db->insert('tools_cue_sheets_clips', $clipArray);

redirect('staff/tools/add_cue_sheet_clips/'.$sheetID);
}

I hope this is enough information for someone to help me get on top of this, I would be extremely greatful.

Thanks,

Tim

+1  A: 

The NOTICE Errors are usually minor, Heres an example

if($unamedVaraible){/.../} //Notince: undefined variable

if(isset($unamedVaraible)){/..../} //no error as its checking correctly for the purpose

of you done

error_reporting(E_ALL ^ E_NOTICE); ///Show all errors but E_NOTICE

you will be able to supress these errors.

You'll find the error_reporting() function located at the top of your main index.php file.

RobertPitt
Thank you very much :)
Tim