Combine Video Audio

Questions and answers on how to get the most out of FFAStrans
Post Reply
graham728
Posts: 76
Joined: Sat Oct 13, 2018 10:54 pm

Combine Video Audio

Post by graham728 »

Apologies if this has been posted before - I've had a quick search and couldn't see it.

I'd like to know if it's possible to take a video source and an external audio file and combine the two (the audio file replaces the audio track on the video source).

Doesn't have to be perfect. Would just be helpful for producers to roughly see how a dub mix is going to look on a client export.

Thanks.
emcodem
Posts: 1645
Joined: Wed Sep 19, 2018 8:11 am

Re: Combine Video Audio

Post by emcodem »

Hey there,
unfortunately nothing built in, so you need to use a custom ffmpeg or commandline.
As usual you need to start your workflow with one of the 2 files, either the video or the audio. The other file type must be excluded in your watchfolder settings.
Assumed you use the video file to start the workflow, then the first thing your workflow does is to calculate the path to the corresponding audio file using a populate processor. It shall fill a variable, e.g. %path_to_audio% with the calculated path to the audio.

Custom ffmpeg for one external wav file that contains all channels (workflow populate->customffmpeg -> deliver):

Code: Select all

-i "%path_to_audio%" -map 0:v -map 1:a -codec copy 
Commandline executor one external wav file that contains all channels (workflow " populate->commandline proc"):

Code: Select all

cmd /C ""%s_ffmpeg%" -i "%s_source%" -i "%path_to_audio%" -map 0:v -map 1:a -codec copy "%full_path_and_name_of_outputfile%" "
Of course ou can change from codec copy to whatever codec you like, i guess you know how to do that.

If you have special audio config, like multiple additional files and/or you need more than one track in the output let me know which config you have
emcodem, wrapping since 2009 you got the rhyme?
graham728
Posts: 76
Joined: Sat Oct 13, 2018 10:54 pm

Re: Combine Video Audio

Post by graham728 »

Hi,

Thanks, so I've got

Populate variable set to Set user variable > %path_to_audio% to %s_original_path%\%s_original_name%.wav

> Custom FFMPEG
Options:
-i "%path_to_audio%" -map 0:v -map 1:a -codec copy

> Deliver

The output file outputs as an .avs file.
emcodem
Posts: 1645
Joined: Wed Sep 19, 2018 8:11 am

Re: Combine Video Audio

Post by emcodem »

The output is an avs only when you use a/v decoder normally... Can u send me the wf please?
emcodem, wrapping since 2009 you got the rhyme?
graham728
Posts: 76
Joined: Sat Oct 13, 2018 10:54 pm

Re: Combine Video Audio

Post by graham728 »

Can I be really shameless and ask if someone can build this as a workflow? I've tried a few times with no success. Ideally it would be a watch folder that waits for a .wav and .mp4 with the same file names and then combines them.
emcodem
Posts: 1645
Joined: Wed Sep 19, 2018 8:11 am

Re: Combine Video Audio

Post by emcodem »

Sure, we love creating example workflows :D
However, a lot of things depends on your requirements. This workflow example works like:

-) accepts only *.wav files (actually you can submit anything that has some audio tracks)
-) expects a *.mp4 file with exact same name to be placed into the watchfolder within 1 hour (the matching *.mp4 must have at least 1 video track)
-) does not transcode, just copies video from mp4 and audio from wav
-) creates a mkv output file, i chose mkv because i dont know which codecs you want to work with and MKV is compatible to more codecs than mov and mp4 is
-) the mkv output file is written back into the watchfolder called *_merged.mkv
emcodem_add_external_audio.json
(3.42 KiB) Downloaded 43 times
Please check it out and let me know all additional/defined requirements :D
emcodem, wrapping since 2009 you got the rhyme?
graham728
Posts: 76
Joined: Sat Oct 13, 2018 10:54 pm

Re: Combine Video Audio

Post by graham728 »

Thanks - That appears to be working - it is however trimming the video to the length of the audio. Is there a way to have it keep the video original length?

If audio is shorter, it just becomes mute after that point. If audio is longer than the video then black?

I've changed -shortest to -longest however, I get the below error code:
Process exited with error code 1 (bunch of FFmpeg copyright info) configurations: --disable-static --enable-shared--pkg-c

Thanks!
emcodem
Posts: 1645
Joined: Wed Sep 19, 2018 8:11 am

Re: Combine Video Audio

Post by emcodem »

No problem. At least in the direction you mention: audio is shorter than video. The other direction would require more logic.
Btw, in order to investigate the real ffmpeg error message, please use webinterface log viewer.

In the workflow i sent, the cmd processor, replace the stuff by this:

Code: Select all

"%s_ffmpeg%" -i "%s_original_path%\%s_original_name%.mp4" -i "%s_source%" -map 0:V -map 1:a -shortest -af apad  -c:v copy -c:a pcm_s24le -y "%s_original_path%\%s_original_name%_merged.mkv"
What changed from the version that i sent?
  • -af apad
  • -c:a pcm_s24le
So we now set a fixed audio codec instead of just copying what was in the wav. In ffmpeg, we must use an audio filter to "pad" audios. For that, we cannot "copy" the audio codec anymore but we must [in addition to the -af filter] also specify the audio codec we want to "encode to".

Note that i am more or less just making educated guesses here, i know that it kind of works but please test the results under different circumstances before you take into production.
emcodem, wrapping since 2009 you got the rhyme?
graham728
Posts: 76
Joined: Sat Oct 13, 2018 10:54 pm

Re: Combine Video Audio

Post by graham728 »

Thanks - this sorts the issue out if the video is longer than the audio. The audio goes mute if the wav finishes.

If the video is shorter, the video keeps its original duration and cuts off the audio. For example - Audio is 30 secs, video is 10 secs. The video output is 10 secs and cuts off the last 20 secs of audio.

This should be fine for the use I need it for, however.

Thanks!
emcodem
Posts: 1645
Joined: Wed Sep 19, 2018 8:11 am

Re: Combine Video Audio

Post by emcodem »

Great, looking forward to hear if it works for you in production :D
Btw, i missed to explain: if we'd just remove -shortest (above you tried to replace shortest by longest) instead of inserting the apad filter, what would actually happen is that we write less audio than video into the output file. This "can" work but each and every player/decoder might react differently to it, some might continue playing even without audio, others maybe just stop and some might just crash.
emcodem, wrapping since 2009 you got the rhyme?
Post Reply