PDA

View Full Version : Merging 2 x264 MP4 files


TurnX
19th January 2007, 19:00
Pretty much I have 2 files. 1 file is a short textless opening for a show in WMV9 format. The other file is the actual show which is in Xvid. I can successfully encode each file seperatly into x264. The problem comes when I want to merge these 2 files together, so I get 1 file with the opening and the episode. Ive tried a couple methods of merging the resulting mp4 files but got some bad results. YAMB gave me a file but the audio was de-synced and the second video wouldnt show. I found other methods but they require re encoding the file into an avi with a divx/xvid codec. So is there anyway to merge the resulting mp4 without re encoding/de-synced audio or is there something I can do before encoding to merge the files? thanks for any help.

foxyshadis
20th January 2007, 08:13
yamb/mp4box is incapable of correctly merging two streams with differing headers. (Simply joining two avi streams with a hex editor or copy /b works, hackish as it is, though seeking is slow.) Since you've already opened a thread in avisynth usage on the topic, the way I would recommend, I feel I should close it, but I'll leave it for any further discussion.

Also, moving to new containers, since it's more of a container than codec problem.

Drachir
20th January 2007, 10:09
Which tools do you use to create your mp4 files?

If you encoded both files with the same settings(same decoder specific info) you can try this:

Take a look at your first file with "MP4Box -info firstFile.mp4".

Example:

MP4Box -info firstFile.mp4

* Movie Info *
Timescale 1000 - Duration 00:02:33.840
Fragmented File no - 2 track(s)
File Brand isom - version 512
Created: GMT Fri Jan 2 00:00:00 1970

File has no MPEG4 IOD/OD

Track # 1 Info - TrackID 1 - TimeScale 25025 - Duration 00:02:33.840
Media Info: Language "Undetermined" - Type "vide:avc1" - 3846 samples
MPEG-4 Config: Visual Stream - ObjectTypeIndication 0x21
AVC/H264 Video - Visual Size 640 x 480 - Profile High @ Level 5.1
NAL Unit length bits: 32
Pixel Aspect Ratio 1:1 - Indicated track size 640 x 480
Self-synchronized

Track # 2 Info - TrackID 2 - TimeScale 48000 - Duration 00:02:33.728
Media Info: Language "Undetermined" - Type "soun:mp4a" - 7206 samples
MPEG-4 Config: Audio Stream - ObjectTypeIndication 0x40
MPEG-4 Audio AAC LC - 2 Channel(s) - SampleRate 48000
Synchronized on stream 1


You can see that the Audio Track is 120ms shorter.
Now take your second file and set a Delay of 120ms for the Audio Track.


MP4Box -add secondFile.mp4#1 -add secondFile.mp4#2:delay=112 secondFile_withDelay.mp4


Now concatenate both files:

MP4Box -add firstFile.mp4 -cat secondFile_withDelay.mp4 output.mp4


Try it wit a recent CVS snapshot.

If you want append a file with a different encoding(decoder specific info change) you could do that if you update the OD Track of the MP4 file at a given time(example - there are other ways too). This way you will have two Audio/Video Tracks, but a real MP4 Player like Osmo4 will play them one after another.
But most players don't support MPEG 4 systems, so this is probably no help to you.

bond
20th January 2007, 17:41
yamb/mp4box is incapable of correctly merging two streams with differing headers. (Simply joining two avi streams with a hex editor or copy /b works, hackish as it is, though seeking is slow.) Since you've already opened a thread in avisynth usage on the topic, the way I would recommend, I feel I should close it, but I'll leave it for any further discussion.which decoder handles avc streams with changing headers in avi?

also such a stream to be avc compliant needs to signal different IDs for the different SPS/PPS. x264 always uses the same ID by default, so i doubt that the guy can create a valid avc stream the way you described it

foxyshadis
21st January 2007, 07:39
I'm going to go out on a limb and presume the HD-DVD/Bluray certified ones can, if multiple different streams are allowed in them (like DVD). But I don't actually know, I haven't tried with avi and don't have any proper samples of mp4/mpg. I've seen ASP decoders do it for avi, though, and fail for mp4s muxed with mp4box.

The muxer could rewrite IDs on each stream as it writes the new one if it was needed. Maybe not the best, but the most compatible way.

bond
21st January 2007, 11:33
I've seen ASP decoders do it for avi, though, and fail for mp4s muxed with mp4box.in asp it is not allowed to have changing headers in a stream, so your files, while maybe working in some decoders, werent spec compliant

akupenguin
22nd January 2007, 17:59
H.264 does allow the same sps id to be re-used with different contents, as long as it only changes between the last frame of one gop and the idr frame of the next gop (i.e. exactly where the sps goes when you concatenate separately encoded streams). While such a stream is standard-compliant, it can't be stored (without re-writing the headers) in mp4, mkv, or any other container that requires global headers. It can be stored in avi, mpeg-ts, mpeg-ps, or h264es.
libavcodec can correctly decode most such streams, though extreme changes like differing resolutions can break it whether or not they re-use one sps id.

While x264 always uses the same sps id by default, you can change it with --sps-id.