Commit | Line | Data |
---|---|---|
23e57d16 MS |
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 | ||
21 | #include <stdio.h> | |
7ba34575 | 22 | #include <stdlib.h> |
4e81b5f5 | 23 | |
896bb0d7 | 24 | #include "libavutil/time.h" |
23e57d16 | 25 | #include "libavformat/avformat.h" |
23e57d16 MS |
26 | |
27 | static int usage(const char *argv0, int ret) | |
28 | { | |
052b9785 LB |
29 | fprintf(stderr, "%s [-b bytespersec] [-d duration] [-oi <options>] [-oo <options>] input_url output_url\n", argv0); |
30 | fprintf(stderr, "<options>: AVOptions expressed as key=value, :-separated\n"); | |
23e57d16 MS |
31 | return ret; |
32 | } | |
33 | ||
34 | int main(int argc, char **argv) | |
35 | { | |
87acd33c | 36 | int bps = 0, duration = 0, ret, i; |
23e57d16 MS |
37 | const char *input_url = NULL, *output_url = NULL; |
38 | int64_t stream_pos = 0; | |
39 | int64_t start_time; | |
40 | char errbuf[50]; | |
41 | AVIOContext *input, *output; | |
052b9785 LB |
42 | AVDictionary *in_opts = NULL; |
43 | AVDictionary *out_opts = NULL; | |
23e57d16 MS |
44 | |
45 | av_register_all(); | |
46 | avformat_network_init(); | |
47 | ||
48 | for (i = 1; i < argc; i++) { | |
03f2de58 | 49 | if (!strcmp(argv[i], "-b") && i + 1 < argc) { |
23e57d16 MS |
50 | bps = atoi(argv[i + 1]); |
51 | i++; | |
87acd33c MS |
52 | } else if (!strcmp(argv[i], "-d") && i + 1 < argc) { |
53 | duration = atoi(argv[i + 1]); | |
54 | i++; | |
052b9785 LB |
55 | } else if (!strcmp(argv[i], "-oi") && i + 1 < argc) { |
56 | if (av_dict_parse_string(&in_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], "-oo") && i + 1 < argc) { | |
63 | if (av_dict_parse_string(&out_opts, argv[i + 1], "=", ":", 0) < 0) { | |
64 | fprintf(stderr, "Cannot parse option string %s\n", | |
65 | argv[i + 1]); | |
66 | return usage(argv[0], 1); | |
67 | } | |
68 | i++; | |
23e57d16 MS |
69 | } else if (!input_url) { |
70 | input_url = argv[i]; | |
71 | } else if (!output_url) { | |
72 | output_url = argv[i]; | |
73 | } else { | |
74 | return usage(argv[0], 1); | |
75 | } | |
76 | } | |
77 | if (!output_url) | |
78 | return usage(argv[0], 1); | |
79 | ||
052b9785 | 80 | ret = avio_open2(&input, input_url, AVIO_FLAG_READ, NULL, &in_opts); |
23e57d16 MS |
81 | if (ret) { |
82 | av_strerror(ret, errbuf, sizeof(errbuf)); | |
83 | fprintf(stderr, "Unable to open %s: %s\n", input_url, errbuf); | |
84 | return 1; | |
85 | } | |
87acd33c MS |
86 | if (duration && !bps) { |
87 | int64_t size = avio_size(input); | |
88 | if (size < 0) { | |
89 | av_strerror(ret, errbuf, sizeof(errbuf)); | |
90 | fprintf(stderr, "Unable to get size of %s: %s\n", input_url, errbuf); | |
91 | goto fail; | |
92 | } | |
93 | bps = size / duration; | |
94 | } | |
052b9785 | 95 | ret = avio_open2(&output, output_url, AVIO_FLAG_WRITE, NULL, &out_opts); |
23e57d16 MS |
96 | if (ret) { |
97 | av_strerror(ret, errbuf, sizeof(errbuf)); | |
98 | fprintf(stderr, "Unable to open %s: %s\n", output_url, errbuf); | |
99 | goto fail; | |
100 | } | |
101 | ||
e205429f | 102 | start_time = av_gettime_relative(); |
23e57d16 MS |
103 | while (1) { |
104 | uint8_t buf[1024]; | |
105 | int n; | |
106 | n = avio_read(input, buf, sizeof(buf)); | |
107 | if (n <= 0) | |
108 | break; | |
109 | avio_write(output, buf, n); | |
110 | stream_pos += n; | |
111 | if (bps) { | |
112 | avio_flush(output); | |
e205429f | 113 | while ((av_gettime_relative() - start_time) * bps / AV_TIME_BASE < stream_pos) |
896bb0d7 | 114 | av_usleep(50 * 1000); |
23e57d16 MS |
115 | } |
116 | } | |
117 | ||
6a73f3bb | 118 | avio_flush(output); |
23e57d16 | 119 | avio_close(output); |
4e81b5f5 | 120 | |
23e57d16 | 121 | fail: |
052b9785 LB |
122 | av_dict_free(&in_opts); |
123 | av_dict_free(&out_opts); | |
23e57d16 MS |
124 | avio_close(input); |
125 | avformat_network_deinit(); | |
126 | return ret ? 1 : 0; | |
127 | } |