• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

tools/aviocat.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2012 Martin Storsjo
00003  *
00004  * This file is part of Libav.
00005  *
00006  * Libav is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Lesser General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2.1 of the License, or (at your option) any later version.
00010  *
00011  * Libav is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Lesser General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License along with Libav; if not, write to the Free Software
00018  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00019  */
00020 
00021 #include <stdio.h>
00022 #include <stdlib.h>
00023 #include <unistd.h>
00024 #include "libavformat/avformat.h"
00025 
00026 static int usage(const char *argv0, int ret)
00027 {
00028     fprintf(stderr, "%s [-b bytespersec] input_url output_url\n", argv0);
00029     return ret;
00030 }
00031 
00032 int main(int argc, char **argv)
00033 {
00034     int bps = 0, ret, i;
00035     const char *input_url = NULL, *output_url = NULL;
00036     int64_t stream_pos = 0;
00037     int64_t start_time;
00038     char errbuf[50];
00039     AVIOContext *input, *output;
00040 
00041     av_register_all();
00042     avformat_network_init();
00043 
00044     for (i = 1; i < argc; i++) {
00045         if (!strcmp(argv[i], "-b")) {
00046             bps = atoi(argv[i + 1]);
00047             i++;
00048         } else if (!input_url) {
00049             input_url = argv[i];
00050         } else if (!output_url) {
00051             output_url = argv[i];
00052         } else {
00053             return usage(argv[0], 1);
00054         }
00055     }
00056     if (!output_url)
00057         return usage(argv[0], 1);
00058 
00059     ret = avio_open2(&input, input_url, AVIO_FLAG_READ, NULL, NULL);
00060     if (ret) {
00061         av_strerror(ret, errbuf, sizeof(errbuf));
00062         fprintf(stderr, "Unable to open %s: %s\n", input_url, errbuf);
00063         return 1;
00064     }
00065     ret = avio_open2(&output, output_url, AVIO_FLAG_WRITE, NULL, NULL);
00066     if (ret) {
00067         av_strerror(ret, errbuf, sizeof(errbuf));
00068         fprintf(stderr, "Unable to open %s: %s\n", output_url, errbuf);
00069         goto fail;
00070     }
00071 
00072     start_time = av_gettime();
00073     while (1) {
00074         uint8_t buf[1024];
00075         int n;
00076         n = avio_read(input, buf, sizeof(buf));
00077         if (n <= 0)
00078             break;
00079         avio_write(output, buf, n);
00080         stream_pos += n;
00081         if (bps) {
00082             avio_flush(output);
00083             while ((av_gettime() - start_time)*bps/AV_TIME_BASE < stream_pos)
00084                 usleep(50*1000);
00085         }
00086     }
00087 
00088     avio_flush(output);
00089     avio_close(output);
00090 fail:
00091     avio_close(input);
00092     avformat_network_deinit();
00093     return ret ? 1 : 0;
00094 }
Generated on Sun Apr 22 2012 21:54:09 for Libav by doxygen 1.7.1