我有什麼我懷疑是一個邏輯問題,我用一個算法在PHP中使用視頻時間碼。所有的幫助表示讚賞。PHP +代碼點火器時間碼計算邏輯錯誤
的目的
嗯,基本上我想有時間碼工作,並執行計算
對於那些不熟悉的時間碼,它看起來像這樣
1時10分58秒: 12或HH:MM:SS:FF'AKA'HOURS:MINUTES:SECONDS:FRAMES
我已經使用HERE的腳本來幫助我處理這種格式。
的問題
現在我能說的是這個劇本的作品!時間碼計算(在這種情況下是附加)正在正確執行。但是這個劇本不斷地引發以下錯誤,但仍然會產生正確的輸出,當我嘗試做以下計算
00:01:26:00 + 00:02:00:12
從該計算的誤差被示出如下
遇到甲PHP錯誤
嚴重性:注意
消息:未定義指數:關鍵
文件名:管理人員/ tools.php
行號:169
一個PHP錯誤是 遇到
嚴重性:注意
消息:未定義索引:鑰匙
文件名:員工/工具。PHP
行號:169
行號169在parseInput()功能
// 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;
現在我還要提到的是,上述引發錯誤的次數取決於我正在計算什麼
00:00:00:00 + 00:00:00:00
返回沒有錯誤。
01:01:01:01 + 02:02:02:02
產生上述誤差的8。
供您參考,這裏是它的代碼是全部
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);
}
我希望這是一個人足夠的信息來幫助我在此之上,我將非常感激。
感謝,
添
非常感謝:) – Tim 2010-06-08 02:42:10