main.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. Copyright (C) 2014 Olivier Marty <olivier.marty.m at gmail.com>
  3. This program is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU General Public License
  5. as published by the Free Software Foundation; either version 2
  6. of the License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  14. */
  15. #include "parser.h"
  16. #include "time.h"
  17. #include "printer.h"
  18. #include <stdlib.h>
  19. #include <unistd.h>
  20. #include <string.h>
  21. void displayUsage(char *name)
  22. {
  23. printf("Usage : %s file.srt\n", name);
  24. printf("Options :\n");
  25. printf(" -s sec\t: skip the first x seconds\n");
  26. printf(" -d sec\t: wait x seconds before starting (default : 5)\n");
  27. printf(" -t x\t\t: time factor x1000\n");
  28. printf(" -m px\t\t: margin with the bottom of the screen\n");
  29. printf(" -p px\t\t: padding of the box\n");
  30. printf(" -g px\t\t: gap between two lines\n");
  31. printf(" -f fontname\t: name of the font to use\n");
  32. printf(" -i fontname\t: name of the italic font to use\n");
  33. printf(" -b fontname\t: name of the bold font to use\n");
  34. printf(" -j fontname\t: name of the bold and italic font to use\n");
  35. printf(" -h\t\t: display this help and exit\n");
  36. }
  37. int main(int argc, char **argv)
  38. {
  39. int i, shift = 0, delay = 5, margin_bottom = 50, padding = 5, gap = 5;
  40. float factor = 1.0;
  41. char *font = NULL, *font_i = NULL, *font_b = NULL, *font_bi = NULL;
  42. FILE *f = NULL;
  43. // parse arguments
  44. int c;
  45. while((c = getopt (argc, argv, "s:d:t:m:p:g:f:i:b:j:h")) != -1)
  46. switch(c)
  47. {
  48. case 's':
  49. shift = atoi(optarg);
  50. break;
  51. case 'd':
  52. delay = atoi(optarg);
  53. break;
  54. case 't':
  55. factor = (float)atoi(optarg)/1000;
  56. break;
  57. case 'm':
  58. margin_bottom = atoi(optarg);
  59. break;
  60. case 'p':
  61. padding = atoi(optarg);
  62. break;
  63. case 'g':
  64. gap = atoi(optarg);
  65. break;
  66. case 'f':
  67. font = malloc(strlen(optarg)+1);
  68. strcpy(font, optarg);
  69. break;
  70. case 'i':
  71. font_i = malloc(strlen(optarg)+1);
  72. strcpy(font_i, optarg);
  73. break;
  74. case 'b':
  75. font_b = malloc(strlen(optarg)+1);
  76. strcpy(font_b, optarg);
  77. break;
  78. case 'j':
  79. font_bi = malloc(strlen(optarg)+1);
  80. strcpy(font_bi, optarg);
  81. break;
  82. case 'h':
  83. displayUsage(argv[0]);
  84. return 0;
  85. //case '?':
  86. default:
  87. return 1;
  88. }
  89. if(optind >= argc)
  90. {
  91. fprintf(stderr, "Missing filename.\n");
  92. displayUsage(argv[0]);
  93. return 1;
  94. }
  95. f = fopen(argv[optind], "r");
  96. if(f == NULL)
  97. {
  98. perror("fopen()");
  99. exit(1);
  100. }
  101. // open the window
  102. struct printerEnv penv = printerOpenWindow(font, font_i, font_b, font_bi);
  103. // set attributes
  104. penv.margin_bottom = margin_bottom;
  105. penv.padding = padding;
  106. penv.gap = gap;
  107. // show a counter before start the clock
  108. for(i = delay; i > 0; i--)
  109. {
  110. char t[16];
  111. sprintf(t, "%d...\n", i);
  112. printf("%s", t);
  113. printerShow(&penv, t, T_ITALIC);
  114. sleep(1);
  115. }
  116. printf("0 !\n");
  117. printerClean(penv);
  118. timeInitialize(-factor*shift);
  119. struct SubtitleLine sline;
  120. int id = 0;
  121. while(!feof(f))
  122. {
  123. id = next(f, id+1, &sline);
  124. if(timeInFuture(timeFactor(sline.end, factor)))
  125. {
  126. timeSleepUntil(timeFactor(sline.begin, factor));
  127. printf("%ds\n", sline.begin.tv_sec);
  128. // show
  129. printf("%s\n", sline.text);
  130. printerShow(&penv, sline.text, 0);
  131. // hide
  132. timeSleepUntil(timeFactor(sline.end, factor));
  133. // TODO manage when the next subtitle appear before
  134. printf("\n");
  135. printerClean(penv);
  136. }
  137. else
  138. {
  139. printf("skipped :\n");
  140. printf("%s\n", sline.text);
  141. }
  142. }
  143. printerCloseWindow(penv);
  144. fclose(f);
  145. }