Answers.Body: <p><strong>Step 1</strong> Get the duration of the main file.</p>
  
  <pre><code>ffprobe main.mp4 -show_entries format=duration -of compact=nk=1:p=0 -v 0
  </code></pre>
  
  <p>This will print out a single value, like this</p>
  
  <pre><code>61.600204
  </code></pre>
  
  <p>which is the duration in seconds.</p>
  
  <p><strong>Step 2</strong> Perform the concat</p>
  
  <pre><code>ffmpeg -i main.mp4 -i intro.mp4 -i outro.mp4 \
    -filter_complex "[1]trim=0:20,setpts=PTS-STARTPTS[pre]; \
                     [2]trim=10,setpts=PTS-STARTPTS[post]; \
                     [pre][0][post]concat=n=3:v=1:a=0[v]; \
                     [1:a][0:a]acrossfade=d=10[a1];[a1][2:a]acrossfade=d=10[a]" \
    -map '[v]' -map '[a]' out.mp4
  </code></pre>
  
  <p>Since the video and audio from the intro/outro are treated differently, in terms of duration, we perform two separate concats.</p>
  
  <p>First, the intro video is trimmed to leave out the last ten seconds. The outro is trimmed to leave out its first ten seconds. Then those two trimmed segments are sandwiched with the main video stream in the first concat.</p>
  
  <p>For audio, the crossfades are performed in two stages. First, the intro with the main audio. Then, that result with the outro audio. Duration of crossfade is 10 seconds.</p>
  
  <hr>
  
  <p>In case, the intro/outro music and image are separate files, use</p>
  
  <pre><code>ffmpeg -i main.mp4 -i intro.mp3 -i outro.mp3 \
         -loop 1 -framerate X -t 20 -i pre.jpg -loop 1 -framerate X -t 20 -i post.jpg \
    -filter_complex "[3][0][4]concat=n=3:v=1:a=0[v]; \
                     [1:a][0:a]acrossfade=d=10[a1];[a1][2:a]acrossfade=d=10[a]" \
    -map '[v]' -map '[a]' out.mp4
  </code></pre>
  
  <p>Here, <code>X</code> should be replaced with the framerate of the main file. The images should be of the same resolution as the video.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-10-12T11:48:56.240Z
Answers.Text: Really awesome, thanks. Just a few things that are a bit off:
Answers.UserDisplayName: devbanana
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-10-12T11:49:58.927Z
Answers.Text: Sorry submitted too soon. So basically the audio of the video isn't being mixed with the audio of the last X seconds of the intro. So the intro plays, ends, then the video starts playing from that many seconds in. The other issue, I want to know if it is possible to set the bitrate of the intro/outro. The bitrate of the video's audio isn't great, so right now it's I think the intro/outro is matching that bitrate. The intro/outro are actually wav files.
Answers.UserDisplayName: devbanana
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-10-12T11:54:33.663Z
Answers.Text: Here's the command I'm using. I actually needed the intro to overlap at 36 seconds, and the outro at 49 seconds. The duration of the video is 1977 seconds, so I'm using 1977-49 = 1928...let me know if this is wrong.
  ffmpeg -i "$video" -i "$intro" -i "$outro" \
         -loop 1 -framerate 25 -t 36 -i $logo -loop 1 -framerate 25 -t 49 -i $logo \
    -filter_complex "[3][0][4]concat=n=3:v=1:a=0[v]; \
                     [0:a]atrim=36:1928,asetpts=PTS-STARTPTS[amain]; \
                     [1:a][amain][2:a]concat=n=3:v=0:a=1[a]" \
    -map '[v]' -map '[a]' out.mp4
Answers.UserDisplayName: devbanana
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-10-12T12:55:14.577Z
Answers.Text: Modified commands to mix the audio streams. Input codecs don't affect the result of the output. Of course, if the input sample rate, bit depth or quality is low, then output can't sound better than that; only match it, at best.
Answers.UserDisplayName: Gyan
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-10-12T14:55:22.117Z
Answers.Text: Thanks. Testing that now. Final question: will the first 10 seconds of the video stream appear? I only want the logo for the first 20 seconds, before the video begins. I'm visually impaired so I can't see what the visual output is, only hear the audio output. Just wanted to verify. Thanks for your help :)
Answers.UserDisplayName: devbanana
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-10-12T15:03:38.013Z
Answers.Text: Yes, both commands preserve the entire video of the main input.
Answers.UserDisplayName: Gyan
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-10-12T18:00:23.647Z
Answers.Text: Thanks @Mulvya. One more request. The crossfade is also troublesome because I'd rather the video audio come in at full volume, or at least not be a slow fade-in. I'd prefer for the intro/outro music to simply be in the background while the video starts/ends. The reason is that the video is an audio lecture of sorts, so if it takes 10+ seconds for the audio to fully fade in, part of the speech will be missed. Hopefully that makes sense.
  
  I'm thinking of more like what you see in podcasts sometimes, where the intro music plays in the background while the host starts speaking.
