[FFmpeg-devel] [PATCH] tools/aviocat: support getopt() interface

Stefano Sabatini stefasab at gmail.com
Thu Jan 31 21:06:48 CET 2013


Simplify pending change, extend flexibility.
---
 tools/aviocat.c |   56 +++++++++++++++++++++++++++++++++++++++++++++----------
 1 file changed, 46 insertions(+), 10 deletions(-)

diff --git a/tools/aviocat.c b/tools/aviocat.c
index 52a96bd..f9c366a 100644
--- a/tools/aviocat.c
+++ b/tools/aviocat.c
@@ -18,15 +18,33 @@
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  */
 
+#include "config.h"
+
 #include <stdio.h>
 #include <stdlib.h>
 
+#if HAVE_UNISTD_H
+#include <unistd.h>             /* getopt */
+#endif
+
 #include "libavutil/time.h"
 #include "libavformat/avformat.h"
 
+#if !HAVE_GETOPT
+#include "compat/getopt.c"
+#endif
+
 static int usage(const char *argv0, int ret)
 {
-    fprintf(stderr, "%s [-b bytespersec] input_url output_url\n", argv0);
+    printf("Read and write a file using the libavformat I/O facilities\n");
+    printf("usage: %s [options] [input_url] [output_url]\n", argv0);
+
+    printf("\n"
+           "Options:\n"
+           "-b                set bytes per second\n"
+           "-h                print this help\n"
+           "-i INPUT_URL      set INPUT_URL\n"
+           "-o OUTFILE        set OUTFILE as output file\n");
     return ret;
 }
 
@@ -36,26 +54,44 @@ int main(int argc, char **argv)
     const char *input_url = NULL, *output_url = NULL;
     int64_t stream_pos = 0;
     int64_t start_time;
-    char errbuf[50];
+    char errbuf[50], c;
     AVIOContext *input, *output;
 
     av_register_all();
     avformat_network_init();
 
-    for (i = 1; i < argc; i++) {
-        if (!strcmp(argv[i], "-b")) {
-            bps = atoi(argv[i + 1]);
-            i++;
-        } else if (!input_url) {
+    while ((c = getopt(argc, argv, "b:hi:o:")) != -1) {
+        switch (c) {
+        case 'b':
+            bps = atoi(optarg);
+            break;
+        case 'h':
+            return usage(argv[0], 0);
+        case 'i':
+            input_url = optarg;
+            break;
+        case 'o':
+            output_url = optarg;
+            break;
+        case '?':
+            return usage(argv[0], 1);
+        }
+    }
+
+    for (i = optind; i < argc; i++) {
+        if (!input_url) {
             input_url = argv[i];
         } else if (!output_url) {
             output_url = argv[i];
         } else {
-            return usage(argv[0], 1);
+            fprintf(stderr, "Too many arguments provided\n");
+            return 1;
         }
     }
-    if (!output_url)
-        return usage(argv[0], 1);
+    if (!output_url) {
+        fprintf(stderr, "Input or output was not specified\n");
+        return 1;
+    }
 
     ret = avio_open2(&input, input_url, AVIO_FLAG_READ, NULL, NULL);
     if (ret) {
-- 
1.7.9.5



More information about the ffmpeg-devel mailing list