2 * Copyright (c) 2012 Martin Storsjo
4 * This file is part of FFmpeg.
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.
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.
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
24 #include "libavutil/time.h"
25 #include "libavformat/avformat.h"
27 static int usage(const char *argv0, int ret)
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");
34 int main(int argc, char **argv)
36 int bps = 0, duration = 0, ret, i;
37 const char *input_url = NULL, *output_url = NULL;
38 int64_t stream_pos = 0;
41 AVIOContext *input, *output;
42 AVDictionary *in_opts = NULL;
43 AVDictionary *out_opts = NULL;
45 avformat_network_init();
47 for (i = 1; i < argc; i++) {
48 if (!strcmp(argv[i], "-b") && i + 1 < argc) {
49 bps = atoi(argv[i + 1]);
51 } else if (!strcmp(argv[i], "-d") && i + 1 < argc) {
52 duration = atoi(argv[i + 1]);
54 } else if (!strcmp(argv[i], "-oi") && i + 1 < argc) {
55 if (av_dict_parse_string(&in_opts, argv[i + 1], "=", ":", 0) < 0) {
56 fprintf(stderr, "Cannot parse option string %s\n",
58 return usage(argv[0], 1);
61 } else if (!strcmp(argv[i], "-oo") && i + 1 < argc) {
62 if (av_dict_parse_string(&out_opts, argv[i + 1], "=", ":", 0) < 0) {
63 fprintf(stderr, "Cannot parse option string %s\n",
65 return usage(argv[0], 1);
68 } else if (!input_url) {
70 } else if (!output_url) {
73 return usage(argv[0], 1);
77 return usage(argv[0], 1);
79 ret = avio_open2(&input, input_url, AVIO_FLAG_READ, NULL, &in_opts);
81 av_strerror(ret, errbuf, sizeof(errbuf));
82 fprintf(stderr, "Unable to open %s: %s\n", input_url, errbuf);
85 if (duration && !bps) {
86 int64_t size = avio_size(input);
88 av_strerror(ret, errbuf, sizeof(errbuf));
89 fprintf(stderr, "Unable to get size of %s: %s\n", input_url, errbuf);
92 bps = size / duration;
94 ret = avio_open2(&output, output_url, AVIO_FLAG_WRITE, NULL, &out_opts);
96 av_strerror(ret, errbuf, sizeof(errbuf));
97 fprintf(stderr, "Unable to open %s: %s\n", output_url, errbuf);
101 start_time = av_gettime_relative();
105 n = avio_read(input, buf, sizeof(buf));
108 avio_write(output, buf, n);
110 av_strerror(output->error, errbuf, sizeof(errbuf));
111 fprintf(stderr, "Unable to write %s: %s\n", output_url, errbuf);
117 while ((av_gettime_relative() - start_time) * bps / AV_TIME_BASE < stream_pos)
118 av_usleep(50 * 1000);
126 av_dict_free(&in_opts);
127 av_dict_free(&out_opts);
129 avformat_network_deinit();