Answers.UserDisplayName: devbanana
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-10-12T18:26:00.720Z
Answers.Text: You can specify interpolations for the audio transition: `acrossfade=d=10:curve1=dese:curve2=exp`. curve1 applies to fade-out of the first input and `curve2` to fade-in of the 2nd. See `curve` section at https://ffmpeg.org/ffmpeg-filters.html#afade-1 for possible curves.
Answers.UserDisplayName: Gyan
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-10-12T23:01:37.083Z
Answers.Text: Thank you. Is there a way to do it without crossfade though? My intro's overlap is 36 seconds, and the outro's overlap is 49 seconds. This method would work for shorter intros/outros I think, but for this it just makes the speech sound like it is very slowly fading in, which is not the effect I want at all. I want an overlap but without the speech starting out soft, or fading out at the end.
  
  I've had success when doing this for my podcasts with adelay and apad. But not sure how to get something like that to work here since video is involved as well.
Answers.UserDisplayName: devbanana
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-10-13T01:21:28.770Z
Answers.Text: This is what ended up working for me. Accepting your answer since you got me most of the way there.
  
  ffmpeg -y -i "$video" -i "$intro" -i "$outro" \
         -loop 1 -framerate "$framerate" -t 36 -i "logo.jpg" -loop 1 -framerate "$framerate" -t 49 -i "logo.jpg" \
    -filter_complex "[3][0][4]concat=n=3:v=1:a=0[v]; \
                     [0:a]adelay=$start[a1]; \
                     [2:a]adelay=$end[a2]; \
                     [1:a][a1][a2]amix=3[a]" \
    -map '[v]' -map '[a]' out.mp4
Answers.UserDisplayName: devbanana
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-10-12T05:12:30.240Z
Answers.LastActivityDate: 2017-10-12T12:58:29.930Z
Answers.LastEditDate: 2017-10-12T12:58:29.930Z
Answers.UserDisplayName: Gyan
Body: <p>I'm trying to add a music intro and outro to a video. When the music is playing, a particular image should be shown as well, both before and after.</p>
  
  <p>If I could overlap x seconds of the music with the video as well, that'd be even better.</p>
  
  <p>I've done this sort of thing with only an audio file, but not sure how to do it with video.</p>
  
  <p>So example, let's say I have a 30-second intro and a 30-second outro, each of which I wish to overlap by 10 seconds with the video.</p>
  
  <p>So for 20 seconds, the image should show. Then the video should start for the last 10 seconds of the intro.</p>
  
  <p>Then the first 10 seconds of outro will begin during the last 10 seconds of the video, and for the final 20 seconds of the outro, the image will appear.</p>
  
  <p>Any help would be appreciated.</p>
  
