aviocat.c
Go to the documentation of this file.
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>
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] input_url output_url\n", argv0);
30  return ret;
31 }
32 
33 int main(int argc, char **argv)
34 {
35  int bps = 0, ret, i;
36  const char *input_url = NULL, *output_url = NULL;
37  int64_t stream_pos = 0;
38  int64_t start_time;
39  char errbuf[50];
41 
44 
45  for (i = 1; i < argc; i++) {
46  if (!strcmp(argv[i], "-b")) {
47  bps = atoi(argv[i + 1]);
48  i++;
49  } else if (!input_url) {
50  input_url = argv[i];
51  } else if (!output_url) {
52  output_url = argv[i];
53  } else {
54  return usage(argv[0], 1);
55  }
56  }
57  if (!output_url)
58  return usage(argv[0], 1);
59 
60  ret = avio_open2(&input, input_url, AVIO_FLAG_READ, NULL, NULL);
61  if (ret) {
62  av_strerror(ret, errbuf, sizeof(errbuf));
63  fprintf(stderr, "Unable to open %s: %s\n", input_url, errbuf);
64  return 1;
65  }
66  ret = avio_open2(&output, output_url, AVIO_FLAG_WRITE, NULL, NULL);
67  if (ret) {
68  av_strerror(ret, errbuf, sizeof(errbuf));
69  fprintf(stderr, "Unable to open %s: %s\n", output_url, errbuf);
70  goto fail;
71  }
72 
73  start_time = av_gettime();
74  while (1) {
75  uint8_t buf[1024];
76  int n;
77  n = avio_read(input, buf, sizeof(buf));
78  if (n <= 0)
79  break;
80  avio_write(output, buf, n);
81  stream_pos += n;
82  if (bps) {
83  avio_flush(output);
84  while ((av_gettime() - start_time) * bps / AV_TIME_BASE < stream_pos)
85  av_usleep(50 * 1000);
86  }
87  }
88 
89  avio_flush(output);
90  avio_close(output);
91 
92 fail:
93  avio_close(input);
95  return ret ? 1 : 0;
96 }
Bytestream IO Context.
Definition: avio.h:68
#define AVIO_FLAG_READ
read-only
Definition: avio.h:332
#define AVIO_FLAG_WRITE
write-only
Definition: avio.h:333
int av_usleep(unsigned usec)
Sleep for a period of time.
Definition: time.c:56
int main(int argc, char **argv)
Definition: aviocat.c:33
static int64_t start_time
Definition: ffplay.c:293
int avformat_network_init(void)
Do global initialization of network components.
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:173
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:478
int avio_close(AVIOContext *s)
Close the resource accessed by the AVIOContext s and free it.
Definition: aviobuf.c:821
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame This method is called when a frame is wanted on an output For an input
int void avio_flush(AVIOContext *s)
Force flushing of buffered data to the output s.
Definition: aviobuf.c:193
#define AV_TIME_BASE
Internal time base represented as integer.
Definition: avutil.h:196
ret
Definition: avfilter.c:821
int64_t av_gettime(void)
Get the current time in microseconds.
Definition: time.c:39
int avformat_network_deinit(void)
Undo the initialization done by avformat_network_init.
NULL
Definition: eval.c:55
void * buf
Definition: avisynth_c.h:594
synthesis window for stochastic i
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:804
static int usage(const char *argv0, int ret)
Definition: aviocat.c:27
int av_strerror(int errnum, char *errbuf, size_t errbuf_size)
Put a description of the AVERROR code errnum in errbuf.
Definition: error.c:53
Main libavformat public API header.
these buffered frames must be flushed immediately if a new input produces new output(Example:frame rate-doubling filter:filter_frame must(1) flush the second copy of the previous frame, if it is still there,(2) push the first copy of the incoming frame,(3) keep the second copy for later.) If the input frame is not enough to produce output
unsigned bps
Definition: movenc.c:895
void av_register_all(void)
Initialize libavformat and register all the muxers, demuxers and protocols.
Definition: allformats.c:52