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