Created
January 16, 2015 15:21
-
-
Save zhaofengli/b1cdb989356a3c1c9581 to your computer and use it in GitHub Desktop.
Quick-and-dirty LRC-to-SubRip converter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Okay, the music player in CM12 only supports srt lyrics files (they must have watched too many films), so... | |
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 | |
'lyrics' => trim( $m[4] ) | |
); | |
} | |
} | |
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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment