FFmpeg
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"
25 #include "libavformat/avformat.h"
26 
27 static 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 
34 int 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;
39  int64_t start_time;
40  char errbuf[50];
42  AVDictionary *in_opts = NULL;
43  AVDictionary *out_opts = NULL;
44 
46 
47  for (i = 1; i < argc; i++) {
48  if (!strcmp(argv[i], "-b") && i + 1 < argc) {
49  bps = atoi(argv[i + 1]);
50  i++;
51  } else if (!strcmp(argv[i], "-d") && i + 1 < argc) {
52  duration = atoi(argv[i + 1]);
53  i++;
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",
57  argv[i + 1]);
58  return usage(argv[0], 1);
59  }
60  i++;
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",
64  argv[i + 1]);
65  return usage(argv[0], 1);
66  }
67  i++;
68  } else if (!strcmp(argv[i], "-v")) {
69  verbose = 1;
70  } else if (!input_url) {
71  input_url = argv[i];
72  } else if (!output_url) {
73  output_url = argv[i];
74  } else {
75  return usage(argv[0], 1);
76  }
77  }
78  if (!output_url)
79  return usage(argv[0], 1);
80 
81  ret = avio_open2(&input, input_url, AVIO_FLAG_READ, NULL, &in_opts);
82  if (ret) {
83  av_strerror(ret, errbuf, sizeof(errbuf));
84  fprintf(stderr, "Unable to open %s: %s\n", input_url, errbuf);
85  return 1;
86  }
87  if (verbose) {
88  int64_t size = avio_seek(input, 0, AVSEEK_SIZE);
89  if (size >= 0) {
90  fprintf(stderr, "aviocat: input size: %"PRId64"\n", size);
91  } else {
92  fprintf(stderr, "aviocat: input size: unknown\n");
93  }
94  }
95  if (duration && !bps) {
96  int64_t size = avio_size(input);
97  if (size < 0) {
98  av_strerror(ret, errbuf, sizeof(errbuf));
99  fprintf(stderr, "Unable to get size of %s: %s\n", input_url, errbuf);
100  goto fail;
101  }
102  bps = size / duration;
103  }
104  ret = avio_open2(&output, output_url, AVIO_FLAG_WRITE, NULL, &out_opts);
105  if (ret) {
106  av_strerror(ret, errbuf, sizeof(errbuf));
107  fprintf(stderr, "Unable to open %s: %s\n", output_url, errbuf);
108  goto fail;
109  }
110 
112  while (1) {
113  uint8_t buf[1024];
114  int n;
115  n = avio_read(input, buf, sizeof(buf));
116  if (n <= 0)
117  break;
118  avio_write(output, buf, n);
119  if (output->error) {
120  av_strerror(output->error, errbuf, sizeof(errbuf));
121  fprintf(stderr, "Unable to write %s: %s\n", output_url, errbuf);
122  break;
123  }
124  stream_pos += n;
125  if (bps) {
127  while ((av_gettime_relative() - start_time) * bps / AV_TIME_BASE < stream_pos)
128  av_usleep(50 * 1000);
129  }
130  }
131 
134 
135 fail:
136  av_dict_free(&in_opts);
137  av_dict_free(&out_opts);
138  avio_close(input);
140  return ret ? 1 : 0;
141 }
av_gettime_relative
int64_t av_gettime_relative(void)
Get the current time in microseconds since some unspecified starting point.
Definition: time.c:56
avio_close
int avio_close(AVIOContext *s)
Close the resource accessed by the AVIOContext s and free it.
Definition: aviobuf.c:1169
main
int main(int argc, char **argv)
Definition: aviocat.c:34
output
filter_frame For filters that do not use the this method is called when a frame is pushed to the filter s input It can be called at any time except in a reentrant way If the input frame is enough to produce output
Definition: filter_design.txt:225
AVSEEK_SIZE
#define AVSEEK_SIZE
ORing this as the "whence" parameter to a seek function causes it to return the filesize without seek...
Definition: avio.h:531
AVDictionary
Definition: dict.c:30
avio_size
int64_t avio_size(AVIOContext *s)
Get the filesize.
Definition: aviobuf.c:342
avio_open2
int avio_open2(AVIOContext **s, const char *url, int flags, const AVIOInterruptCB *int_cb, AVDictionary **options)
Create and initialize a AVIOContext for accessing the resource indicated by url.
Definition: aviobuf.c:1163
fail
#define fail()
Definition: checkasm.h:133
av_strerror
int av_strerror(int errnum, char *errbuf, size_t errbuf_size)
Put a description of the AVERROR code errnum in errbuf.
Definition: error.c:105
avformat_network_init
int avformat_network_init(void)
Do global initialization of network libraries.
Definition: utils.c:5056
duration
int64_t duration
Definition: movenc.c:64
AVIO_FLAG_WRITE
#define AVIO_FLAG_WRITE
write-only
Definition: avio.h:675
av_usleep
int av_usleep(unsigned usec)
Sleep for a period of time.
Definition: time.c:84
avio_flush
void avio_flush(AVIOContext *s)
Force flushing of buffered data.
Definition: aviobuf.c:245
NULL
#define NULL
Definition: coverity.c:32
time.h
AVIOContext
Bytestream IO Context.
Definition: avio.h:161
usage
static int usage(const char *argv0, int ret)
Definition: aviocat.c:27
start_time
static int64_t start_time
Definition: ffplay.c:332
bps
unsigned bps
Definition: movenc.c:1612
size
int size
Definition: twinvq_data.h:10344
avio_write
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:225
av_dict_free
void av_dict_free(AVDictionary **pm)
Free all the memory allocated for an AVDictionary struct and all keys and values.
Definition: dict.c:203
input
and forward the test the status of outputs and forward it to the corresponding return FFERROR_NOT_READY If the filters stores internally one or a few frame for some input
Definition: filter_design.txt:172
i
int i
Definition: input.c:407
AV_TIME_BASE
#define AV_TIME_BASE
Internal time base represented as integer.
Definition: avutil.h:254
uint8_t
uint8_t
Definition: audio_convert.c:194
ret
ret
Definition: filter_design.txt:187
avio_seek
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:253
verbose
int verbose
Definition: checkasm.c:285
avformat.h
avformat_network_deinit
int avformat_network_deinit(void)
Undo the initialization done by avformat_network_init.
Definition: utils.c:5068
av_dict_parse_string
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:180
avio_read
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:633
AVIO_FLAG_READ
#define AVIO_FLAG_READ
read-only
Definition: avio.h:674