Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fade out the main audio track (idea) #178

Open
rebotnix opened this issue Jan 31, 2022 · 1 comment
Open

Fade out the main audio track (idea) #178

rebotnix opened this issue Jan 31, 2022 · 1 comment

Comments

@rebotnix
Copy link

rebotnix commented Jan 31, 2022

HI,

i tried to set an fade in and fade audio out to the main audio track. Without a smooth fadeout and when the audio track is longer then the generated one, the cut is really hard without a fading.

{
outPath: './firevideo.mp4',
audioFilePath: 'assets/audiot1.aac',
keepSourceAudio:true,

...

It seems that there is no such kind of filter yet. I experiment with ffmpeg and was able to set in and out´s without having the excatly duration of the generated clip.

The filtercomplex i used was:
ffmpeg -i firevideo.mp4 -vcodec copy -filter_complex "aevalsrc=0:d=3.6 [a_silence]; [0:a:0] [a_silence] acrossfade=d=3.6" output.mp4

The value 3.6 fades the audio out at the durationtotal-3.6 seconds.

I would like to add this in audio.js in the filter complex section

 async function mixArbitraryAudio({ streams, audioNorm, outputVolume }) {
...

filterComplex += `;${streams.map((s, i) => `[a${i}]`).join('')}amix=inputs=${streams.length}:duration=first:dropout_transition=0:weights=${streams.map((s) => (s.mixVolume != null ? s.mixVolume : 1)).join(' ')}${audioNormArg}${volumeArg}`;

When i insert the ffmpeg synthax i got that the filter can´t implement so many audio tracks. Currently a hot fix is to add this in the index.ts when everything is ready to copy the videotrack and re-encode the main audiotrack.

Is there a way to implement it in the audio.js part?

@saschaglo
Copy link

saschaglo commented Feb 8, 2022

I've taken another approach where I pass in the total length of clips to the mixArbitraryAudio function and do the following:

let filterComplex = streams.map(({ start, cutFrom, cutTo, fadeOut }, i) => {
      const cutToArg = (cutTo != null ? `:end=${cutTo}` : '');
      const apadArg = i > 0 ? ',apad' : ''; // Don't pad the first track (audio from video clips with correct duration)
      const fadeOutArg = (fadeOut != null ? `,afade=type=out:duration=${fadeOut}:start_time=${clipsDuration - fadeOut}` : '');

      return `[${i}]atrim=start=${cutFrom || 0}${cutToArg},adelay=delays=${Math.floor((start || 0) * 1000)}:all=1${apadArg}${fadeOutArg}[a${i}]`;
    }).join(';');

As you can see I've added a fadeOutArg (since I only need fadeout, not fadein) to the args list and apply that after the pad argument. In the main audio tracks list I can then set a defined length of fadeOut which will be applied at the very end of the video, also if the audio track is longer than the video itself

Example (1 second of fadeout at the end):

[...]
    "audioTracks": [
        {
            "mixVolume": 0.3,
            "path": "./assets/song.mp3",
            "fadeOut": 1
        }
    ],
[...]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants