【问题标题】:Converting Seconds and Duration to SRT (subtitle) time format (PHP)将秒和持续时间转换为 SRT(字幕)时间格式 (PHP)
【发布时间】:2016-10-25 02:32:59
【问题描述】:

我有这样的输入:

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) 吗?

这就是我真正需要的,其余的我可以自己解决。

谢谢,非常感谢。

【问题讨论】:

    标签: php subtitle datetime-conversion srt


    【解决方案1】:
    private function seconds2SRT($seconds)
    {
        $hours = 0;
        $whole = floor($seconds);
        $fraction = $seconds - $whole;
        $milliseconds = number_format($fraction, 3, '.', ',');
        $milliseconds_array = explode('.', strval($milliseconds));
        $milliseconds = $milliseconds_array[1];
    
        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" : '');
    }
    

    【讨论】:

      【解决方案2】:

      您可以像这样创建字幕:

      $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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-11-05
        • 1970-01-01
        • 2017-07-31
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多