【发布时间】:2010-06-08 02:11:38
【问题描述】:
我怀疑在 PHP 中处理视频时间码的算法存在逻辑问题。感谢所有帮助。
目标
基本上我想使用时间码并执行计算
对于那些不熟悉时间码的人来说,它看起来像这样
01:10:58:12 或 HH:MM:SS:FF 'AKA' HOURS:MINUTES:SECONDS:FRAMES
我使用了来自 HERE 的脚本来帮助我处理这种格式。
问题
现在我可以说这个脚本有效吗!!!时间码计算(在这种情况下是添加)正在正确执行。然而,这个脚本不断抛出以下错误,但当我尝试进行以下计算时仍会产生正确的输出
00:01:26:00 + 00:02:00:12
这个计算的错误如下所示
遇到 PHP 错误
严重性:通知
消息:未定义的索引:键
文件名:staff/tools.php
行号:169
PHP 错误是 遇到
严重性:通知
消息:未定义的索引:键
文件名:staff/tools.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);
}
我希望这些信息足以让某人帮助我了解这一点,我将非常感激。
谢谢,
提姆
【问题讨论】:
标签: php codeigniter logic