Skip to content

Instantly share code, notes, and snippets.

@barlino
Created June 21, 2012 07:21
Show Gist options
  • Save barlino/2964376 to your computer and use it in GitHub Desktop.
Save barlino/2964376 to your computer and use it in GitHub Desktop.
divide the lyrics into sync and unsync lyrics
<?php
/**
目的:分流歌詞成同步歌詞與非同步歌詞。
step1 將文件內容轉換為UTF-8編碼
step2 針對文件內容每一行,判斷是否存在timecode,若有則為同步歌詞,反之為非同步
step3 若unsync長度少於十行,應從sync中過濾出非同步歌詞
step4 儲存
*/
require_once("encoding.php");
$list = array("640708.txt",
"640709.txt",
"640999.txt");
foreach ($list as $lyrics) {
//step1
$content = file_get_contents($lyrics);
$encoding = getFileEncoding($content);
$utf8 = iconv($encoding, "UTF-8", $content);
//step2
$lines = explode("\r\n", $utf8);
$sync = array();
$sync_without_timecode = array();
$unsync = array();
$remainder = array();
$regexp = "/([[0-9a-z]+:[^]]+])/";
foreach ($lines as $line) {
if (preg_match($regexp, $line)) {
$sync []= $line;
$sync_without_timecode []= preg_replace($regexp, "", $line);
} else {
$remainder []= $line;
}
}
//step3
if (count($remainder) <= 10) {
$unsync = $sync_without_timecode;
} else {
$unsync = $remainder;
}
//step4
file_put_contents(basename($lyrics) . "_sync.lrc", implode("\r\n", $sync));
file_put_contents(basename($lyrics) . "_unsync.lrc", implode("\r\n", $unsync));
echo "\n[SYNC] => \n" . file_get_contents(basename($lyrics) . "_sync.lrc");
echo "\n[UNSYNC] => \n" . file_get_contents(basename($lyrics) . "_unsync.lrc");
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment