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

使用的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;
}复制代码


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值