main.c 3.6 KB

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