Skip to content

Instantly share code, notes, and snippets.

@andrei-coelho
Last active January 22, 2022 05:59
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 andrei-coelho/d9aa323910a2936e4acc89e5c9517c47 to your computer and use it in GitHub Desktop.
Save andrei-coelho/d9aa323910a2936e4acc89e5c9517c47 to your computer and use it in GitHub Desktop.
Esse é um script para arrumar legendas que não batem com o tempo do filme ou da série
<?php
$subtrair = false; # --> tempo para subtrair quando a legenda está atrasada
$somar = "00:00:08"; # --> tempo para somar quando a legenda está adiantada
$legendaEntrada = "legenda_s01_ep01(errada).srt"; # Nome do Arquivo a ser alterado
$legendaSaida = "legenda_s01_ep01(correta).srt"; # Nome do Arquivo a ser gerado
$arquivo = fopen($legendaEntrada, "r");
$conteudo = "";
if ($arquivo) {
while (($linha = fgets($arquivo, 4096)) !== false) {
if($subtrair) $conteudo .= verificaESubtrai($linha, $subtrair);
if($somar) $conteudo .= verificaESoma($linha, $somar);
}
if (!feof($arquivo)) {
throw new Exception("Erro Inesperado", 1);
}
fclose($arquivo);
file_put_contents($legendaSaida, $conteudo);
exit( "Arquivo gerado com Sucesso!" );
}
throw new Exception("Arquivo não existe", 1);
function verificaESubtrai($linha, $subtrair){
preg_match('/(\d{2}:\d{2}:\d{2}),[\d\s\->]+(\d{2}:\d{2}:\d{2}),\d+[\\n\\r]/', $linha, $saida);
if($saida){
$str = $saida[0];
$tempoIni = $saida[1];
$tempoFim = $saida[2];
$tempoIniArrumado = gmdate('H:i:s', strtotime( $tempoIni ) - strtotime( $subtrair ) );
$tempoFimArrumado = gmdate('H:i:s', strtotime( $tempoFim ) - strtotime( $subtrair ) );
$str = str_replace($tempoIni, $tempoIniArrumado, $str);
$str = str_replace($tempoFim, $tempoFimArrumado, $str);
$linha = $str;
}
return $linha;
}
function verificaESoma($linha, $somar){
preg_match('/(\d{2}:\d{2}:\d{2}),[\d\s\->]+(\d{2}:\d{2}:\d{2}),\d+[\\n\\r]/', $linha, $saida);
if($saida){
$str = $saida[0];
$tempoIni = $saida[1];
$tempoFim = $saida[2];
$partsIni = explode(":", $tempoIni);
$partsFim = explode(":", $tempoFim);
$partsSom = explode(":", $somar);
$temposSomaI = (((int)$partsIni[0] * 60 * 60) + ((int)$partsIni[1] * 60) + (int)$partsIni[2])
+
(((int)$partsSom[0] * 60 * 60) + ((int)$partsSom[1] * 60) + (int)$partsSom[2]);
$temposSomaF = (((int)$partsFim[0] * 60 * 60) + ((int)$partsFim[1] * 60) + (int)$partsFim[2])
+
(((int)$partsSom[0] * 60 * 60) + ((int)$partsSom[1] * 60) + (int)$partsSom[2]);
$tempoIniArrumado = gmdate('H:i:s', $temposSomaI );
$tempoFimArrumado = gmdate('H:i:s', $temposSomaF );
$str = str_replace($tempoIni, $tempoIniArrumado, $str);
$str = str_replace($tempoFim, $tempoFimArrumado, $str);
$linha = $str;
}
return $linha;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment