aviocat.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. * Copyright (c) 2012 Martin Storsjo
  3. *
  4. * This file is part of Libav.
  5. *
  6. * Libav 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. * Libav 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 Libav; 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] input_url output_url\n", argv0);
  27. return ret;
  28. }
  29. int main(int argc, char **argv)
  30. {
  31. int bps = 0, ret, i;
  32. const char *input_url = NULL, *output_url = NULL;
  33. int64_t stream_pos = 0;
  34. int64_t start_time;
  35. char errbuf[50];
  36. AVIOContext *input, *output;
  37. av_register_all();
  38. avformat_network_init();
  39. for (i = 1; i < argc; i++) {
  40. if (!strcmp(argv[i], "-b")) {
  41. bps = atoi(argv[i + 1]);
  42. i++;
  43. } else if (!input_url) {
  44. input_url = argv[i];
  45. } else if (!output_url) {
  46. output_url = argv[i];
  47. } else {
  48. return usage(argv[0], 1);
  49. }
  50. }
  51. if (!output_url)
  52. return usage(argv[0], 1);
  53. ret = avio_open2(&input, input_url, AVIO_FLAG_READ, NULL, NULL);
  54. if (ret) {
  55. av_strerror(ret, errbuf, sizeof(errbuf));
  56. fprintf(stderr, "Unable to open %s: %s\n", input_url, errbuf);
  57. return 1;
  58. }
  59. ret = avio_open2(&output, output_url, AVIO_FLAG_WRITE, NULL, NULL);
  60. if (ret) {
  61. av_strerror(ret, errbuf, sizeof(errbuf));
  62. fprintf(stderr, "Unable to open %s: %s\n", output_url, errbuf);
  63. goto fail;
  64. }
  65. start_time = av_gettime();
  66. while (1) {
  67. uint8_t buf[1024];
  68. int n;
  69. n = avio_read(input, buf, sizeof(buf));
  70. if (n <= 0)
  71. break;
  72. avio_write(output, buf, n);
  73. stream_pos += n;
  74. if (bps) {
  75. avio_flush(output);
  76. while ((av_gettime() - start_time) * bps / AV_TIME_BASE < stream_pos)
  77. av_usleep(50 * 1000);
  78. }
  79. }
  80. avio_flush(output);
  81. avio_close(output);
  82. fail:
  83. avio_close(input);
  84. avformat_network_deinit();
  85. return ret ? 1 : 0;
  86. }