flv2srt.c 4.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 "flv.h"
  25. #include "srt.h"
  26. #include "avc.h"
  27. #define LENGTH_SIZE 4
  28. int main (int argc, char** argv)
  29. {
  30. const char* path = argv[1];
  31. sei_t sei;
  32. flvtag_t tag;
  33. srt_t* srt = 0, *head = 0;
  34. int i, has_audio, has_video;
  35. caption_frame_t frame;
  36. flvtag_init (&tag);
  37. caption_frame_init (&frame);
  38. FILE* flv = flv_open_read (path);
  39. if (!flv_read_header (flv,&has_audio,&has_video)) {
  40. fprintf (stderr,"'%s' Not an flv file\n", path);
  41. } else {
  42. fprintf (stderr,"Reading from '%s'\n", path);
  43. }
  44. while (flv_read_tag (flv,&tag)) {
  45. if (flvtag_avcpackettype_nalu == flvtag_avcpackettype (&tag)) {
  46. ssize_t size = flvtag_payload_size (&tag);
  47. uint8_t* data = flvtag_payload_data (&tag);
  48. while (0<size) {
  49. ssize_t nalu_size = (data[0]<<24) | (data[1]<<16) | (data[2]<<8) | data[3];
  50. uint8_t* nalu_data = &data[4];
  51. uint8_t nalu_type = nalu_data[0]&0x1F;
  52. data += nalu_size + LENGTH_SIZE;
  53. size -= nalu_size + LENGTH_SIZE;
  54. if (6 == nalu_type) {
  55. sei_init (&sei);
  56. sei_parse_nalu (&sei, nalu_data, nalu_size, flvtag_dts (&tag), flvtag_cts (&tag));
  57. cea708_t cea708;
  58. sei_message_t* msg;
  59. cea708_init (&cea708);
  60. // for (msg = sei_message_head (&sei) ; msg ; msg = sei_message_next (msg)) {
  61. // if (sei_type_user_data_registered_itu_t_t35 == sei_message_type (msg)) {
  62. // cea708_parse (sei_message_data (msg), sei_message_size (msg), &cea708);
  63. // cea708_dump (&cea708);
  64. // }
  65. // }
  66. // sei_dump(&sei);
  67. sei_to_caption_frame (&sei,&frame);
  68. sei_free (&sei);
  69. // caption_frame_dump (&frame);
  70. srt = srt_from_caption_frame (&frame,srt,&head);
  71. }
  72. }
  73. }
  74. }
  75. srt_dump (head);
  76. srt_free (head);
  77. return 1;
  78. }