aviocat.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * Copyright (c) 2012 Martin Storsjo
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include "libavutil/time.h"
  23. #include "libavformat/avformat.h"
  24. static int usage(const char *argv0, int ret)
  25. {
  26. fprintf(stderr, "%s [-b bytespersec] [-d duration] [-oi <options>] [-oo <options>] [-v] input_url output_url\n", argv0);
  27. fprintf(stderr, "<options>: AVOptions expressed as key=value, :-separated\n");
  28. return ret;
  29. }
  30. int main(int argc, char **argv)
  31. {
  32. int bps = 0, duration = 0, verbose = 0, ret, i;
  33. const char *input_url = NULL, *output_url = NULL;
  34. int64_t stream_pos = 0;
  35. int64_t start_time;
  36. char errbuf[50];
  37. AVIOContext *input, *output;
  38. AVDictionary *in_opts = NULL;
  39. AVDictionary *out_opts = NULL;
  40. avformat_network_init();
  41. for (i = 1; i < argc; i++) {
  42. if (!strcmp(argv[i], "-b") && i + 1 < argc) {
  43. bps = atoi(argv[i + 1]);
  44. i++;
  45. } else if (!strcmp(argv[i], "-d") && i + 1 < argc) {
  46. duration = atoi(argv[i + 1]);
  47. i++;
  48. } else if (!strcmp(argv[i], "-oi") && i + 1 < argc) {
  49. if (av_dict_parse_string(&in_opts, argv[i + 1], "=", ":", 0) < 0) {
  50. fprintf(stderr, "Cannot parse option string %s\n",
  51. argv[i + 1]);
  52. return usage(argv[0], 1);
  53. }
  54. i++;
  55. } else if (!strcmp(argv[i], "-oo") && i + 1 < argc) {
  56. if (av_dict_parse_string(&out_opts, argv[i + 1], "=", ":", 0) < 0) {
  57. fprintf(stderr, "Cannot parse option string %s\n",
  58. argv[i + 1]);
  59. return usage(argv[0], 1);
  60. }
  61. i++;
  62. } else if (!strcmp(argv[i], "-v")) {
  63. verbose = 1;
  64. } else if (!input_url) {
  65. input_url = argv[i];
  66. } else if (!output_url) {
  67. output_url = argv[i];
  68. } else {
  69. return usage(argv[0], 1);
  70. }
  71. }
  72. if (!output_url)
  73. return usage(argv[0], 1);
  74. ret = avio_open2(&input, input_url, AVIO_FLAG_READ, NULL, &in_opts);
  75. if (ret) {
  76. av_strerror(ret, errbuf, sizeof(errbuf));
  77. fprintf(stderr, "Unable to open %s: %s\n", input_url, errbuf);
  78. return 1;
  79. }
  80. if (verbose) {
  81. int64_t size = avio_seek(input, 0, AVSEEK_SIZE);
  82. if (size >= 0) {
  83. fprintf(stderr, "aviocat: input size: %"PRId64"\n", size);
  84. } else {
  85. fprintf(stderr, "aviocat: input size: unknown\n");
  86. }
  87. }
  88. if (duration && !bps) {
  89. int64_t size = avio_size(input);
  90. if (size < 0) {
  91. av_strerror(ret, errbuf, sizeof(errbuf));
  92. fprintf(stderr, "Unable to get size of %s: %s\n", input_url, errbuf);
  93. goto fail;
  94. }
  95. bps = size / duration;
  96. }
  97. ret = avio_open2(&output, output_url, AVIO_FLAG_WRITE, NULL, &out_opts);
  98. if (ret) {
  99. av_strerror(ret, errbuf, sizeof(errbuf));
  100. fprintf(stderr, "Unable to open %s: %s\n", output_url, errbuf);
  101. goto fail;
  102. }
  103. start_time = av_gettime_relative();
  104. while (1) {
  105. uint8_t buf[1024];
  106. int n;
  107. n = avio_read(input, buf, sizeof(buf));
  108. if (n <= 0)
  109. break;
  110. avio_write(output, buf, n);
  111. if (output->error) {
  112. av_strerror(output->error, errbuf, sizeof(errbuf));
  113. fprintf(stderr, "Unable to write %s: %s\n", output_url, errbuf);
  114. break;
  115. }
  116. stream_pos += n;
  117. if (bps) {
  118. avio_flush(output);
  119. while ((av_gettime_relative() - start_time) * bps / AV_TIME_BASE < stream_pos)
  120. av_usleep(50 * 1000);
  121. }
  122. }
  123. avio_flush(output);
  124. avio_close(output);
  125. fail:
  126. av_dict_free(&in_opts);
  127. av_dict_free(&out_opts);
  128. avio_close(input);
  129. avformat_network_deinit();
  130. return ret ? 1 : 0;
  131. }