flv+srt.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 "flv.h"
  29. #include "avc.h"
  30. // #include "sei.h"
  31. #define MAX_SRT_SIZE (10*1024*1024)
  32. #define MAX_READ_SIZE 4096
  33. srt_t* srt_from_file (const char* path)
  34. {
  35. srt_t* head = 0;
  36. size_t read, totl = 0;
  37. FILE* file = fopen (path,"r");
  38. if (file) {
  39. char* srt_data = malloc (MAX_SRT_SIZE);
  40. size_t srt_size = 0;
  41. size_t size = MAX_SRT_SIZE;
  42. char* data = srt_data;
  43. while (0 < (read = fread (data,1,size,file))) {
  44. totl += read; data += read; size -= read; srt_size += read;
  45. }
  46. head = srt_parse (srt_data,srt_size);
  47. free (srt_data);
  48. }
  49. return head;
  50. }
  51. int main (int argc, char** argv)
  52. {
  53. flvtag_t tag;
  54. FILE* flv = flv_open_read (argv[1]);
  55. FILE* out = flv_open_write (argv[3]);
  56. int has_audio, has_video;
  57. flvtag_init (&tag);
  58. if (!flv_read_header (flv,&has_audio,&has_video)) {
  59. fprintf (stderr,"%s is not an flv file\n", argv[1]);
  60. return EXIT_FAILURE;
  61. }
  62. srt_t* head = srt_from_file (argv[2]);
  63. srt_t* srt = head;
  64. if (! head) {
  65. fprintf (stderr,"%s is not an srt file\n", argv[2]);
  66. return EXIT_FAILURE;
  67. }
  68. flv_write_header (out,has_audio,has_video);
  69. while (flv_read_tag (flv,&tag)) {
  70. // TODO handle B freame!
  71. if (srt && flvtag_pts_seconds (&tag) >= srt->timestamp && flvtag_avcpackettype_nalu == flvtag_avcpackettype (&tag)) {
  72. fprintf (stderr,"%f: %s\n", srt->timestamp, srt_data (srt));
  73. flvtag_addcaption (&tag, srt_data (srt));
  74. srt = srt->next;
  75. }
  76. flv_write_tag (out,&tag);
  77. // Write tag out here
  78. }
  79. srt_free (head);
  80. return EXIT_SUCCESS;
  81. }