]> git.sesse.net Git - ffmpeg/commitdiff
avfilter/asrc_anullsrc: add support to set output duration
authorPaul B Mahol <onemda@gmail.com>
Fri, 4 Sep 2020 16:30:46 +0000 (18:30 +0200)
committerPaul B Mahol <onemda@gmail.com>
Fri, 4 Sep 2020 17:37:29 +0000 (19:37 +0200)
doc/filters.texi
libavfilter/asrc_anullsrc.c

index ebe0d3301e32f6200d6588de697679a58b49a46b..d0ff3d470fb154eaf8d3fe6796744fa12fc1b4e5 100644 (file)
@@ -6076,6 +6076,13 @@ Specifies the sample rate, and defaults to 44100.
 @item nb_samples, n
 Set the number of samples per requested frames.
 
+@item duration, d
+Set the duration of the sourced audio. See
+@ref{time duration syntax,,the Time duration section in the ffmpeg-utils(1) manual,ffmpeg-utils}
+for the accepted syntax.
+
+If not specified, or the expressed duration is negative, the audio is
+supposed to be generated forever.
 @end table
 
 @subsection Examples
index 7093f08275380033289f67178d6a228e07674f24..ab56d844242813d575020c8cf1f9c290a94b5f56 100644 (file)
@@ -40,6 +40,7 @@ typedef struct ANullContext {
     uint64_t channel_layout;
     char   *sample_rate_str;
     int     sample_rate;
+    int64_t duration;
     int nb_samples;             ///< number of samples per requested frame
     int64_t pts;
 } ANullContext;
@@ -54,6 +55,8 @@ static const AVOption anullsrc_options[]= {
     { "r",              "set sample rate",    OFFSET(sample_rate_str)   , AV_OPT_TYPE_STRING, {.str = "44100"}, 0, 0, FLAGS },
     { "nb_samples",     "set the number of samples per requested frame", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.i64 = 1024}, 0, INT_MAX, FLAGS },
     { "n",              "set the number of samples per requested frame", OFFSET(nb_samples), AV_OPT_TYPE_INT, {.i64 = 1024}, 0, INT_MAX, FLAGS },
+    { "duration",       "set the audio duration",                        OFFSET(duration),   AV_OPT_TYPE_DURATION, {.i64 = -1}, -1, INT64_MAX, FLAGS },
+    { "d",              "set the audio duration",                        OFFSET(duration),   AV_OPT_TYPE_DURATION, {.i64 = -1}, -1, INT64_MAX, FLAGS },
     { NULL }
 };
 
@@ -108,6 +111,10 @@ static int request_frame(AVFilterLink *outlink)
     ANullContext *null = outlink->src->priv;
     AVFrame *samplesref;
 
+    if (null->duration >= 0 &&
+        av_rescale_q(null->pts, outlink->time_base, AV_TIME_BASE_Q) >= null->duration)
+        return AVERROR_EOF;
+
     samplesref = ff_get_audio_buffer(outlink, null->nb_samples);
     if (!samplesref)
         return AVERROR(ENOMEM);