FFmpeg
Loading...
Searching...
No Matches
aviocat.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2012 Martin Storsjo
3 *
4 * This file is part of FFmpeg.
5 *
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.
10 *
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.
15 *
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
19 */
20
21#include <stdio.h>
22#include <stdlib.h>
23
24#include "libavutil/time.h"
26
27static int usage(const char *argv0, int ret)
28{
29 fprintf(stderr, "%s [-b bytespersec] [-d duration] [-oi <options>] [-oo <options>] [-v] input_url output_url\n", argv0);
30 fprintf(stderr, "<options>: AVOptions expressed as key=value, :-separated\n");
31 return ret;
32}
33
34int main(int argc, char **argv)
35{
36 int bps = 0, duration = 0, verbose = 0, ret, i;
37 const char *input_url = NULL, *output_url = NULL;
38 int64_t stream_pos = 0;
40 AVIOContext *input, *output;
41 AVDictionary *in_opts = NULL;
42 AVDictionary *out_opts = NULL;
43
45
46 for (i = 1; i < argc; i++) {
47 if (!strcmp(argv[i], "-b") && i + 1 < argc) {
48 bps = atoi(argv[i + 1]);
49 i++;
50 } else if (!strcmp(argv[i], "-d") && i + 1 < argc) {
51 duration = atoi(argv[i + 1]);
52 i++;
53 } else if (!strcmp(argv[i], "-oi") && i + 1 < argc) {
54 if (av_dict_parse_string(&in_opts, argv[i + 1], "=", ":", 0) < 0) {
55 fprintf(stderr, "Cannot parse option string %s\n",
56 argv[i + 1]);
57 return usage(argv[0], 1);
58 }
59 i++;
60 } else if (!strcmp(argv[i], "-oo") && i + 1 < argc) {
61 if (av_dict_parse_string(&out_opts, argv[i + 1], "=", ":", 0) < 0) {
62 fprintf(stderr, "Cannot parse option string %s\n",
63 argv[i + 1]);
64 return usage(argv[0], 1);
65 }
66 i++;
67 } else if (!strcmp(argv[i], "-v")) {
68 verbose = 1;
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
80 ret = avio_open2(&input, input_url, AVIO_FLAG_READ, NULL, &in_opts);
81 if (ret) {
82 fprintf(stderr, "Unable to open %s: %s\n", input_url, av_err2str(ret));
83 return 1;
84 }
85 if (verbose) {
86 int64_t size = avio_seek(input, 0, AVSEEK_SIZE);
87 if (size >= 0) {
88 fprintf(stderr, "aviocat: input size: %"PRId64"\n", size);
89 } else {
90 fprintf(stderr, "aviocat: input size: unknown\n");
91 }
92 }
93 if (duration && !bps) {
94 int64_t size = avio_size(input);
95 if (size < 0) {
96 fprintf(stderr, "Unable to get size of %s: %s\n", input_url, av_err2str(ret));
97 goto fail;
98 }
99 bps = size / duration;
100 }
101 ret = avio_open2(&output, output_url, AVIO_FLAG_WRITE, NULL, &out_opts);
102 if (ret) {
103 fprintf(stderr, "Unable to open %s: %s\n", output_url, av_err2str(ret));
104 goto fail;
105 }
106
108 while (1) {
109 uint8_t buf[1024];
110 int n;
111 n = avio_read(input, buf, sizeof(buf));
112 if (n <= 0)
113 break;
114 avio_write(output, buf, n);
115 if (output->error) {
116 fprintf(stderr, "Unable to write %s: %s\n", output_url, av_err2str(output->error));
117 break;
118 }
119 stream_pos += n;
120 if (bps) {
121 avio_flush(output);
122 while ((av_gettime_relative() - start_time) * bps / AV_TIME_BASE < stream_pos)
123 av_usleep(50 * 1000);
124 }
125 }
126
127 avio_flush(output);
128 avio_close(output);
129
130fail:
131 av_dict_free(&in_opts);
132 av_dict_free(&out_opts);
133 avio_close(input);
135 return ret ? 1 : 0;
136}
Main libavformat public API header.
int avio_close(AVIOContext *s)
Close the resource accessed by the AVIOContext s and free it.
Definition avio.c:684
int avio_open2(AVIOContext **s, const char *filename, int flags, const AVIOInterruptCB *int_cb, AVDictionary **options)
Create and initialize a AVIOContext for accessing the resource indicated by url.
Definition avio.c:559
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition aviobuf.c:236
#define AVIO_FLAG_READ
read-only
Definition avio.h:617
int64_t avio_size(AVIOContext *s)
Get the filesize.
Definition aviobuf.c:326
#define AVSEEK_SIZE
Passing this as the "whence" parameter to a seek function causes it to return the filesize without se...
Definition avio.h:468
#define AVIO_FLAG_WRITE
write-only
Definition avio.h:618
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition aviobuf.c:615
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition aviobuf.c:206
void avio_flush(AVIOContext *s)
Force flushing of buffered data.
Definition aviobuf.c:228
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
int main
Definition dovi_rpuenc.c:38
static int64_t duration
Definition ffplay.c:330
static int64_t start_time
Definition ffplay.c:329
const char * usage
#define fail
Definition test.h:479
int avformat_network_deinit(void)
Undo the initialization done by avformat_network_init.
Definition utils.c:579
int avformat_network_init(void)
Do global initialization of network libraries.
Definition utils.c:567
void av_dict_free(AVDictionary **pm)
Free all the memory allocated for an AVDictionary struct and all keys and values.
Definition dict.c:233
int av_dict_parse_string(AVDictionary **pm, const char *str, const char *key_val_sep, const char *pairs_sep, int flags)
Parse the key/value pairs list and add the parsed entries to a dictionary.
Definition dict.c:210
#define av_err2str(errnum)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition error.h:122
#define AV_TIME_BASE
Internal time base represented as integer.
Definition avutil.h:253
unsigned bps
Definition movenc.c:2074
Bytestream IO Context.
Definition avio.h:160
int error
contains the error code or 0 if no error happened
Definition avio.h:239
int av_usleep(unsigned usec)
Sleep for a period of time.
Definition time.c:93
int64_t av_gettime_relative(void)
Get the current time in microseconds since some unspecified starting point.
Definition time.c:57
int size