Skip to content

Instantly share code, notes, and snippets.

@vluzrmos
Created July 6, 2015 17:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vluzrmos/297e3cca7d412f7959cd to your computer and use it in GitHub Desktop.
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
#!/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