Я только что нашел файл *.srt
, который mpv
не может быть загружен.Итак, я подумал, давайте создадим свой собственный анализатор субтитров, который принимает путь субтитров в качестве аргумента командной строки.Вот что я попробовал:
/* Intended to be a program for parsing *.srt subtitles as an alternative to video players' */
#include <ncurses.h>
#include <unistd.h>
#define SEC_IN_MIN 60
#define MIN_IN_HR 60
long get_duration(FILE *fp); // to get the duration of a dialogue in seconds
long turn_to_sec(int hours, int minutes, int seconds); // returns sum of hours and minutes, all in seconds
int main(int argc, char **argv)
{
FILE *fp;
long sec;
char ch;
if(argv[1] == NULL)
{
printf("Please enter a filename!\n");
return 1;
}
printf("Trying to open specified file %s\n",argv[1]);
fp = fopen(argv[1],"r");
if(fp == NULL)
{
printf("Error while opening file %s\n",argv[1]);
return 1;
}
initscr(); // initialise nCurses window
ch = getc(fp);
while(ch != EOF)
{
clear();
sec = get_duration(fp);
while(1)
{
if((ch = getc(fp)) == '\n')
{
if((ch = getc(fp)) == '\n' || ch == EOF)
break;
else
addch(ch);
}
addch(ch);
}
refresh();
sleep(sec);
}
endwin(); // close nCurses
fclose(fp); // close the file
return 0;
}
long get_duration(FILE *fp)
{
long duration = 0;
char ch;
short hour_start = 0, hour_end = 0, minute_start = 0, minute_end = 0, second_start = 0, second_end = 0;
short count=0;
/* just to get to the point where time-specs of the dialogue start */
while((ch = getc(fp)) != '\n');
/* extract characters until ':' to get hour_start */
while((ch = getc(fp)) != 58)
{
hour_start += ch;
count++;
}
hour_start -= (hour_start/(49*count));
/* extract characters until ':' to get minute_start */
count = 0;
while((ch = getc(fp)) != 58)
{
minute_start += ch;
count++;
}
minute_start -= (minute_start/(49*count));
/* extract characters until ',' to get second_start */
count = 0;
while((ch = getc(fp)) != 44)
{
second_start += ch;
count++;
}
second_start -= (second_start/(49*count));
/* now, see if you can find a '>' */
while((ch = getc(fp)) != 62);
ch = getc(fp); // to get rid of that space after "-->"
/* extract characters until ':' to get hour_end */
while((ch = getc(fp)) != 58)
{
hour_end += ch;
count++;
}
hour_end -= (hour_end/(49*count));
/* extract characters until ':' to get minute_end */
count = 0;
while((ch = getc(fp)) != 58)
{
minute_end += ch;
count++;
}
minute_end -= (minute_end/(49*count));
/* extract characters until ',' to get second_end */;
count = 0;
while((ch = getc(fp)) != 44)
{
second_end += ch;
count++;
}
second_end -= (second_end/(49*count));
/* finally, gonna get those values */
second_end -= second_start;
minute_end -= minute_start;
hour_end -= hour_start;
duration += (turn_to_sec(hour_end, minute_end, second_end));
/* positioning the fp to the right position just to keep the 'main()' :) */
while((ch = getc(fp)) != '\n' || ch != EOF);
return duration;
}
long turn_to_sec(int hours, int minutes, int seconds)
{
long temp;
/* manipulating hours */
temp = hours;
temp *= MIN_IN_HR;
temp *= SEC_IN_MIN;
seconds += temp;
/* manipulating minutes */
temp = minutes;
temp *= SEC_IN_MIN;
seconds += temp;
return seconds;
}
При первой попытке я использовал время начала диалога в качестве длительности диалога , то есть end_time - start_time и вот почему эта часть отсутствовала:
/* extract characters until ':' to get hour_end */
while((ch = getc(fp)) != 58)
{
hour_end += ch;
count++;
}
hour_end = (hour_end/(49*count));
/* extract characters until ':' to get minute_end */
count = 0;
while((ch = getc(fp)) != 58)
{
minute_end += ch;
count++;
}
minute_end = (minute_end/(49*count));
/* extract characters until ',' to get second_end */
count = 0;
while((ch = getc(fp)) != 44)
{
second_end += ch;
count++;
}
second_end = (second_end/(49*count));
и имена переменных были немного другими, и затем я понял, что был неправ, но это все не имеет значения.Я просто говорю это, потому что до того времени код работал просто отлично (результаты были неожиданными, хотя был какой-то мусор), но теперь он просто зависает и ничего не делает.Это почему?Большое спасибо за ваше время!
Вот файл, который я пробую: https://gist.github.com/gaurav712/6646ad7dfd3c487536dce9b0712471e7