Tags: <ffmpeg>
Title: Use Ffmpeg to Add Music Intro and Outro to Video
UserDisplayName: devbanana
ThreadText: # Use Ffmpeg to Add Music Intro and Outro to Video
  
  (Asked by: username_0 on 2017-10-11)
  
  I'm trying to add a music intro and outro to a video. When the music is playing, a particular image should be shown as well, both before and after.
  
  If I could overlap x seconds of the music with the video as well, that'd be even better.
  
  I've done this sort of thing with only an audio file, but not sure how to do it with video.
  
  So example, let's say I have a 30-second intro and a 30-second outro, each of which I wish to overlap by 10 seconds with the video.
  
  So for 20 seconds, the image should show. Then the video should start for the last 10 seconds of the intro.
  
  Then the first 10 seconds of outro will begin during the last 10 seconds of the video, and for the final 20 seconds of the outro, the image will appear.
  
  Any help would be appreciated.
  
  **Tags:** ffmpeg
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2017-10-12. Score: 1 | Accepted answer
  
  **Step 1** Get the duration of the main file.
  
  ```
  ffprobe main.mp4 -show_entries format=duration -of compact=nk=1:p=0 -v 0
  
  ```
  
  This will print out a single value, like this
  
  ```
  61.600204
  
  ```
  
  which is the duration in seconds.
  
  **Step 2** Perform the concat
  
  ```
  ffmpeg -i main.mp4 -i intro.mp4 -i outro.mp4 \
    -filter_complex "[1]trim=0:20,setpts=PTS-STARTPTS[pre]; \
                     [2]trim=10,setpts=PTS-STARTPTS[post]; \
                     [pre][0][post]concat=n=3:v=1:a=0[v]; \
                     [1:a][0:a]acrossfade=d=10[a1];[a1][2:a]acrossfade=d=10[a]" \
    -map '[v]' -map '[a]' out.mp4
  
  ```
  
  Since the video and audio from the intro/outro are treated differently, in terms of duration, we perform two separate concats.
  
  First, the intro video is trimmed to leave out the last ten seconds. The outro is trimmed to leave out its first ten seconds. Then those two trimmed segments are sandwiched with the main video stream in the first concat.
  
  For audio, the crossfades are performed in two stages. First, the intro with the main audio. Then, that result with the outro audio. Duration of crossfade is 10 seconds.
  
  ---
  
  In case, the intro/outro music and image are separate files, use
  
  ```
  ffmpeg -i main.mp4 -i intro.mp3 -i outro.mp3 \
         -loop 1 -framerate X -t 20 -i pre.jpg -loop 1 -framerate X -t 20 -i post.jpg \
    -filter_complex "[3][0][4]concat=n=3:v=1:a=0[v]; \
                     [1:a][0:a]acrossfade=d=10[a1];[a1][2:a]acrossfade=d=10[a]" \
    -map '[v]' -map '[a]' out.mp4
  
  ```
  
  Here, `X` should be replaced with the framerate of the main file. The images should be of the same resolution as the video.
  
  > **Comments:**
  > **username_0**: Really awesome, thanks. Just a few things that are a bit off:  
  > **username_0**: Sorry submitted too soon. So basically the audio of the video isn't being mixed with the audio of the last X seconds of the intro. So the intro plays, ends, then the video starts playing from that many seconds in. The other issue, I want to know if it is possible to set the bitrate of the intro/outro. The bitrate of the video's audio isn't great, so right now it's I think the intro/outro is matching that bitrate. The intro/outro are actually wav files.  
  > **username_0**: Here's the command I'm using. I actually needed the intro to overlap at 36 seconds, and the outro at 49 seconds. The duration of the video is 1977 seconds, so I'm using 1977-49 = 1928...let me know if this is wrong. ffmpeg -i "$video" -i "$intro" -i "$outro" \        -loop 1 -framerate 25 -t 36 -i $logo -loop 1 -framerate 25 -t 49 -i $logo \   -filter_complex "[3][0][4]concat=n=3:v=1:a=0[v]; \                    [0:a]atrim=36:1928,asetpts=PTS-STARTPTS[amain]; \                    [1:a][amain][2:a]concat=n=3:v=0:a=1[a]" \   -map '[v]' -map '[a]' out.mp4  
  > **username_1**: Modified commands to mix the audio streams. Input codecs don't affect the result of the output. Of course, if the input sample rate, bit depth or quality is low, then output can't sound better than that; only match it, at best.  
  > **username_0**: Thanks. Testing that now. Final question: will the first 10 seconds of the video stream appear? I only want the logo for the first 20 seconds, before the video begins. I'm visually impaired so I can't see what the visual output is, only hear the audio output. Just wanted to verify. Thanks for your help :)  
  > **username_1**: Yes, both commands preserve the entire video of the main input.  
  > **username_0**: Thanks @Mulvya. One more request. The crossfade is also troublesome because I'd rather the video audio come in at full volume, or at least not be a slow fade-in. I'd prefer for the intro/outro music to simply be in the background while the video starts/ends. The reason is that the video is an audio lecture of sorts, so if it takes 10+ seconds for the audio to fully fade in, part of the speech will be missed. Hopefully that makes sense.  I'm thinking of more like what you see in podcasts sometimes, where the intro music plays in the background while the host starts speaking.  
  > **username_1**: You can specify interpolations for the audio transition: `acrossfade=d=10:curve1=dese:curve2=exp`. curve1 applies to fade-out of the first input and `curve2` to fade-in of the 2nd. See `curve` section at https://ffmpeg.org/ffmpeg-filters.html#afade-1 for possible curves.  
  > **username_0**: Thank you. Is there a way to do it without crossfade though? My intro's overlap is 36 seconds, and the outro's overlap is 49 seconds. This method would work for shorter intros/outros I think, but for this it just makes the speech sound like it is very slowly fading in, which is not the effect I want at all. I want an overlap but without the speech starting out soft, or fading out at the end.  I've had success when doing this for my podcasts with adelay and apad. But not sure how to get something like that to work here since video is involved as well.  
  > **username_0**: This is what ended up working for me. Accepting your answer since you got me most of the way there.  ffmpeg -y -i "$video" -i "$intro" -i "$outro" \        -loop 1 -framerate "$framerate" -t 36 -i "logo.jpg" -loop 1 -framerate "$framerate" -t 49 -i "logo.jpg" \   -filter_complex "[3][0][4]concat=n=3:v=1:a=0[v]; \                    [0:a]adelay=$start[a1]; \                    [2:a]adelay=$end[a2]; \                    [1:a][a1][a2]amix=3[a]" \   -map '[v]' -map '[a]' out.mp4  
  
  ---
  
  