O programa utiliza o ajuste passado como parâmetro para
sincronizar a legenda dos filmes. são aceitos valores negativos
e positivos em milisegundos.
exemplo: Se a legenda estiver com atrazo de 1 min e 2,345 s
será utilizado um ajuste de - (1*60*1000) + (2 * 1000) + 345 = -62345
O código pode ser usado, distribuído e melhorado sem nenhuma restrição.
Peço que em caso de meloras, me envie a melhoria para que eu tenha também.
gjr_rj@msn.com
/*
Programa para sincronização de legendas no formato srt.
Por: Geraldo José Ferreira Chagas Júnior
20/07/2008
compilador: gcc - GNU/Linux
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
const int TAMLINHA=256;
long strToMili (char* str);
void miliToStr (long mili, char* str);
int main (int argc, char* argv[])
{
char opc;
char* arqOri;
char* arqDest;
long ajust;
FILE* fOri;
FILE* fDest;
long tamArq;
/* esse jogo com as duas variáveis de percentual servira para não ficar
imprimindo o percentual o tempo todo, caso não tenha mudado*/
int percent;
int percentAnt=-1;
char linha [TAMLINHA];
char temp [20];
char* sub;
int pos;
if (argc != 4)
{
printf ("Os parâmetros são:\n");
printf ("1 - Arquivo a ser convertido\n");
printf ("2 - Ajuste da sincronização em milisegundos\n");
printf ("3 - Arquivo sincronizado\n");
return 0;
}
arqOri = argv[1];
arqDest = argv[3];
ajust = atoi (argv[2]);
if (!(fOri=fopen(arqOri, "r")))
{
printf ("Erro na abertura do arquivo de origem !\n");
return 0;
}
if ((fDest=fopen(arqDest, "r")))
{
close (fDest);
printf ("O aquivo de destino %s já existe. Deseja substitui-lo (s/n) ? ", arqDest);
opc = getchar();
if ((opc!='S') && (opc!='s')) exit (0);
}
if (!(fDest=fopen(arqDest, "w+")))
{
printf ("Erro na abertura do arquivo de destino !\n");
return 0;
}
/* pegando o tamanho do arquivo de origem para imprimir a porcentagem */
fseek (fOri, 0, SEEK_END);
tamArq = ftell (fOri);
/* voltando ao inicio do arquivo */
fseek (fOri, 0, SEEK_SET);
printf ("0 %c convertido.",'%');
while (fgets (linha, TAMLINHA, fOri))
{
if (sub=strstr(linha," --> "))
{
pos = sub - &linha[0];
strcpy (temp, &linha[pos+5]);
miliToStr (strToMili (linha)+ajust, linha);
miliToStr (strToMili (temp)+ajust, temp);
strcat (linha, " --> ");
strcat (linha, temp);
strcat (linha, "\n");
}
fputs (linha, fDest);
percent = ftell (fOri) * 100 / tamArq;
if (percent!=percentAnt)
{
printf ("\r%d %c convertido.",percent,'%');
percentAnt=percent;
}
}
printf("\n");
close (fOri);
close (fDest);
}
long strToMili (char* str)
{
char temp[10];
long h;
long m;
long s;
int i=0;
int j=0;
while (str[i]!='\0')
{
if ((str[i]==' ') || (str[i]==':')) break;
temp[j++]=str[i++];
}
temp[j]='\0';
h=atoi(temp) * 60 * 60 * 1000;
if (str[i]!='\0')
{
i++;
j=0;
}
while (str[i]!='\0')
{
if ((str[i]==' ') || (str[i]==':')) break;
temp[j++]=str[i++];
}
temp[j]='\0';
m=atoi(temp) * 60 * 1000;
if (str[i]!='\0')
{
i++;
j=0;
}
while (str[i]!='\0')
{
if ((str[i]==' ') || (str[i]==':') || (str[i]==',')) break;
temp[j++]=str[i++];
}
temp[j]='\0';
s=atoi(temp) * 1000;
if (str[i]!='\0')
{
i++;
j=0;
}
while (str[i]!='\0')
{
if (str[i]==' ') break;
temp[j++]=str[i++];
}
temp[j]='\0';
return (h + m + s + atoi(temp));
}
void miliToStr (long mili, char* str)
{
char temp[20];
long h;
long m;
long s;
int i;
h = (mili - (mili % 3600000)) / 3600000;
mili = mili-(h*3600000);
m = (mili - (mili % 60000)) / 60000;
mili = mili-(m*60000);
s = (mili - (mili % 1000)) / 1000;
mili = mili-(s*1000);
snprintf(str,20,"%2d:%2d:%2d,%3d", h, m , s, mili);
for (i=0;str[i]!='\0';i++)
{
if (str[0]==' ')
str[0]='0';
else if ((str[i]==' ') && (str[i-1] != '>') && (str[i+1] != '-'))
str[i]='0';
}
}
Nenhum comentário:
Postar um comentário