[FFmpeg-devel] [PATCH] Added an option (-ignore_empty_streams) to ignore output files that do not contain a stream and continue processing and write all the other output files that do have a stream.

Ryan Dingman rdingman at gmail.com
Tue Jun 18 09:30:44 CEST 2013


For example, we may have an input mp3 file for which we want to produce 3 outputs. The first output is to extract the metadata from the mp3, the second output to reencode the mp3 and the third output to extract the artwork from the video stream for the mp3.

The video stream containing the track artwork may or maynot exist. So, the following invocation of ffmpeg will not write anything to the file artwork.jpg. Additionally, it will not write anything to metadata.txt or output.mp3 even though it would otherwise be able to successfully write the output for these files.

ffmpeg -y -i input.mp3 -an -f ffmetadata metadata.txt -acodec libmp3lame -ab 160k output.mp3 -an -f image2pipe -vcodec copy artwork.jpg

So, the -ignore_empty_streams option allows ffmpeg to skip any output that does not contain a stream and still write all the rest of the output files for which there is a stream. The following invocation of ffmpeg is similar to the previous invocation of ffmpeg in that they both will not write anything to artwork.jpg because there is not video stream containing the mp3 artwork. However, it differs from the pervious invocation in that it will continue processing and write both metadata.txt and output.mp3 because it can succesfully process those.

ffmpeg -ignore_empty_streams -y -i input.mp3 -an -f ffmetadata metadata.txt -acodec libmp3lame -ab 160k output.mp3 -an -f image2pipe -vcodec copy artwork.jpg

Signed-off-by: Ryan Dingman <rdingman at gmail.com>
---
 ffmpeg.c     | 9 ++++++++-
 ffmpeg.h     | 1 +
 ffmpeg_opt.c | 3 +++
 3 files changed, 12 insertions(+), 1 deletion(-)

diff --git a/ffmpeg.c b/ffmpeg.c
index e199a10..5345923 100644
--- a/ffmpeg.c
+++ b/ffmpeg.c
@@ -2098,7 +2098,10 @@ static int transcode_init(void)
         if (!oc->nb_streams && !(oc->oformat->flags & AVFMT_NOSTREAMS)) {
             av_dump_format(oc, i, oc->filename, 1);
             av_log(NULL, AV_LOG_ERROR, "Output file #%d does not contain any stream\n", i);
-            return AVERROR(EINVAL);
+
+            if (!ignore_empty_streams) {
+                return AVERROR(EINVAL);
+            }
         }
     }
 
@@ -2483,6 +2486,10 @@ static int transcode_init(void)
     /* open files and write file headers */
     for (i = 0; i < nb_output_files; i++) {
         oc = output_files[i]->ctx;
+        if (ignore_empty_streams && !oc->nb_streams && !(oc->oformat->flags & AVFMT_NOSTREAMS)) {
+            continue;
+        }
+
         oc->interrupt_callback = int_cb;
         if ((ret = avformat_write_header(oc, &output_files[i]->opts)) < 0) {
             char errbuf[128];
diff --git a/ffmpeg.h b/ffmpeg.h
index 24e6d47..1c04412 100644
--- a/ffmpeg.h
+++ b/ffmpeg.h
@@ -415,6 +415,7 @@ extern int print_stats;
 extern int qp_hist;
 extern int stdin_interaction;
 extern int frame_bits_per_raw_sample;
+extern int ignore_empty_streams;
 extern AVIOContext *progress_avio;
 
 extern const AVIOInterruptCB int_cb;
diff --git a/ffmpeg_opt.c b/ffmpeg_opt.c
index 2aff601..5f49e3f 100644
--- a/ffmpeg_opt.c
+++ b/ffmpeg_opt.c
@@ -84,6 +84,7 @@ int print_stats       = -1;
 int qp_hist           = 0;
 int stdin_interaction = 1;
 int frame_bits_per_raw_sample = 0;
+int ignore_empty_streams      = 0;
 
 
 static int intra_only         = 0;
@@ -2565,6 +2566,8 @@ const OptionDef options[] = {
         "force format", "fmt" },
     { "y",              OPT_BOOL,                                    {              &file_overwrite },
         "overwrite output files" },
+    { "ignore_empty_streams",              OPT_BOOL,                        {              &ignore_empty_streams },
+        "continue processing even if one or more output files do not contain a stream" },
     { "n",              OPT_BOOL,                                    {              &no_file_overwrite },
         "do not overwrite output files" },
     { "c",              HAS_ARG | OPT_STRING | OPT_SPEC |
-- 
1.8.2.1 (Apple Git-45)



More information about the ffmpeg-devel mailing list