diff --git a/ffmpeg.c b/ffmpeg.c
index bd59534..09c5bb6 100644
--- a/ffmpeg.c
+++ b/ffmpeg.c
@@ -609,6 +609,15 @@ static void *grow_array(void *array, int elem_size, int *size, int new_size)
     return array;
 }
 
+static void log_callback_errorfilter(void* ptr, int level, const char* fmt, va_list vl)
+{
+    av_log_default_callback(ptr, level, fmt, vl);
+
+    /* level == AV_LOG_FATAL already caught */
+    if(exit_on_error && level == AV_LOG_ERROR)
+        ffmpeg_exit(1);
+}
+
 static void choose_sample_fmt(AVStream *st, AVCodec *codec)
 {
     if(codec && codec->sample_fmts){
@@ -4558,6 +4567,11 @@ int main(int argc, char **argv)
     if(verbose>=0)
         show_banner();
 
+    /* parse_options opens input files and might find errors;
+       this way, trigger if -xerror occurs before relevant -i */
+    if (!run_as_daemon)
+        av_log_set_callback(log_callback_errorfilter);
+
     /* parse options */
     parse_options(argc, argv, options, opt_output_file);
 
