ts2srt.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 "ts.h"
  25. #include "srt.h"
  26. #include "avc.h"
  27. #include <stdio.h>
  28. int main (int argc, char** argv)
  29. {
  30. const char* path = argv[1];
  31. ts_t ts;
  32. sei_t sei;
  33. avcnalu_t nalu;
  34. srt_t* srt = 0, *head = 0;
  35. caption_frame_t frame;
  36. uint8_t pkt[TS_PACKET_SIZE];
  37. ts_init (&ts);
  38. avcnalu_init (&nalu);
  39. caption_frame_init (&frame);
  40. FILE* file = fopen (path,"rb+");
  41. while (TS_PACKET_SIZE == fread (&pkt[0],1,TS_PACKET_SIZE, file)) {
  42. switch (ts_parse_packet (&ts,&pkt[0])) {
  43. case LIBCAPTION_OK:
  44. // fprintf (stderr,"read ts packet\n");
  45. break;
  46. case LIBCAPTION_READY: {
  47. // fprintf (stderr,"read ts packet DATA\n");
  48. while (ts.size) {
  49. // fprintf (stderr,"ts.size %d (%02X%02X%02X%02X)\n",ts.size, ts.data[0], ts.data[1], ts.data[2], ts.data[3]);
  50. switch (avcnalu_parse_annexb (&nalu, &ts.data, &ts.size)) {
  51. case LIBCAPTION_OK:
  52. break;
  53. case LIBCAPTION_ERROR:
  54. // fprintf (stderr,"LIBCAPTION_ERROR == avcnalu_parse_annexb()\n");
  55. avcnalu_init (&nalu);
  56. break;
  57. case LIBCAPTION_READY: {
  58. if (6 == avcnalu_type (&nalu)) {
  59. // fprintf (stderr,"NALU %d (%d)\n", avcnalu_type (&nalu), avcnalu_size (&nalu));
  60. sei_init (&sei);
  61. sei_parse_avcnalu (&sei, &nalu, ts_dts_seconds (&ts), ts_cts_seconds (&ts));
  62. // sei_dump (&sei);
  63. if (LIBCAPTION_READY == sei_to_caption_frame (&sei,&frame)) {
  64. // caption_frame_dump (&frame);
  65. srt = srt_from_caption_frame (&frame,srt,&head);
  66. // srt_dump (srt);
  67. }
  68. sei_free (&sei);
  69. }
  70. avcnalu_init (&nalu);
  71. } break;
  72. }
  73. }
  74. } break;
  75. case LIBCAPTION_ERROR:
  76. // fprintf (stderr,"read ts packet ERROR\n");
  77. break;
  78. }
  79. }
  80. srt_dump (head);
  81. srt_free (head);
  82. return 1;
  83. }