Created
July 6, 2015 17:20
-
-
Save vluzrmos/297e3cca7d412f7959cd to your computer and use it in GitHub Desktop.
Command line helper to convert all .srt files into utf-8. The resultant files will have the extension .utf8.srt
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
#!/usr/bin/env php | |
<?php | |
/** @var string local dir */ | |
$cwd = getcwd(); | |
/** @var array local dir subdirectories */ | |
$dirs = glob($cwd."/*", GLOB_ONLYDIR); | |
/** @var array local dir subtitles */ | |
$files = dirSubtitles($cwd); | |
/* convert srts on root dir */ | |
convertFiles($files); | |
/* convert all files from all subdirs */ | |
foreach($dirs as $dir){ | |
$files = dirSubtitles($dir); | |
convertFiles($files); | |
} | |
/** | |
* Get subtitles for a dir | |
* @param [type] $dir [description] | |
* @return [type] [description] | |
*/ | |
function dirSubtitles($dir){ | |
return glob($dir."/*.srt"); | |
} | |
/** | |
* Convert subtitles | |
* @param [type] $files [description] | |
* @param [type] $ext [description] | |
* @return [type] [description] | |
*/ | |
function convertFiles($files, $ext = ".utf8.srt"){ | |
foreach($files as $file){ | |
$in = file_get_contents($file); | |
$out = iconv('ISO-8859-1','UTF-8//TRANSLIT', $in); | |
file_put_contents(preg_replace('/.srt$/', $ext, $file), $out); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment