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 #include <unistd.h>
24 #include "libavformat/avformat.h"
25 
26 static int usage(const char *argv0, int ret)
27 {
28  fprintf(stderr, "%s [-b bytespersec] input_url output_url\n", argv0);
29  return ret;
30 }
31 
32 int main(int argc, char **argv)
33 {
34  int bps = 0, ret, i;
35  const char *input_url = NULL, *output_url = NULL;
36  int64_t stream_pos = 0;
37  int64_t start_time;
38  char errbuf[50];
39  AVIOContext *input, *output;
40 
43 
44  for (i = 1; i < argc; i++) {
45  if (!strcmp(argv[i], "-b")) {
46  bps = atoi(argv[i + 1]);
47  i++;
48  } else if (!input_url) {
49  input_url = argv[i];
50  } else if (!output_url) {
51  output_url = argv[i];
52  } else {
53  return usage(argv[0], 1);
54  }
55  }
56  if (!output_url)
57  return usage(argv[0], 1);
58 
59  ret = avio_open2(&input, input_url, AVIO_FLAG_READ, NULL, NULL);
60  if (ret) {
61  av_strerror(ret, errbuf, sizeof(errbuf));
62  fprintf(stderr, "Unable to open %s: %s\n", input_url, errbuf);
63  return 1;
64  }
65  ret = avio_open2(&output, output_url, AVIO_FLAG_WRITE, NULL, NULL);
66  if (ret) {
67  av_strerror(ret, errbuf, sizeof(errbuf));
68  fprintf(stderr, "Unable to open %s: %s\n", output_url, errbuf);
69  goto fail;
70  }
71 
72  start_time = av_gettime();
73  while (1) {
74  uint8_t buf[1024];
75  int n;
76  n = avio_read(input, buf, sizeof(buf));
77  if (n <= 0)
78  break;
79  avio_write(output, buf, n);
80  stream_pos += n;
81  if (bps) {
82  avio_flush(output);
83  while ((av_gettime() - start_time)*bps/AV_TIME_BASE < stream_pos)
84  usleep(50*1000);
85  }
86  }
87 
88  avio_flush(output);
89  avio_close(output);
90 fail:
91  avio_close(input);
93  return ret ? 1 : 0;
94 }
Bytestream IO Context.
Definition: avio.h:68
#define AVIO_FLAG_READ
read-only
Definition: avio.h:565
#define AVIO_FLAG_WRITE
write-only
Definition: avio.h:566
int main(int argc, char **argv)
Definition: aviocat.c:32
int avformat_network_init(void)
Do global initialization of network components.
Definition: utils.c:4056
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:190
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:652
static int64_t start_time
Definition: avplay.c:240
int avio_close(AVIOContext *s)
Close the resource accessed by the AVIOContext s and free it.
Definition: aviobuf.c:987
int void avio_flush(AVIOContext *s)
Definition: aviobuf.c:205
#define AV_TIME_BASE
Internal time base represented as integer.
Definition: avutil.h:277
int avformat_network_deinit(void)
Undo the initialization done by avformat_network_init.
Definition: utils.c:4068
NULL
Definition: eval.c:50
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:970
static int usage(const char *argv0, int ret)
Definition: aviocat.c:26
int av_strerror(int errnum, char *errbuf, size_t errbuf_size)
Put a description of the AVERROR code errnum in errbuf.
Definition: error.c:22
Main libavformat public API header.
unsigned bps
Definition: movenc.c:671
#define fprintf
Definition: internal.h:153
void av_register_all(void)
Initialize libavformat and register all the muxers, demuxers and protocols.
Definition: allformats.c:40
int64_t av_gettime(void)
Get the current time in microseconds.
Definition: utils.c:3531