main.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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(" -m px\t\t: margin with the bottom of the screen\n");
  28. printf(" -p px\t\t: padding of the box\n");
  29. printf(" -g px\t\t: gap between two lines\n");
  30. printf(" -f fontname\t: name of the font to use\n");
  31. printf(" -i fontname\t: name of the italic font to use\n");
  32. printf(" -b fontname\t: name of the bold font to use\n");
  33. printf(" -j fontname\t: name of the bold and italic font to use\n");
  34. printf(" -h\t\t: display this help and exit\n");
  35. }
  36. int main(int argc, char **argv)
  37. {
  38. int i, shift = 0, delay = 5, margin_bottom = 50, padding = 5, gap = 5;
  39. char *font = NULL, *font_i = NULL, *font_b = NULL, *font_bi = NULL;
  40. FILE *f = NULL;
  41. // parse arguments
  42. int c;
  43. while((c = getopt (argc, argv, "s:d:m:p:g:f:i:b:j:h")) != -1)
  44. switch(c)
  45. {
  46. case 's':
  47. shift = atoi(optarg);
  48. break;
  49. case 'd':
  50. delay = atoi(optarg);
  51. break;
  52. case 'm':
  53. margin_bottom = atoi(optarg);
  54. break;
  55. case 'p':
  56. padding = atoi(optarg);
  57. break;
  58. case 'g':
  59. gap = atoi(optarg);
  60. break;
  61. case 'f':
  62. font = malloc(strlen(optarg)+1);
  63. strcpy(font, optarg);
  64. break;
  65. case 'i':
  66. font_i = malloc(strlen(optarg)+1);
  67. strcpy(font_i, optarg);
  68. break;
  69. case 'b':
  70. font_b = malloc(strlen(optarg)+1);
  71. strcpy(font_b, optarg);
  72. break;
  73. case 'j':
  74. font_bi = malloc(strlen(optarg)+1);
  75. strcpy(font_bi, optarg);
  76. break;
  77. case 'h':
  78. displayUsage(argv[0]);
  79. return 0;
  80. //case '?':
  81. default:
  82. return 1;
  83. }
  84. if(optind >= argc)
  85. {
  86. fprintf(stderr, "Missing filename.\n");
  87. displayUsage(argv[0]);
  88. return 1;
  89. }
  90. f = fopen(argv[optind], "r");
  91. if(f == NULL)
  92. {
  93. perror("fopen()");
  94. exit(1);
  95. }
  96. // open the window
  97. struct printerEnv penv = printerOpenWindow(font, font_i, font_b, font_bi);
  98. // set attributes
  99. penv.margin_bottom = margin_bottom;
  100. penv.padding = padding;
  101. penv.gap = gap;
  102. // show a counter before start the clock
  103. for(i = delay; i > 0; i--)
  104. {
  105. char t[16];
  106. sprintf(t, "%d...\n", i);
  107. printf("%s", t);
  108. printerShow(&penv, t, T_ITALIC);
  109. sleep(1);
  110. }
  111. printf("0 !\n");
  112. printerClean(penv);
  113. timeInitialize(-shift);
  114. struct SubtitleLine sline;
  115. int id = 0;
  116. while(!feof(f))
  117. {
  118. id = next(f, id+1, &sline);
  119. if(!timeSleepUntil(sline.begin)) // no error and in the future
  120. {
  121. printf("%ds\n", sline.begin.tv_sec);
  122. // show
  123. printf("%s\n", sline.text);
  124. printerShow(&penv, sline.text, 0);
  125. // hide
  126. timeSleepUntil(sline.end);
  127. // TODO manage when the next subtitle appear before
  128. printf("\n");
  129. printerClean(penv);
  130. }
  131. else
  132. {
  133. printf("skipped :\n");
  134. printf("%s\n", sline.text);
  135. }
  136. }
  137. printerCloseWindow(penv);
  138. fclose(f);
  139. }