>From bd86133c3ddfc0904becffe3676ce04c4951de9d Mon Sep 17 00:00:00 2001
From: KangLin <kl222@126.com>
Date: Thu, 26 May 2016 11:45:01 +0800
Subject: [PATCH 4/4] ffmpeg: read interaction command from a file.

Signed-off-by: KangLin <kl222@126.com>
---
 ffmpeg.c     | 29 +++++++++++++++++++++++++++--
 ffmpeg.h     |  2 ++
 ffmpeg_opt.c |  2 ++
 3 files changed, 31 insertions(+), 2 deletions(-)

diff --git a/ffmpeg.c b/ffmpeg.c
index 7c60075..b6c087d 100644
--- a/ffmpeg.c
+++ b/ffmpeg.c
@@ -147,6 +147,9 @@ int         nb_output_files   = 0;
 FilterGraph **filtergraphs;
 int        nb_filtergraphs;
 
+FILE* g_command_config_file = NULL;
+char* g_command_config_filename = "CommandConfigFile.txt";
+
 #if HAVE_TERMIOS_H
 
 /* init terminal so that we can grab keys */
@@ -400,7 +403,29 @@ void term_init(void)
 /* read a key without blocking */
 static int read_key(void)
 {
-    unsigned char ch;
+    char ch;
+    if (!g_command_config_file && g_command_config_filename)
+        g_command_config_file = fopen(g_command_config_filename, "rt");
+    if (g_command_config_file){
+        ch = getc(g_command_config_file);
+        if (EOF == ch){
+            if (0 != fclose(g_command_config_file)){
+                av_log(NULL, AV_LOG_ERROR, "close file faile:%s\n", g_command_config_filename);
+            }
+            if (0 != remove(g_command_config_filename)){
+                av_log(NULL, AV_LOG_ERROR, "remove file faile:%s\n", g_command_config_filename);
+            }
+
+            g_command_config_file = NULL;
+            return '\n';
+        }
+        else
+            return ch;
+    }
+
+    if(run_as_daemon)
+        return -1;
+
 #if HAVE_TERMIOS_H
     int n = 1;
     struct timeval tv;
@@ -3450,7 +3475,7 @@ static int check_keyboard_interaction(int64_t cur_time)
     if (received_nb_signals)
         return AVERROR_EXIT;
     /* read_key() returns 0 on EOF */
-    if(cur_time - last_time >= 100000 && !run_as_daemon){
+    if(cur_time - last_time >= 100000){
         key =  read_key();
         last_time = cur_time;
     }else
diff --git a/ffmpeg.h b/ffmpeg.h
index 20a1bf7..40cc9fd 100644
--- a/ffmpeg.h
+++ b/ffmpeg.h
@@ -515,6 +515,8 @@ extern int         nb_output_files;
 extern FilterGraph **filtergraphs;
 extern int        nb_filtergraphs;
 
+extern char* g_command_config_filename;
+
 extern char *vstats_filename;
 extern char *sdp_filename;
 
diff --git a/ffmpeg_opt.c b/ffmpeg_opt.c
index c217462..835ccf2 100644
--- a/ffmpeg_opt.c
+++ b/ffmpeg_opt.c
@@ -3129,6 +3129,8 @@ const OptionDef options[] = {
         "force format", "fmt" },
     { "y",              OPT_BOOL,                                    {              &file_overwrite },
         "overwrite output files" },
+    { "ic",           HAS_ARG | OPT_STRING,                          {              &g_command_config_filename },
+        "read interaction command from a file. default is CommandConfigFile.txt", "filename" },
     { "n",              OPT_BOOL,                                    {              &no_file_overwrite },
         "never overwrite output files" },
     { "ignore_unknown", OPT_BOOL,                                    {              &ignore_unknown_streams },
-- 
2.6.3

