rollup.c 4.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 "flv.h"
  28. #include "avc.h"
  29. #include "srt.h"
  30. #include "wonderland.h"
  31. #define SECONDS_PER_LINE 3.0
  32. srt_t* appennd_caption (const utf8_char_t* data, srt_t* prev, srt_t** head)
  33. {
  34. int r, c, chan = 0;
  35. ssize_t size = (ssize_t) strlen (data);
  36. size_t char_count, char_length, line_length = 0, trimmed_length = 0;
  37. for (r = 0 ; 0 < size && SCREEN_ROWS > r ; ++r) {
  38. line_length = utf8_line_length (data);
  39. trimmed_length = utf8_trimmed_length (data,line_length);
  40. char_count = utf8_char_count (data,trimmed_length);
  41. // If char_count is greater than one line can display, split it.
  42. if (SCREEN_COLS < char_count) {
  43. char_count = utf8_wrap_length (data,SCREEN_COLS);
  44. line_length = utf8_string_length (data,char_count+1);
  45. }
  46. // fprintf (stderr,"%.*s\n", line_length, data);
  47. prev = srt_new (data, line_length, prev ? prev->timestamp + SECONDS_PER_LINE : 0, prev, head);
  48. data += line_length;
  49. size -= (ssize_t) line_length;
  50. }
  51. return prev;
  52. }
  53. int main (int argc, char** argv)
  54. {
  55. int i = 0;
  56. flvtag_t tag;
  57. srt_t* head = 0, *tail = 0;
  58. int has_audio, has_video;
  59. FILE* flv = flv_open_read (argv[1]);
  60. FILE* out = flv_open_write (argv[2]);
  61. flvtag_init (&tag);
  62. for (i = 0 ; wonderland[i][0]; ++i) {
  63. tail = appennd_caption (wonderland[i], tail, &head);
  64. }
  65. if (!flv_read_header (flv,&has_audio,&has_video)) {
  66. fprintf (stderr,"%s is not an flv file\n", argv[1]);
  67. return EXIT_FAILURE;
  68. }
  69. flv_write_header (out,has_audio,has_video);
  70. while (flv_read_tag (flv,&tag)) {
  71. if (head && flvtag_avcpackettype_nalu == flvtag_avcpackettype (&tag) && head->timestamp <= flvtag_pts_seconds (&tag)) {
  72. fprintf (stderr,"%f %s\n", flvtag_pts_seconds (&tag), srt_data (head));
  73. flvtag_addcaption (&tag, srt_data (head));
  74. head = srt_free_head (head);
  75. }
  76. flv_write_tag (out,&tag);
  77. }
  78. return EXIT_SUCCESS;
  79. }