aboutsummaryrefslogtreecommitdiffstats
path: root/fftools/ffmpeg_mux.c
diff options
context:
space:
mode:
authorAnton Khirnov <anton@khirnov.net>2023-03-28 10:26:15 +0200
committerAnton Khirnov <anton@khirnov.net>2023-04-13 15:11:56 +0200
commit79e136f14b4f2ef9afdc5cf81fb15f0a4cec0950 (patch)
tree5bb990260463e4f6f7b15bdc1fdd232ccb8c78ba /fftools/ffmpeg_mux.c
parent5cf81bed881f2aad6f3fec441129d050338b90d7 (diff)
downloadffmpeg-79e136f14b4f2ef9afdc5cf81fb15f0a4cec0950.tar.gz
fftools/ffmpeg: factorize checking whether any output was written
This is currently done in two places: * at the end of print_final_stats(), which merely prints a warning if the total size of all written packets is zero * at the end of transcode() (under a misleading historical 'close each encoder' comment), which instead checks the packet count to implement -abort_on empty_output[_stream] Consolidate both of these blocks into a single function called from of_write_trailer(), which is a more appropriate place for this. Also, return an error code rather than exit immediately, which ensures all output files are properly closed.
Diffstat (limited to 'fftools/ffmpeg_mux.c')
-rw-r--r--fftools/ffmpeg_mux.c43
1 files changed, 43 insertions, 0 deletions
diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c
index c2b7993e66..9a91861bd4 100644
--- a/fftools/ffmpeg_mux.c
+++ b/fftools/ffmpeg_mux.c
@@ -598,6 +598,45 @@ int of_stream_init(OutputFile *of, OutputStream *ost)
return mux_check_init(mux);
}
+static int check_written(OutputFile *of)
+{
+ int64_t total_packets_written = 0;
+ int pass1_used = 1;
+ int ret = 0;
+
+ for (int i = 0; i < of->nb_streams; i++) {
+ OutputStream *ost = of->streams[i];
+ uint64_t packets_written = atomic_load(&ost->packets_written);
+
+ total_packets_written += packets_written;
+
+ if (ost->enc_ctx &&
+ (ost->enc_ctx->flags & (AV_CODEC_FLAG_PASS1 | AV_CODEC_FLAG_PASS2))
+ != AV_CODEC_FLAG_PASS1)
+ pass1_used = 0;
+
+ if (!packets_written &&
+ (abort_on_flags & ABORT_ON_FLAG_EMPTY_OUTPUT_STREAM)) {
+ av_log(ost, AV_LOG_FATAL, "Empty output stream\n");
+ ret = err_merge(ret, AVERROR(EINVAL));
+ }
+ }
+
+ if (!total_packets_written) {
+ int level = AV_LOG_WARNING;
+
+ if (abort_on_flags & ABORT_ON_FLAG_EMPTY_OUTPUT) {
+ ret = err_merge(ret, AVERROR(EINVAL));
+ level = AV_LOG_FATAL;
+ }
+
+ av_log(of, level, "Output file is empty, nothing was encoded%s\n",
+ pass1_used ? "" : "(check -ss / -t / -frames parameters if used)");
+ }
+
+ return ret;
+}
+
int of_write_trailer(OutputFile *of)
{
Muxer *mux = mux_from_of(of);
@@ -629,6 +668,10 @@ int of_write_trailer(OutputFile *of)
}
}
+ // check whether anything was actually written
+ ret = check_written(of);
+ mux_result = err_merge(mux_result, ret);
+
return mux_result;
}