• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • 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 
00025 #include "libavformat/avformat.h"
00026 
00027 static int usage(const char *argv0, int ret)
00028 {
00029     fprintf(stderr, "%s [-b bytespersec] input_url output_url\n", argv0);
00030     return ret;
00031 }
00032 
00033 int main(int argc, char **argv)
00034 {
00035     int bps = 0, ret, i;
00036     const char *input_url = NULL, *output_url = NULL;
00037     int64_t stream_pos = 0;
00038     int64_t start_time;
00039     char errbuf[50];
00040     AVIOContext *input, *output;
00041 
00042     av_register_all();
00043     avformat_network_init();
00044 
00045     for (i = 1; i < argc; i++) {
00046         if (!strcmp(argv[i], "-b")) {
00047             bps = atoi(argv[i + 1]);
00048             i++;
00049         } else if (!input_url) {
00050             input_url = argv[i];
00051         } else if (!output_url) {
00052             output_url = argv[i];
00053         } else {
00054             return usage(argv[0], 1);
00055         }
00056     }
00057     if (!output_url)
00058         return usage(argv[0], 1);
00059 
00060     ret = avio_open2(&input, input_url, AVIO_FLAG_READ, NULL, NULL);
00061     if (ret) {
00062         av_strerror(ret, errbuf, sizeof(errbuf));
00063         fprintf(stderr, "Unable to open %s: %s\n", input_url, errbuf);
00064         return 1;
00065     }
00066     ret = avio_open2(&output, output_url, AVIO_FLAG_WRITE, NULL, NULL);
00067     if (ret) {
00068         av_strerror(ret, errbuf, sizeof(errbuf));
00069         fprintf(stderr, "Unable to open %s: %s\n", output_url, errbuf);
00070         goto fail;
00071     }
00072 
00073     start_time = av_gettime();
00074     while (1) {
00075         uint8_t buf[1024];
00076         int n;
00077         n = avio_read(input, buf, sizeof(buf));
00078         if (n <= 0)
00079             break;
00080         avio_write(output, buf, n);
00081         stream_pos += n;
00082         if (bps) {
00083             avio_flush(output);
00084             while ((av_gettime() - start_time) * bps / AV_TIME_BASE < stream_pos)
00085                 usleep(50 * 1000);
00086         }
00087     }
00088 
00089     avio_flush(output);
00090     avio_close(output);
00091 
00092 fail:
00093     avio_close(input);
00094     avformat_network_deinit();
00095     return ret ? 1 : 0;
00096 }
Generated on Fri Feb 1 2013 14:34:57 for FFmpeg by doxygen 1.7.1