Skip to content

Instantly share code, notes, and snippets.

@zhaofengli
Created January 16, 2015 15:21
Show Gist options
  • Save zhaofengli/b1cdb989356a3c1c9581 to your computer and use it in GitHub Desktop.
Save zhaofengli/b1cdb989356a3c1c9581 to your computer and use it in GitHub Desktop.
Quick-and-dirty LRC-to-SubRip converter
<?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