Welcome to Doom9's Forum, THE in-place to be for everyone interested in DVD conversion.

Before you start posting please read the forum rules. By posting to this forum you agree to abide by the rules.


Go Back   Doom9's Forum > General > Audio encoding

Thread Tools Search this Thread Display Modes
Old 25th October 2022, 05:32   #1  |  Link
Registered User
Join Date: Oct 2022
Posts: 2
interleaveDolbyStreams.py: A Python Tool that Interleaves AC3 and TrueHD Bit Streams

I wrote a command line script that interleaves the AC3 and Dolby TrueHD bit streams in the exact manner found in blu-ray. Even though I wrote this script for my selfish purposes, because it would be useful to others, I’ve decided to freely share it on GitHub: https://github.com/digitalaudionerd/...TrueHD-Streams

I developed this script on Linux, but, because Python is cross-platform, it should run on any system with Python 3.

If you wish to find out how I know that this script produces interleaved AC3/TrueHD bit streams exactly as they are found in blu-ray, please continue reading.


I had some .ac3, .thd and .thd+ac3 files. After having my script interleave the various .ac3 and .thd files, I compared the .thd+ac3 files it made to the original .thd+ac3 files. The interleaved .thd+ac3 files were identical to the original .thd+ac3 files.

Content with those tests, I interleaved a set of .ac3 and .thd files from an MKV file. I then used the video from the MKV file and its newly corresponding .thd+ac3 file to create an AVCHD with tsMuxer. I transferred the AVCHD to my NAS and watched the AVCHD with my Oppo UDP-203 blu-ray player (my selfish purpose).

My Oppo identified the audio stream as Dolby TrueHD (if the audio track is only TrueHD without the AC3 “core,” the Oppo identifies the stream as MLP). My Denon AV receiver decoded the TrueHD audio, just as with any Dolby TrueHD blu-ray or blu-ray backup.

How do I know the structure of AC3 and TrueHD interleaving in blu-ray?

Before writing this interleave script, I wrote a previous Python script that examined the AC3 frames and TrueHD access headers of the .thd+ac3 files I had. After making the script count the frames and access headers, I noticed a clearly distinct pattern consistent in all of the .thd+ac3 files.
digitalaudionerd is offline   Reply With Quote
Old 25th October 2022, 09:46   #2  |  Link
Acid fr0g
tormento's Avatar
Join Date: May 2002
Location: Italy
Posts: 2,437
Originally Posted by digitalaudionerd View Post
I wrote a command line script that interleaves the AC3 and Dolby TrueHD bit streams
What are the differences with THDMerge, found in DGDecodeNV package?
@turment on Telegram
tormento is offline   Reply With Quote
Old 26th October 2022, 03:20   #3  |  Link
Registered User
Join Date: Oct 2022
Posts: 2
One difference is how the streams are interleaved. I saw this comment about how the output of thdmerge doesn’t exactly match ripped files: https://forum.doom9.org/showthread.p...47#post1965447

To investigate, I used thdmerge to create some .thd+ac3 files. I examined these test files and saw the discrepancy. In official blu-ray streams, there are 192 TrueHD access headers for every 5 AC3 frames (the only possible exception is at the end of the interleaved stream). Thdmerge starts with an error: there are 193 TrueHD access headers for the first 5 AC3 frames, and then the remaining bit stream is more correct, with 192 access headers per 5 frames, but not arranged exactly as in blu-ray.

Another difference is that thdmerge alternates reading and writing files throughout the interleaving process. I looked at the source code, and it looks like the code reads one input file, writes its data to the .thd+ac3 file, and then reads/writes for the other input file, repeating the process until reaching the end of each input file. (Thdmerge is written in C++, which isn’t my programming language, so I might be mistaken.) My interleaving script reads the entirety of the .ac3 and .thd files first, parses the data, interleaves the data, and then writes the data to the .thd+ac3 file.

The last difference I noticed is that my script doesn’t have the truncate feature that thdmerge has. I can add the truncate feature if requested.
digitalaudionerd is offline   Reply With Quote
Old 30th October 2022, 17:23   #4  |  Link
Registered User
Join Date: Jul 2012
Posts: 1,209
Your app does appear to create a thd+ac3 file identical to the original, as per a binary file compare.
Good stuff
gonca is offline   Reply With Quote
Old 6th June 2023, 21:24   #5  |  Link
Registered User
Join Date: Mar 2020
Posts: 23
What are the differences with eac3to.exe? And what the truncate feature do?
Hellboy. is offline   Reply With Quote
Old 7th June 2023, 09:31   #6  |  Link
tebasuna51's Avatar
Join Date: Feb 2005
Location: Spain
Posts: 6,773
eac3to read a input thd file, recode it to ac3 with libaften.dll (old and with some bugs) and then mux them.

Here the thd and the ac3 must be the input.

The truncate feature is, when the two streams have different length, truncate the output at short length.
BeHappy, AviSynth audio transcoder.
tebasuna51 is offline   Reply With Quote
Old 18th August 2023, 06:46   #7  |  Link
Registered User
Join Date: Feb 2020
Posts: 529
Does not work with mono tracks.

Also truncate feature is useless. AC3 is always priming format.

Does not work on other samples too. BTW, found out that ffmpeg can truncate last ac3 frame on demux from m2ts compared to TSmuxer


There is no specification for the interleaving and no issues have been reported with streams interleaved by thdmerge. Saying it starts with an error is unsubstantiated FUD. I have looked at many BD disks and there is no consistent pattern to the interleaving, despite what the guy claims. thdmerge merges 5GB files in less than 5-10 seconds on my system. There is no performance issue and I seriously doubt a python solution is as fast.

From https://rationalqm.us/board/viewtopi...p=16528#p16528

Last edited by Balling; 18th August 2023 at 07:24.
Balling is offline   Reply With Quote

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

All times are GMT +1. The time now is 12:50.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2023, vBulletin Solutions Inc.