View Full Version : Split audio in two, filter and re-encode just one part, join again ?
bokeron2020
29th August 2021, 01:09
Hi.
I'm trying to figure out how to do what the title already says.
The audio needs filtering in the final minutes. I'm trying to preserve as much quality as possible not reencoding what doesn't need to be filtered. I know I could transcode to lossless or whatever. For reasons too long to explain, I can't do that. So...
I have a FILE_IN.MKV with 1 HEVC video + 1 AC3 audio streams. Length is, say, 20 minutes.
I want to keep the video as is and split the audio at minute 15. Then I would have audio_A with 15 minutes and audio_B with 5 minutes.
I wouldn't touch audio_A.
I'd apply some filters to just audio_B and re-encode to AC3 again.
Now, I'd want to join back audio_A and audio_B.
Finally, I'd get a FILE_OUT.MKV with original video and the resulting audio stream.
I mostly use ffmpeg for all my audio/video needs. I can think of a few ways to do it but I'm not sure about the results though they'd be quite convolute. Concat, piping out and in, etc. I would prefer not having to demux and if possible keep it all within one line... and as accurate as possible.
So... any ideas? Could someone help me figure it out? Someone already did it?
j7n
29th August 2021, 08:48
It is complicated manual work with a hex editor.
AC-3 compressed data is divided into frames with an overlap. To avoid creating a glitch, so you need to process one additional frame before the 5 minute segment and discard it before concatenation. Encoders also have a delay, typically 256 samples, but not always, which needs to be compensated for by delaying the source 1536-256=1280 samples and discarding the first frame.
The encoding settings (compression: film standard, etc.; dialnorm -27) of the edited part need to match, or you might get a jump in volume at the edit point when dynamic compression or normalization are active in the decoder. Disable prefiltering for DC and 90° rotation.
tebasuna51
29th August 2021, 09:32
Like j7n say is complicated, and the problem is:
...
The encoding settings (compression: film standard, etc.; dialnorm -27) of the edited part need to match, or you might get a jump in volume at the edit point when dynamic compression or normalization are active in the decoder.
because free encoder ffmpeg can't encode with dynamic compression metadata. You need a commercial encoder if your want join with the first part if it have DRC metadata.
Put a sample of original AC3 to see if it have metadata DRC.
GMJCZP
29th August 2021, 14:47
Another way could be using mkvtoolnix, dividing the audio into two chapters. I have never done this but I understand that in a mkv the sequence of a video or audio can be altered given a division into chapters, perhaps the modified piece of audio can be added at the end as a second chapter and saved everything as mka.
bokeron2020
29th August 2021, 20:27
Thanks for your answers!
j7n's answer helps me realize how unprepared I am to face such a task and also gives some clues I'll need to follow and learn the basics of AC3. I think can't do this ATM, I'll consider it again at some point in the future.
tebasuna51's also reveals one more thing I overlooked, DRC metadata. I assumed ffmpeg was managing it properly under the hood but it seems we're out of luck here.
So... using ffmpeg what I could only do is ffmpeg -drc_scale 1 to apply any present DRC and re-encode the now 'statically compressed' audio, couldn't I?
Is there any free tool that could do it? I usually trim, concatenate, apply some low/high-pass flters... how could I work around this limitation in ffmpeg?
I don't have access to the file right now but I'm quite sure it has DRC metadata.
I've used your LeeAudBi tool before and iirc it shows that info, right? I have a copy somewhere, though probably outdated. Sizing the chance... could you please share your most recent 64 bits windows exe?
GMJCZP I'm already using chapters for the content. That would be confusing even if it worked... which, according to what's written above, I'm not so sure about now. I'll give it a try anyway just to see how it goes.
j7n
29th August 2021, 20:55
The resolution of the precalculated compression data is coarse. You don't want to be using it unless the listening conditions demand it. If you hardcode compression only to the edited part, it will sound wrong with DRC off.
Overlap and delay need to be taken into account with any modern format. If you cut at a silent moment, you could get away with ignoring it and accept a tiny sync error.
I've done this incredibly tedious process on a few files before to fade the sound before an unwanted logo. Knowing the constant bitrate (bytes per second), you can jump to a position in time in a hex editor, and go to the nearest header (0B 77). Unfortunately there are no helper tools like mp3directcut for film formats.
bokeron2020
29th August 2021, 22:26
The resolution of the precalculated compression data is coarse. You don't want to be using it unless the listening conditions demand it. If you hardcode compression only to the edited part, it will sound wrong with DRC off.
Overlap and delay need to be taken into account with any modern format. If you cut at a silent moment, you could get away with ignoring it and accept a tiny sync error.
I've done this incredibly tedious process on a few files before to fade the sound before an unwanted logo. Knowing the constant bitrate (bytes per second), you can jump to a position in time in a hex editor, and go to the nearest header (0B 77). Unfortunately there are no helper tools like mp3directcut for film formats.
I'd need to repeat this for a few hundred files. No way I'm able to do it with my current knowledge before I die of old age. I'd need two or three lives to do it properly :eek:
The cutting-editing-joining idea was intended to prevent generating more encoding artifacts on the whole file. Now it looks like a few artifacts more are a blessing compared to going thru that nightmare you describe.
So, case closed. I give up. I'll reencode the whole thing and pray.
As a derived nightmare, I am facing the DRC metadata gordian knot.
I've just learned I can't keep DRC metadata in AC3. Well, I can... only if I just cut without touching anything else. I can't even do a fadein/fadeout.
So what I'm looking for now is a way to edit an audio stream and keep/transfer DRC metadata... which apparently isn't possible unless I buy Nuendo or something like that, and even then I'm not sure if I could.
tebasuna51
29th August 2021, 22:41
Link to last LeeAudBi is here https://forum.doom9.org/showthread.php?p=1522330#post1522330
Only with Dolby commercial encoders you can create DRC data, and also you can't know exactly what is the preset used (Film/music standard/ light...).
My recomendation is recode all the audio, maybe to eac3 or aac to preserve max quality with same size.
DRC metadata is useless actually, only are present in old Dolby streams, now modern AVR's can do the job on the fly with functions like 'Night mode' and others
@j7n
To cut exactly the AC3 streams you can use DelayCut, last version by madshi here http://forum.doom9.org/showthread.php?p=1044001#post1044001
bokeron2020
30th August 2021, 01:10
Link to last LeeAudBi is here https://forum.doom9.org/showthread.php?p=1522330#post1522330
Thanks!
Only with Dolby commercial encoders you can create DRC data, and also you can't know exactly what is the preset used (Film/music standard/ light...).
So the ONLY way would be using something like SurCode for DD and re-applying DRC with whatever settings I dare to apply, right?
My recomendation is recode all the audio, maybe to eac3 or aac to preserve max quality with same size.
Should I apply DRC and keep the compressed range or keep the full range audio?
If I keep full range audio I'd have to rely on what my PC/TV/AVR can do to replicate what DRC is supposed to do, wouldn't I? Late night viewing without being chastised by neighbours and/or wife would then be possible?
Regarding codecs, right now it seems AC3 would be my best chance to ensure compatibility for short to mid-term. I'm not following the market so I don't know if affordable AVRs are now capable of dealing with multichannel AAC, Vorbis... My 2021 Samsung TV can play AAC or Vorbis in MKV but not Opus... I don't know about LG or...
Can someone comment on where is the market aiming?
DRC metadata is useless actually, only are present in old Dolby streams, now modern AVR's can do the job on the fly with functions like 'Night mode' and others
Still present in Apple TV shows like "See" or Disney's "The Mandalorian". I am on the fence waiting for the Affordable But Good Enough™ AVR but meanwhile I have to play from PC or Samsung TV itself... without DRC I might be screwed for a while, no?
PS: Never tried this. What happens when you feed an AVR with multichannel audio without DRC metadata? Are their dynamic range settings based on the feeded metadata so each setting just applies it in a bigger or lesser degree... or does the "Midnight Mode" and the likes apply their own set of rules to even a DRC-less audio?
vBulletin® v3.8.11, Copyright ©2000-2026, vBulletin Solutions Inc.