将秒和持续时间转换为SRT(字幕)时间格式(PHP)

时间:2016-10-25 02:33:00

标签: php subtitle datetime-conversion srt

我有这样的输入:

start: 10 | duration: 1 | text: Subtitle Text 1 
start: 15 | duration: 2 | text: Subtitle Text 2 
start: 20 | duration: 3 | text: Subtitle Text 3

这是一个字幕指令集,表示如下:

At 10 second of the video, show "Subtitle Text 1" for 1 seconds
At 15 second of the video, show "Subtitle Text 2" for 2 seconds
At 20 second of the video, show "Subtitle Text 3" for 3 seconds

此输入需要转换为SRT格式,因此它会变成这样:

1
00:00:10,000 --> 00:00:11,000
Subtitle Text 1

2
00:00:15,000 --> 00:00:17,000
Subtitle Text 2

3
00:00:20,000 --> 00:00:23,000
Subtitle Text 3

有人可以告诉我你如何使用PHP将任何给定的秒值转换为SRT格式(00:00:00,000)?

这就是我真正需要的,剩下的我能弄清楚自己。

谢谢,非常感谢。

2 个答案:

答案 0 :(得分:2)

我最终在https://stackoverflow.com/a/4763921/998415

的帮助下弄明白了

这是我自己的基于上述脚本的功能:

function seconds2SRT( $seconds )
{
  $hours = 0;
  $milliseconds = str_replace( "0.", '', $seconds - floor( $seconds ) );

  if ( $seconds > 3600 )
  {
    $hours = floor( $seconds / 3600 );
  }
  $seconds = $seconds % 3600;


  return str_pad( $hours, 2, '0', STR_PAD_LEFT )
       . gmdate( ':i:s', $seconds )
       . ($milliseconds ? ",$milliseconds" : '')
  ;
}

在此演示中查看它:https://eval.in/665896

答案 1 :(得分:0)

你可以像这样创建字幕:

$subtitles = new Subtitles();
$subtitles->add(10, 11, 'Subtitle Text 1');
$subtitles->add(15, 17, 'Subtitle Text 2');
$subtitles->add(20, 23, 'Subtitle Text 3');
echo $subtitles->content('srt');

// or if you need file
$subtitles->save('subtitle-file.srt');

您需要下载此库:https://github.com/mantas-done/subtitles