I just found a *.srt
file that mpv
fails to load. So, I thought let's make my own subtitle parser that takes subtitle's path as command-line argument. Here's what I tried :
/* 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;
}
On first attempt, I was using just the dialogue's start time as dialogue's duration, i.e end_time - start_time and that's why, this part was missing :
/* 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));
and variables' names were a bit different and then I realised I was wrong but this is all irrelevant . I am just saying this because till then, code was working just fine (results were unexpected though there was some garbage) but now it just stucks and does nothing. Why is that? Much thanks for your time!
Here's the file I am trying : https://gist.github.com/gaurav712/6646ad7dfd3c487536dce9b0712471e7