PHP +代码点火器时间码计算逻辑错误
我怀疑我用来处理 PHP 中视频时间码的算法存在逻辑问题。感谢所有帮助。
目标
基本上我想使用时间码并执行计算
对于那些不熟悉时间码的人来说它看起来像这样
01:10:58:12 或 HH:MM:SS:FF 'AKA' HOURS:MINUTES:SECONDS:FRAMES
我使用了来自 此处 帮助我使用此格式。
问题
现在我可以说这个脚本有效吗!!!时间码计算(在本例中为添加)正在正确执行。然而,这个脚本不断抛出以下错误,但当我尝试进行以下计算时仍然产生正确的输出
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);
}
,我希望这对于有人帮助我解决这个问题有足够的信息,我将非常感激。
谢谢,
蒂姆
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
encounteredSeverity: 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
的示例
注意错误通常很小,这是您完成
后将能够抑制这些错误
。您会发现 error_reporting() 函数位于主 index.php 文件的顶部。
The NOTICE Errors are usually minor, Heres an example
of you done
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.