scc2srt.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /**********************************************************************************************/
  2. /* The MIT License */
  3. /* */
  4. /* Copyright 2016-2016 Twitch Interactive, Inc. or its affiliates. All Rights Reserved. */
  5. /* */
  6. /* Permission is hereby granted, free of charge, to any person obtaining a copy */
  7. /* of this software and associated documentation files (the "Software"), to deal */
  8. /* in the Software without restriction, including without limitation the rights */
  9. /* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell */
  10. /* copies of the Software, and to permit persons to whom the Software is */
  11. /* furnished to do so, subject to the following conditions: */
  12. /* */
  13. /* The above copyright notice and this permission notice shall be included in */
  14. /* all copies or substantial portions of the Software. */
  15. /* */
  16. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR */
  17. /* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, */
  18. /* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE */
  19. /* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER */
  20. /* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, */
  21. /* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN */
  22. /* THE SOFTWARE. */
  23. /**********************************************************************************************/
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include "srt.h"
  28. #include "scc.h"
  29. #define MAX_SCC_SIZE (10*1024*1024)
  30. #define MAX_READ_SIZE 4096
  31. #define MAX_CC 128
  32. size_t read_file (FILE* file, utf8_char_t* data, size_t size)
  33. {
  34. size_t read, totl = 0;
  35. while (0 < (read = fread (data,1,MAX_READ_SIZE<size?MAX_READ_SIZE:size,file))) {
  36. totl += read; data += read; size -= read;
  37. }
  38. return totl;
  39. }
  40. srt_t* scc2srt (const char* data)
  41. {
  42. double pts;
  43. size_t line_size = 0;
  44. int cc_idx, count, i;
  45. srt_t* srt = 0, *head = 0;
  46. caption_frame_t frame;
  47. uint16_t cc_data[MAX_CC];
  48. while (0 < (line_size = utf8_line_length (data))) {
  49. caption_frame_init (&frame);
  50. int cc_count = scc_to_608 (data, &pts, (uint16_t*) &cc_data, MAX_CC);
  51. data += line_size;
  52. data += utf8_line_length (data); // skip empty line
  53. // fprintf (stderr,"%f, %d| %.*s\n", pts, cc_count, (int) line_size,data);
  54. for (cc_idx = 0 ; cc_idx < cc_count ; ++cc_idx) {
  55. // eia608_dump (cc_data[cc_idx]);
  56. caption_frame_decode (&frame,cc_data[cc_idx],pts);
  57. }
  58. // utf8_char_t buff[CAPTION_FRAME_DUMP_BUF_SIZE];
  59. // size_t size = caption_frame_dump (&frame, buff);
  60. // fprintf (stderr,"%s\n", buff);
  61. srt = srt_from_caption_frame (&frame,srt,&head);
  62. }
  63. return head;
  64. }
  65. int main (int argc, char** argv)
  66. {
  67. char frame_buf[CAPTION_FRAME_DUMP_BUF_SIZE];
  68. if (argc < 2) {
  69. return 0;
  70. }
  71. FILE* file = fopen (argv[1],"r");
  72. if (! file) {
  73. return 0;
  74. }
  75. utf8_char_t* data = malloc (MAX_SCC_SIZE);
  76. read_file (file,data,MAX_SCC_SIZE);
  77. srt_t* srt = scc2srt (data);
  78. srt_dump (srt);
  79. srt_free (srt);
  80. free (data);
  81. }