Answers.Body: <p>It's a terrible hack, but this might work if all you want to do is audio fade in/out but don't know exactly how long the clip is:</p>
  
  <pre><code>ffmpeg -i input.mp4 -filter_complex "afade=d=0.5, areverse, afade=d=0.5, areverse" output.mp4
  </code></pre>
  
  <p>[edit 2019-07-24:  Note that this solution is not good for streaming solutions since it requires the full track to be processed before the first byte can be streamed]</p>
  
  <p>another option I is to use <code>acrossfade</code> with a silent track:</p>
  
  <pre><code>ffmpeg -i input.mp4 -filter_complex "aevalsrc=0:d=0.6 [a_silence]; [0:a:0] [a_silence] acrossfade=d=0.6" output.mp4
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-08-30T19:30:04.653Z
Answers.Text: This is actually pretty elegant :-)
Answers.UserDisplayName: bk138
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-07-21T16:23:11.857Z
Answers.Text: this works great. could you explain how it works. trying to make sense of the documentation..
Answers.UserDisplayName: oberhamsi
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-07-24T21:22:58.923Z
Answers.Text: @oberhamsi: Option 1: The first filter adds a fade at the beginning of the track. It then reverse the audio and adds another fade in at the beginning (which is actually the end). Finally the track is reversed again. Option 2 creates a silent track of 0.6 seconds and crossfades it for the same amount of time with the original track. All the filters and their usage are documented here: https://ffmpeg.org/ffmpeg-filters.html (although I have to agree that it's a bit cryptic)
Answers.UserDisplayName: marekventur
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-03-26T01:44:46.557Z
Answers.Text: What would be the script to merge Video + Audio (Audio is longer) and have fade in and our for the audio (Let's say Fade In finishes after 2 seconds, Fade out starts at 48 and ends at 51)?
Answers.UserDisplayName: Royi
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2017-08-15T14:31:38.270Z
Answers.LastActivityDate: 2019-07-24T21:26:06.217Z
Answers.LastEditDate: 2019-07-24T21:26:06.217Z
Answers.UserDisplayName: marekventur
Answers.Body: <p>The answer to question 2 is a resounding YES! I was looking for the same functionality and I ended up writing a bash script that asks for fade duration in seconds and calculates the initial frame for the fade-out:</p>
  
  <pre><code>#!/bin/bash
  
  f="$*" # all args
  p="${f##*/}" # file
  fn="${p%.*}" # name only
  e="${p##*.}" # extension
  
  echo
  echo $f
  echo $p
  echo $fn
  echo $e
  echo
  
  read -e -p "Number of seconds of fade-out: " -i 1 sec # prompt for fade duration in seconds
  
  r=$(ffprobe -show_streams "$f" 2&gt; /dev/null | grep r_frame_rate | head -1 | cut -d \= -f 2) # frame rate
  let "frames = $r * $sec" # duration of fade as number of frames
  echo $r
  echo $sec
  echo $frames
  lf=$(ffprobe -show_streams "$f" 2&gt; /dev/null | grep nb_frames | head -1 | cut -d \= -f 2) # total number of frames
  let "lf = $lf - $frames" # initial frame to start fade at
  
  ffmpeg -loglevel quiet -i "$f" -vf fade=out:$lf:$frames -r $r -vcodec libx264 -preset slow -crf 12 -bf 2 -flags +cgop -pix_fmt yuv420p -acodec copy "$fn $sec sec fade-out.$e"
  open "$fn $sec sec fade-out.$e"
  </code></pre>
  
  <p>Fades require re-encoding so using a low <code>crf</code> value for <code>libx264</code> gives a high quality re-encode. The comments should explain everything else.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-05-07T04:58:24.050Z
Answers.Text: *The answer to question 2 is a resounding YES!* --> it's still No. The Q is *Can ffmpeg automatically figure out the duration...* but you've written a script that runs multiple ff* commands to do it. Useful for *nix systems, though.
Answers.UserDisplayName: Gyan
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-05-07T15:32:57.827Z
Answers.Text: I see, so you insist that YOUR answer is STILL correct? If you follow my script, you will see that it does indeed `automatically figure out the duration` using `ffprobe`, which is installed with `ffmpeg`. Who said anything about any restriction on the number of commands or `*nix`? It seems that the OP is using a version of UNIX. No `.exe` in the `ffmpeg` command. I myself am using `OS X` which is built on `UNIX` but is not `UNIX`.
Answers.UserDisplayName: hmj6jmh
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-05-07T15:42:25.797Z
Answers.Text: * If you follow my script, you will see that it does indeed automatically figure out* --> your script does, ffmpeg does not.
Answers.UserDisplayName: Gyan
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-05-07T15:47:05.947Z
Answers.Text: But `ffprobe` is an integral part of the `ffmpeg` distribution. You're picking at nits to save face IMO.
Answers.UserDisplayName: hmj6jmh
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-12-03T18:45:42.637Z
Answers.Text: I'd have to agree that something being done by a bash script is *not* something being automatically done by ffmpeg.  But if this works, this is a way to work around that problem.  Hence the term "workaround".  I can't say if it works because I, like most people out there, don't have bash or a unix system.  +1 for the effort anyhow.
Answers.UserDisplayName: ashleedawg
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-05-07T02:48:59.323Z
Answers.LastActivityDate: 2017-05-07T02:48:59.323Z
Answers.UserDisplayName: hmj6jmh
Answers.Body: <p><strong>#1</strong> To simultaneously fade the audio in/out:</p>
  
  <pre><code>ffmpeg -i clip.mp4 -vf 'fade=in:0:30,fade=out:960:30'
                     -af 'afade=in:st=0:d=1,afade=out:st=32:d=1'
         -c:v libx264 -crf 22 -preset veryfast fadeInOut.mp4
  </code></pre>
  
  <p>The afade times are in <em>seconds</em>.</p>
  
  <p><strong>#2</strong> Automatically? No. But see workaround below</p>
  
  <p>You can first run ffprobe to get duration.</p>
  
  <pre><code>ffprobe -i clip.mp4 -show_entries stream=codec_type,duration -of compact=p=0:nk=1
  </code></pre>
  
  <p>You'll get something like this:</p>
  
  <pre><code>video|13.556000
  audio|13.816000
  </code></pre>
  
  <p>You can then use the above to place your fades. These times are in seconds.</p>
  
  <p><strong>Workaround</strong></p>
  
  <pre><code>ffmpeg -i clip.mp4 -sseof -1 -copyts -i clip.mp4 -filter_complex
         "[1]fade=out:0:30[t];[0][t]overlay,fade=in:0:30[v];
          anullsrc,atrim=0:2[at];[0][at]acrossfade=d=1,afade=d=1[a]"
         -map "[v]" -map "[a]" -c:v libx264 -crf 22 -preset veryfast -shortest fadeInOut.mp4
  </code></pre>
  
  <p>FFmpeg has a <code>sseof</code> option that allows one to seek an input from the end. We can use that to accomplish our goal. So we feed the input twice, with the 2nd time ingesting only the last second. We tell FFmpeg to preserve the timestamps, so that ffmpeg preserves the temporal position of this tail portion. </p>
  
  <p>We apply a fade out to this tail and then overlay the result onto the full input. Since they are the same media file, the foreground completely covers the background, and since <code>copyts</code> was applied, the overlay happens upon the corresponding identical frame in the background input.</p>
  
  <p>For audio, we create a blank dummy audio of duration 2 seconds, and then apply an audio crossfade from the main audio to this dummy audio. Since the 2nd audio is blank, this is, in effect, a fade-out for the main input. The <code>-shortest</code> is added to leave out portions of the dummy audio after the crossfade has occurred.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-12-06T16:55:09.727Z
Answers.Text: This gives the error "no such filter!" for the audio filter.
Answers.UserDisplayName: felwithe
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-12-06T17:18:50.640Z
Answers.Text: There are 4 audio filters used. Which one? Run your command with `-report` added and share the report.
Answers.UserDisplayName: Gyan
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-11-17T19:51:03.647Z
Answers.LastActivityDate: 2017-05-08T05:41:18.407Z
Answers.LastEditDate: 2017-05-08T05:41:18.407Z
Answers.UserDisplayName: Gyan
Body: <p>I'm working on a project where I'm using ffmpeg to batch convert some video files, and adding fade in/out effects at the head and tail of each clip.</p>
  
  <p>I've been using this bit of code to add the fade/out effect on clips with known durations:</p>
  
  <p><code>
  ffmpeg -i clip.mp4 -filter:v 'fade=in:0:30,fade=out:9650:30' -c:v libx264 -crf 22 -preset veryfast -c:a copy fadeInOut.mp4
  </code></p>
  
  <p>My two questions are:</p>
  
  <ol>
  <li>How can I simultaneously fade the audio in/out?</li>
  <li>Can ffmpeg automatically figure out the duration of the clip and be told just fade out the last 30 frames?</li>
  </ol>
  
  <p>Thanks!</p>
  
Tags: <video><ffmpeg><audio>
Title: How to fade in/out a video/audio clip with unknown duration?
UserDisplayName: d13
ThreadText: # How to fade in/out a video/audio clip with unknown duration?
  
  (Asked by: username_0 on 2016-11-17)
  
  I'm working on a project where I'm using ffmpeg to batch convert some video files, and adding fade in/out effects at the head and tail of each clip.
  
  I've been using this bit of code to add the fade/out effect on clips with known durations:
  
  `ffmpeg -i clip.mp4 -filter:v 'fade=in:0:30,fade=out:9650:30' -c:v libx264 -crf 22 -preset veryfast -c:a copy fadeInOut.mp4`
  
  My two questions are:
  
  1. How can I simultaneously fade the audio in/out?
  2. Can ffmpeg automatically figure out the duration of the clip and be told just fade out the last 30 frames?
  
  Thanks!
  
  **Tags:** video, ffmpeg, audio
  
  
  ---
  
  ## 3 answers
  
  ### Answer by username_6 on 2016-11-17. Score: 25 | Accepted answer
  
  **#1** To simultaneously fade the audio in/out:
  
  ```
  ffmpeg -i clip.mp4 -vf 'fade=in:0:30,fade=out:960:30'
                     -af 'afade=in:st=0:d=1,afade=out:st=32:d=1'
         -c:v libx264 -crf 22 -preset veryfast fadeInOut.mp4
  
  ```
  
  The afade times are in *seconds*.
  
  **#2** Automatically? No. But see workaround below
  
  You can first run ffprobe to get duration.
  
  ```
  ffprobe -i clip.mp4 -show_entries stream=codec_type,duration -of compact=p=0:nk=1
  
  ```
  
  You'll get something like this:
  
  ```
  video|13.556000
  audio|13.816000
  
  ```
  
  You can then use the above to place your fades. These times are in seconds.
  
  **Workaround**
  
  ```
  ffmpeg -i clip.mp4 -sseof -1 -copyts -i clip.mp4 -filter_complex
         "[1]fade=out:0:30[t];[0][t]overlay,fade=in:0:30[v];
          anullsrc,atrim=0:2[at];[0][at]acrossfade=d=1,afade=d=1[a]"
         -map "[v]" -map "[a]" -c:v libx264 -crf 22 -preset veryfast -shortest fadeInOut.mp4
  
  ```
  
  FFmpeg has a `sseof` option that allows one to seek an input from the end. We can use that to accomplish our goal. So we feed the input twice, with the 2nd time ingesting only the last second. We tell FFmpeg to preserve the timestamps, so that ffmpeg preserves the temporal position of this tail portion.
  
  We apply a fade out to this tail and then overlay the result onto the full input. Since they are the same media file, the foreground completely covers the background, and since `copyts` was applied, the overlay happens upon the corresponding identical frame in the background input.
  
  For audio, we create a blank dummy audio of duration 2 seconds, and then apply an audio crossfade from the main audio to this dummy audio. Since the 2nd audio is blank, this is, in effect, a fade-out for the main input. The `-shortest` is added to leave out portions of the dummy audio after the crossfade has occurred.
  
  > **Comments:**
  > **username_8**: This gives the error "no such filter!" for the audio filter.  
  > **username_6**: There are 4 audio filters used. Which one? Run your command with `-report` added and share the report.  
  
  ---
  
  ### Answer by username_1 on 2017-08-15. Score: 15
  
  It's a terrible hack, but this might work if all you want to do is audio fade in/out but don't know exactly how long the clip is:
  
  ```
  ffmpeg -i input.mp4 -filter_complex "afade=d=0.5, areverse, afade=d=0.5, areverse" output.mp4
  
  ```
  
  [edit 2019-07-24: Note that this solution is not good for streaming solutions since it requires the full track to be processed before the first byte can be streamed]
  
  another option I is to use `acrossfade` with a silent track:
  
  ```
  ffmpeg -i input.mp4 -filter_complex "aevalsrc=0:d=0.6 [a_silence]; [0:a:0] [a_silence] acrossfade=d=0.6" output.mp4
  
  ```
  
  > **Comments:**
  > **username_2**: This is actually pretty elegant :-)  
  > **username_3**: this works great. could you explain how it works. trying to make sense of the documentation..  
  > **username_1**: @username_3: Option 1: The first filter adds a fade at the beginning of the track. It then reverse the audio and adds another fade in at the beginning (which is actually the end). Finally the track is reversed again. Option 2 creates a silent track of 0.6 seconds and crossfades it for the same amount of time with the original track. All the filters and their usage are documented here: https://ffmpeg.org/ffmpeg-filters.html (although I have to agree that it's a bit cryptic)  
  > **username_4**: What would be the script to merge Video + Audio (Audio is longer) and have fade in and our for the audio (Let's say Fade In finishes after 2 seconds, Fade out starts at 48 and ends at 51)?  
  
  ---
  
  ### Answer by username_5 on 2017-05-07. Score: 3
  
  The answer to question 2 is a resounding YES! I was looking for the same functionality and I ended up writing a bash script that asks for fade duration in seconds and calculates the initial frame for the fade-out:
  
  ```
  #!/bin/bash
  
  f="$*" # all args
  p="${f##*/}" # file
  fn="${p%.*}" # name only
  e="${p##*.}" # extension
  
  echo
  echo $f
  echo $p
  echo $fn
  echo $e
  echo
  
  read -e -p "Number of seconds of fade-out: " -i 1 sec # prompt for fade duration in seconds
  
  r=$(ffprobe -show_streams "$f" 2> /dev/null | grep r_frame_rate | head -1 | cut -d \= -f 2) # frame rate
  let "frames = $r * $sec" # duration of fade as number of frames
  echo $r
  echo $sec
  echo $frames
  lf=$(ffprobe -show_streams "$f" 2> /dev/null | grep nb_frames | head -1 | cut -d \= -f 2) # total number of frames
  let "lf = $lf - $frames" # initial frame to start fade at
  
  ffmpeg -loglevel quiet -i "$f" -vf fade=out:$lf:$frames -r $r -vcodec libx264 -preset slow -crf 12 -bf 2 -flags +cgop -pix_fmt yuv420p -acodec copy "$fn $sec sec fade-out.$e"
  open "$fn $sec sec fade-out.$e"
  
  ```
  
  Fades require re-encoding so using a low `crf` value for `libx264` gives a high quality re-encode. The comments should explain everything else.
  
  > **Comments:**
  > **username_6**: *The answer to question 2 is a resounding YES!* --> it's still No. The Q is *Can ffmpeg automatically figure out the duration...* but you've written a script that runs multiple ff* commands to do it. Useful for *nix systems, though.  
  > **username_5**: I see, so you insist that YOUR answer is STILL correct? If you follow my script, you will see that it does indeed `automatically figure out the duration` using `ffprobe`, which is installed with `ffmpeg`. Who said anything about any restriction on the number of commands or `*nix`? It seems that the OP is using a version of UNIX. No `.exe` in the `ffmpeg` command. I myself am using `OS X` which is built on `UNIX` but is not `UNIX`.  
  > **username_6**: * If you follow my script, you will see that it does indeed automatically figure out* --> your script does, ffmpeg does not.  
  > **username_5**: But `ffprobe` is an integral part of the `ffmpeg` distribution. You're picking at nits to save face IMO.  
  > **username_7**: I'd have to agree that something being done by a bash script is *not* something being automatically done by ffmpeg.  But if this works, this is a way to work around that problem.  Hence the term "workaround".  I can't say if it works because I, like most people out there, don't have bash or a unix system.  +1 for the effort anyhow.  
  
  ---
  
  