欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

lrc歌词转srt,歌词格式转换

程序员文章站 2022-06-21 15:05:13
...

使用的laravel框架

/**
 * LRC 2 SRT
 */
public function getLrc2srt()
{
    //lrc文件的路径
    $sLrcFile = storage_path() . '/xqxayjr.lrc';
    $sLrcContent = iconv("EUC-CN", 'UTF-8', file_get_contents($sLrcFile));
    if (file_exists($sLrcFile)) {
        $sContent = lrc2srt($sLrcContent);
        if (file_put_contents(storage_path() . '/xqxayjr.srt', $sContent) !== false)
        {
            echo 'ok';
        }
    }
}复制代码

主要方法

public function lrc2srt($lrc) {
    $lrc = explode( "\n", $lrc );
    $srt = "";
    $lines = array();
    foreach ( $lrc as $lrcl ) {
        if ( preg_match( "|\[(\d\d)\:(\d\d)\.(\d\d)\](.+)|", $lrcl, $m ) ) {
            $lines[] = array(
                'time' => "00:{$m[1]}:{$m[2]},{$m[3]}0", // convert to SubRip-style time
                //front 设置字幕颜色
                'lyrics' => '<font color=#FFFFFF>' . trim($m[4]) . '</font>'
            );
        }
    }
    for ( $i = 0; $i < count( $lines ); $i++ ) {
        $n = $i + 1;
        $nexttime = isset( $lines[$n]['time'] ) ? $lines[$n]['time'] : "99:00:00,000";
        $srt .= "$n\n"
            .  "{$lines[$i]['time']} --> {$nexttime}\n"
            .  "{$lines[$i]['lyrics']}\n\n";
    }
    return $srt;
}复制代码