You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

99 lines
2.8KB

  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 <string.h>
  22. #include <unistd.h>
  23. #include <sys/stat.h>
  24. #include "libavformat/avformat.h"
  25. #include "libavformat/riff.h"
  26. #include "libavutil/intreadwrite.h"
  27. #include "libavutil/mathematics.h"
  28. static int usage(const char *argv0, int ret)
  29. {
  30. fprintf(stderr, "%s [-b bytespersec] input_url output_url\n", argv0);
  31. return ret;
  32. }
  33. int main(int argc, char **argv)
  34. {
  35. int bps = 0, ret, i;
  36. const char *input_url = NULL, *output_url = NULL;
  37. int64_t stream_pos = 0;
  38. int64_t start_time;
  39. char errbuf[50];
  40. AVIOContext *input, *output;
  41. av_register_all();
  42. avformat_network_init();
  43. for (i = 1; i < argc; i++) {
  44. if (!strcmp(argv[i], "-b")) {
  45. bps = atoi(argv[i + 1]);
  46. i++;
  47. } else if (!input_url) {
  48. input_url = argv[i];
  49. } else if (!output_url) {
  50. output_url = argv[i];
  51. } else {
  52. return usage(argv[0], 1);
  53. }
  54. }
  55. if (!output_url)
  56. return usage(argv[0], 1);
  57. ret = avio_open2(&input, input_url, AVIO_FLAG_READ, NULL, NULL);
  58. if (ret) {
  59. av_strerror(ret, errbuf, sizeof(errbuf));
  60. fprintf(stderr, "Unable to open %s: %s\n", input_url, errbuf);
  61. return 1;
  62. }
  63. ret = avio_open2(&output, output_url, AVIO_FLAG_WRITE, NULL, NULL);
  64. if (ret) {
  65. av_strerror(ret, errbuf, sizeof(errbuf));
  66. fprintf(stderr, "Unable to open %s: %s\n", output_url, errbuf);
  67. goto fail;
  68. }
  69. start_time = av_gettime();
  70. while (1) {
  71. uint8_t buf[1024];
  72. int n;
  73. n = avio_read(input, buf, sizeof(buf));
  74. if (n <= 0)
  75. break;
  76. avio_write(output, buf, n);
  77. stream_pos += n;
  78. if (bps) {
  79. avio_flush(output);
  80. while ((av_gettime() - start_time)*bps/AV_TIME_BASE < stream_pos)
  81. usleep(50*1000);
  82. }
  83. }
  84. avio_flush(output);
  85. avio_close(output);
  86. fail:
  87. avio_close(input);
  88. avformat_network_deinit();
  89. return ret ? 1 : 0;
  90. }