This function is a PHP script designed to convert lyric files into SRT subtitle files. The specific implementation process is as follows:
- Remove extra blank lines using a regular expression.
preg_replace("/^\s*$/m", "", $lrc)。 - Split the lyrics text into lines and use it.
explode("\n", $lrc)。 - Iterate over each line of lyrics using a regular expression.
preg_match_all("/\[(\d{2}):(\d{2})(?:\.(\d{2,3}))?\]/", $lines[$i], $matches)Match time labels with lyrics content. - If a time tag is matched, retrieve the start and end times. If the lyrics follow directly after the last time tag, calculate the end time based on the time tag in the next line; otherwise, set the end time to the start time plus 5 seconds.
- Generate SRT subtitle content based on start and end times as well as the song lyrics, then save it.
$srtIn a string. - Return
$srtalphabetic string.
This function is primarily used to convert LRC-format lyrics into SRT-format subtitles, making it easier to display the lyrics within a video player.
Code:
function lrc2srt($lrc) {
// print_r($lrc);
// 去除多余的空白行
$lrc = preg_replace("/^\s*$/m", "", $lrc);
// 按行分割
$lines = explode("\n", $lrc);
$count = count($lines);
// print_r($lines);
$srt = '';
$index = 1;
for ($i = 0; $i < $count; $i++) {
// 匹配时间标签和歌词内容
preg_match_all("/\[(\d{2}):(\d{2})(?:\.(\d{2,3}))?\]/", $lines[$i], $matches);
// print_r($matches);
if (!empty($matches[0])) {
// 获取时间标签
$startHour = intval($matches[1][0]);
$startMinute = intval($matches[2][0]);
$startSecond = intval($matches[3][0]);
// $startTime = sprintf("%02d:%02d:%02d,%03d", 0,$startHour, $startMinute, $startSecond);
$startTime = sprintf('%02d:%02d:%02d,%03d', 0, $startHour, $startMinute, $startSecond * 10);
// 获取歌词内容
if (strpos($lines[$i], ']') === false) {
// 特殊情况:歌词内容直接跟在最后一个时间标签之后的情况
$lyric = trim(substr($lines[$i], strlen($matches[0][0])));
if ($i < $count - 1 && strpos($lines[$i + 1], '[') !== false) {
// 下一行是新的时间标签,计算结束时间
preg_match_all("/\[(\d{2}):(\d{2})(?:\.(\d{2,3}))?\]/", $lines[$i + 1], $nextMatches);
$endHour = intval($nextMatches[1][0]);
$endMinute = intval($nextMatches[2][0]);
$endSecond = intval($nextMatches[3][0]);
} else {
// 下一行无新的时间标签,将结束时间设为开始时间+5秒
$endHour = $startHour;
$endMinute = $startMinute;
$endSecond = $startSecond + 5;
}
} else {
$lyric = trim(preg_replace("/\[(\d{2}):(\d{2})(?:\.(\d{2,3}))?\]/", "", $lines[$i]));
// 获取下一行的时间标签
preg_match_all("/\[(\d{2}):(\d{2})(?:\.(\d{2,3}))?\]/", $lines[$i + 1], $nextMatches);
if (!empty($nextMatches[0])) {
// 计算结束时间
$endHour = intval($nextMatches[1][0]);
$endMinute = intval($nextMatches[2][0]);
$endSecond = intval($nextMatches[3][0]);
} else if ($i !== $count - 1) {
// 下一行无时间标签,将结束时间设为开始时间+5秒
$endHour = $startHour;
$endMinute = $startMinute;
$endSecond = $startSecond + 5;
}
}
// 计算结束时间
// $endTime = sprintf("%02d:%02d:%02d,%03d", 0, $endHour, $endMinute, $endSecond);
$endTime = sprintf('%02d:%02d:%02d,%03d', 0, $endHour, $endMinute, ($endSecond - 5) * 10);
if(!empty($lyric)){
// 构建SRT字幕内容
$srt .= $index . "\n";
$srt .= $startTime . ' --> ' . $endTime . "\n";
$srt .= $lyric . "\n\n";
$index++;
}
}
}
return $srt;
}
}