2012-01-19 15:01:19 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2012 Martin Storsjo
|
|
|
|
*
|
2013-08-16 00:12:51 +03:00
|
|
|
* This file is part of FFmpeg.
|
2012-01-19 15:01:19 +03:00
|
|
|
*
|
2013-08-16 00:12:51 +03:00
|
|
|
* FFmpeg is free software; you can redistribute it and/or
|
2012-01-19 15:01:19 +03:00
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
2013-08-16 00:12:51 +03:00
|
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
2012-01-19 15:01:19 +03:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2013-08-16 00:12:51 +03:00
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
2012-01-19 15:01:19 +03:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2012-01-21 12:33:35 +03:00
|
|
|
#include <stdlib.h>
|
2012-01-25 17:03:18 +03:00
|
|
|
|
2012-06-21 22:31:44 +03:00
|
|
|
#include "libavutil/time.h"
|
2012-01-19 15:01:19 +03:00
|
|
|
#include "libavformat/avformat.h"
|
|
|
|
|
|
|
|
static int usage(const char *argv0, int ret)
|
|
|
|
{
|
2016-10-18 14:50:45 +02:00
|
|
|
fprintf(stderr, "%s [-b bytespersec] [-d duration] [-oi <options>] [-oo <options>] input_url output_url\n", argv0);
|
|
|
|
fprintf(stderr, "<options>: AVOptions expressed as key=value, :-separated\n");
|
2012-01-19 15:01:19 +03:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2014-01-16 11:01:03 +03:00
|
|
|
int bps = 0, duration = 0, ret, i;
|
2012-01-19 15:01:19 +03:00
|
|
|
const char *input_url = NULL, *output_url = NULL;
|
|
|
|
int64_t stream_pos = 0;
|
|
|
|
int64_t start_time;
|
|
|
|
char errbuf[50];
|
|
|
|
AVIOContext *input, *output;
|
2016-10-18 14:50:45 +02:00
|
|
|
AVDictionary *in_opts = NULL;
|
|
|
|
AVDictionary *out_opts = NULL;
|
2012-01-19 15:01:19 +03:00
|
|
|
|
|
|
|
avformat_network_init();
|
|
|
|
|
|
|
|
for (i = 1; i < argc; i++) {
|
2014-01-16 10:58:03 +03:00
|
|
|
if (!strcmp(argv[i], "-b") && i + 1 < argc) {
|
2012-01-19 15:01:19 +03:00
|
|
|
bps = atoi(argv[i + 1]);
|
|
|
|
i++;
|
2014-01-16 11:01:03 +03:00
|
|
|
} else if (!strcmp(argv[i], "-d") && i + 1 < argc) {
|
|
|
|
duration = atoi(argv[i + 1]);
|
|
|
|
i++;
|
2016-10-18 14:50:45 +02:00
|
|
|
} else if (!strcmp(argv[i], "-oi") && i + 1 < argc) {
|
|
|
|
if (av_dict_parse_string(&in_opts, argv[i + 1], "=", ":", 0) < 0) {
|
|
|
|
fprintf(stderr, "Cannot parse option string %s\n",
|
|
|
|
argv[i + 1]);
|
|
|
|
return usage(argv[0], 1);
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
} else if (!strcmp(argv[i], "-oo") && i + 1 < argc) {
|
|
|
|
if (av_dict_parse_string(&out_opts, argv[i + 1], "=", ":", 0) < 0) {
|
|
|
|
fprintf(stderr, "Cannot parse option string %s\n",
|
|
|
|
argv[i + 1]);
|
|
|
|
return usage(argv[0], 1);
|
|
|
|
}
|
|
|
|
i++;
|
2012-01-19 15:01:19 +03:00
|
|
|
} else if (!input_url) {
|
|
|
|
input_url = argv[i];
|
|
|
|
} else if (!output_url) {
|
|
|
|
output_url = argv[i];
|
|
|
|
} else {
|
|
|
|
return usage(argv[0], 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!output_url)
|
|
|
|
return usage(argv[0], 1);
|
|
|
|
|
2016-10-18 14:50:45 +02:00
|
|
|
ret = avio_open2(&input, input_url, AVIO_FLAG_READ, NULL, &in_opts);
|
2012-01-19 15:01:19 +03:00
|
|
|
if (ret) {
|
|
|
|
av_strerror(ret, errbuf, sizeof(errbuf));
|
|
|
|
fprintf(stderr, "Unable to open %s: %s\n", input_url, errbuf);
|
|
|
|
return 1;
|
|
|
|
}
|
2014-01-16 11:01:03 +03:00
|
|
|
if (duration && !bps) {
|
|
|
|
int64_t size = avio_size(input);
|
|
|
|
if (size < 0) {
|
|
|
|
av_strerror(ret, errbuf, sizeof(errbuf));
|
|
|
|
fprintf(stderr, "Unable to get size of %s: %s\n", input_url, errbuf);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
bps = size / duration;
|
|
|
|
}
|
2016-10-18 14:50:45 +02:00
|
|
|
ret = avio_open2(&output, output_url, AVIO_FLAG_WRITE, NULL, &out_opts);
|
2012-01-19 15:01:19 +03:00
|
|
|
if (ret) {
|
|
|
|
av_strerror(ret, errbuf, sizeof(errbuf));
|
|
|
|
fprintf(stderr, "Unable to open %s: %s\n", output_url, errbuf);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2014-05-07 00:16:50 +03:00
|
|
|
start_time = av_gettime_relative();
|
2012-01-19 15:01:19 +03:00
|
|
|
while (1) {
|
|
|
|
uint8_t buf[1024];
|
|
|
|
int n;
|
|
|
|
n = avio_read(input, buf, sizeof(buf));
|
|
|
|
if (n <= 0)
|
|
|
|
break;
|
|
|
|
avio_write(output, buf, n);
|
2017-09-02 23:02:54 +02:00
|
|
|
if (output->error) {
|
|
|
|
av_strerror(output->error, errbuf, sizeof(errbuf));
|
|
|
|
fprintf(stderr, "Unable to write %s: %s\n", output_url, errbuf);
|
|
|
|
break;
|
|
|
|
}
|
2012-01-19 15:01:19 +03:00
|
|
|
stream_pos += n;
|
|
|
|
if (bps) {
|
|
|
|
avio_flush(output);
|
2014-05-07 00:16:50 +03:00
|
|
|
while ((av_gettime_relative() - start_time) * bps / AV_TIME_BASE < stream_pos)
|
2012-06-21 22:31:44 +03:00
|
|
|
av_usleep(50 * 1000);
|
2012-01-19 15:01:19 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-20 23:53:18 +03:00
|
|
|
avio_flush(output);
|
2012-01-19 15:01:19 +03:00
|
|
|
avio_close(output);
|
2012-01-25 17:03:18 +03:00
|
|
|
|
2012-01-19 15:01:19 +03:00
|
|
|
fail:
|
2016-10-18 14:50:45 +02:00
|
|
|
av_dict_free(&in_opts);
|
|
|
|
av_dict_free(&out_opts);
|
2012-01-19 15:01:19 +03:00
|
|
|
avio_close(input);
|
|
|
|
avformat_network_deinit();
|
|
|
|
return ret ? 1 : 0;
|
|
|
|
}
|