Ffmpeg - Add audio FX on transition (UPDATED - EASIER)
New here? Learn about Bountify and follow @bountify to get notified of new bounties! x

Attached is 100% working code which succesfuly is able to cross fade two videos, cross fading audio and video.

Task: add an additional parameter $audioFxFile, which will be the audio FX file used (an mp3. Imagine a "boom" FX effect lasting 2 seconds). This audio should be inserted right in the middle of the crossfade transition. The audio should be composited over the final composition, and should not replace the current audio, but be summed up.

Current working code: https://pastebin.com/s96C4kjc

function crossfade(
$audioFxFile, /** NEW PARAMETER **/

Sample runs on XAMPP, using FFmpeg as a binary.

IMPORTANT: A code that is able to simply add an Audio on top of a video at a desired time using ffmpeg, will ALSO be accepted as a winner solution!!!!

This seems to be the solution:

ffmpeg -y -i a.mp4 -itsoffset 00:00:30 sng.m4a -map 0:0 -map 1:0 -c:v copy -preset ultrafast -async 1 out.mp4

Now, I need someone to test it and just prove it works!!

You may use ffmpeg binaries and run this code, and then just parametrize the parameters for me so I can run this from an exec() from PHP.

awarded to PlatinumBobo

Crowdsource coding tasks.

1 Solution

Winning solution

I've tested the command line and it doesn't work for your purpose. Firstly, there's a "-i" missing before sng.m4a. Secondly, even with that fix, all it does is overwrite the audio track with one that has only the sound effect at 30 seconds. That is, it doesn't mix the sound effect with the original audio.

As far as I know, FFmpeg isn't very well suited for doing the kind of editing you want. However I do have a solution that does what you want, albeit not very efficiently. It's split in three commands:

ffmpeg -y -f lavfi -i anullsrc -t 00:00:30 silence.wav
This creates a silent audio file of a desired length. That length would need to be the point at which you want the sound effect to be inserted.

ffmpeg -y -i silence.wav -i sound_fx.m4a -filter_complex "[0:0][1:0] concat=n=2:v=0:a=1 [out]" -map "[out]" with_fx.wav
This concatenates the sound effect to the silent file to have a track that has the audio at the right moment.

ffmpeg -y -i video.mp4 -i with_fx.wav -filter_complex amix -c:v copy -c:a aac out.mp4
This mixes the delayed sound effect to the original audio of the video. In your case, video.mp4 would be the result of the prior crossfade operation.

I'm more used to AviSynth than FFmpeg so that likely isn't the best solution. I wouldn't be surprised if someone could come up with something that does all that alongside the crossfade operation in one giant -filter_complex command.

Hi Platinum, thanks for the reply. Does your solution solve the problem well? Regardless that is not "efficient" is it effective? I mean is the result exactly what I want? Thanks :)
georgefountain 2 months ago
I've tested it with an mp4 video and an m4a sound effect and it does what you want, if you couple it with the cross fade operation from your current code.
PlatinumBobo 2 months ago
So I should first run my cross-fade code and then run your 3 commands? Thanks
georgefountain 2 months ago
Also what is Avisynth? What have you been able to do with that? I might hire you for additional work. Please tell me more! Transitions?
georgefountain 2 months ago
Yeah, the crossfade would give you a video that would be used in the third command. AviSynth is a frameserver that works with scripts. It allows to programmatically process videos to do all sorts of things. As a matter of fact, the script to do what you want (crossfade with sound effect) is actually really simple. If your server is on Windows, you should consider using AviSynth. FFmpeg can actually take an AviSynth script as input, so it can still take care of the encoding. Sadly, there's no viable alternative for other OSes as far as I know. Edit: There seems to be a Linux port, but I don't know how well it works.
PlatinumBobo 2 months ago
Yes I am working on Windows! How can ffmoeg use Avisynth? Thanks
georgefountain 2 months ago
First you need to install AviSynth. Then it's as simple as executing ffmpeg -i script.avs output.mp4, as long as the script is valid. For reading video into Avisynth, I would recommend using the FFmpegSource plugin so as to avoid messing with codecs. Here's the script that does what you want: https://pastebin.com/ZNN1cpua. You would need to change the paths to the input files and the crossfade duration with PHP before sending the script to ffmpeg.
PlatinumBobo 2 months ago
Amazing! Thanks so much :). I also see that you are working on my other bounty "AI investigation". Let me know if you have any questions. Thanks so much for your help!
georgefountain 1 month ago