View Full Version : HowTo Edit H264/AVC TS Streams
DreckSoft
5th August 2006, 11:51
Recently I tried to add a second language track to a TS file containing H264/AVC Video and AC3 audio. It proved more difficult than I thought. I came up with the following solution. Most parts are collected from some thread in this forum, so credits go to the original posters.
1. Demuxing TS:
Extract the video to RAW H264 and AC3 audio. I used mencoder for this. I used the version from 31.07.2006 from here: http://tirnanog.fate.jp/mirror/mplayer/
The latest official release didn't work
To demux video use the following command:
mencoder.exe "Z:\Infile.ts" -of rawvideo -ovc copy -oac copy -noskip -o Y:\Outfile.264
Demux audio using ProjectX (http://www.lucike.info/page_projectx.htm). This has the advantage that you see stream errors in the log. ProjectX will complain a lot about GOP size and won't extract the video track. It will extract all audio tracks and fix errors. I recommend recapping if you'll get errors as watching a file with errors is not much fun and synching the audio will be a pain in the ass. If you don't like ProjectX, you can still use
mencoder.exe "Z:\Infile.ts" -of rawaudio -ovc copy -oac copy -noskip -o Y:\Outfile.ac3
but you'll only get one audio track and no error report.
2. Put H264 into AVI:
AVC2AVI: (link see post from crypto)
avc2avi -i Y:\Outfile.264 -o Z:\Avifile.avi -c H264 -s 2000
Note: AVC2AVI doesn't like streams from BBCHD. Those are in 1440x1088 but AVC2AVI says 1440x1084 and will create one large avi which will require the fixing mentioned in Step 4 (look in section Obsolete). The original AVC2AVI reports 1440x540...
3./4. Removed
5. Add Audio:
You will most likely have to use a delay since the AV delay is not fixed when demuxing. The best thing is to add the audio multiple times with different delays and then see which is the best. You can also add more audio tracks here or cut unwanted parts. Save to a new AVI. Btw: I use VirtualDubMod for this.
6. (Optional) Convert to MKV:
AVI and H264/AVC isn't the best combination, so you may want to use MKV instead of AVI. Use MKVToolnix to create the MKV. You'll have to add the option "--engage allow_avc_in_vfw_mode". The resulting file may already work, it did half the time here. Better also do the following: Open the MKV in AVIMux GUI and save to a new MKV.
Obsolete (were necesary if you use original AVC2AVI from http://forum.doom9.org/showthread.php?t=89979):
3. Change the resolution:
The AVI will have a resolution of 1920x544 instead of 1920x1088. Use a hex editor to change the third occurence of 2002 to 4004.
4. Fix AVI File:
Open the AVI in VirtualDubMod, choose "Ask for extended options after this dialog", then choose "Re-derive keyframe flags".
It will take a looooong time till it's finished. Save the file with direct stream copy to a new avi, so you won't have to do the rekeying again if something goes wrong later. Note that VirtualDub most likely will display a lot of garbage when seeking but the resulting file will play fine
Obsolete (alternate demux commands, didn't work properly once, so I'm using the ones mentioned above):
1. Demuxing TS:
mplayer -dumpvideo -dumpfile Y:\Outfile.264 Z:\Infile.ts
mplayer -dumpaudio -dumpfile Y:\Outfile.ac3 Z:\Infile.ts
DreckSoft
5th August 2006, 11:55
Btw, the following did not work:
1. Put everything into a MP4 container using MP4Box: AC3 not supported
2. Mux H264 RAW directly to MKV using MKVToolnix: Consumes all memory than crashes
3. Mux H264 RAW directly to AVI/MKV using GraphEdit: Stops after 1/2-2/3 of the file
4. Mux TS directly to AVI/MKV using GraphEdit: Stops after 1/2-2/3 of the file
bond
5th August 2006, 14:30
remuxing avc from avi to mkv is not recommended
remux the raw avc stream to mp4 and then remux the mp4 to mkv for getting native avc in mkv
DreckSoft
5th August 2006, 15:53
Doesn't work, same as (2): Consumes all memory than crashes
I'll try another stream soon and post the results.
Eeknay
6th August 2006, 02:24
You can get h264 TS into MKV using GraphEdit. I seem to remember doing it successfully before, but I just tried again now and got weird results (input file was 2 minutes, new file was 5 hours). YMMV.
Alternatively you can rename your desired audio track to the same filename as the TS and do it that way rather than remuxing... a much easier solution until better software appears.
DreckSoft
6th August 2006, 10:15
GraphEdit never muxed the complete file.
I tried a second stream and this time TS => RAW => MP4 => MKV worked but the file was unplayable (stuttering video, async audio). The AVI I created plays fine (is skipped step 6). AVI + AVC may not be a recommended solution but currently it's the only one working properly here.
bond
6th August 2006, 10:22
does the mp4 play fine?
what player are you using?
Schmendrick
6th August 2006, 19:22
From an Astra-Pro7 HD transmission I have recorded a transmission with an AVC/H.264-video and an AC3-audio stream. Using DVBstreamexplorer I can demux the recorded ts-file into a raw H.264- and a raw AC3-file. From the the raw H.264-file I have to cut out everything from the beginning of the raw H.264-file until the first SPS within the file like bond has explained:
http://forum.doom9.org/showthread.php?p=664470#post664470
The AC3-file has to be beautified using AC3fix.
Then the H.264-file is muxed into a MP4-file using Yamb (which uses MP4Box). The MP4-file then can be used as video source (it does not work directly with the corrected H.264-file) with the MKV-editor which also accepts the AC3-file as audio source (which Yamb and MP4Box refuses to accept) so that now after muxing you have a MKV-file which plays fine within Media-Player-classic using the Cyberlink AVC/H.264-decoder from PowerDVD6 and AC3Filter.
The only problem which remains is the synchronization of the video and the audio streams. So far you still have to find an offset value by trial and error and hope that you don't have frame drops within your recording. The offset can be used with "AC3 delay corrector" to adjust the audio stream prior to muxing.
As the botom line we still need an extension to AVC/H.264-containing video files for programs like ProjectX or PVAstrumento to demux TS-files with automatic compensation of offset values and frame drops.
DreckSoft
6th August 2006, 19:48
does the mp4 play fine?
Hm, forgot to try this and already deleted the source file
what player are you using?
ZoomPlayer
crypto
14th August 2006, 09:54
I followed the avi road and the results are playing ok. That's good. But there are several issues:
Step 2. Put H264 into AVI: That should be done by the demuxer.
Step 3. Change the resolution: That's a bug in AVC2AVI. Does not respects the interlaced flag.
Step 4. Fix AVI File: Is a joke. It takes hours and doesn't do the right thing. After that step every frame is a key frame.
Is there a better tool to (re)index an avi file? Or does someone know how to do that programmatically? The key frames can be detected by the frame size.
I am thinking about adding steps 1-4 into TSConverter, but I need some info about step 4.
DreckSoft
14th August 2006, 10:27
But there are several issues:
Yup, but it was the only way that worked.
Step 4. Fix AVI File: Is a joke. It takes hours and doesn't do the right thing. After that step every frame is a key frame.
I remeber a tool called DivFix, but haven't tried it.
I am thinking about adding steps 1-4 into TSConverter, but I need some info about step 4.
It may be easier to add the ability to mux additional audio tracks. Cutting H264 TS would also be nice.
crypto
14th August 2006, 15:22
Yup, but it was the only way that worked.
I remeber a tool called DivFix, but haven't tried it.
Thanks for the info. It seems now to be called DivFix++. Tried it and it ends after writing 4 bytes without an error. I also read a thread on doom9 mentioning that it does not tag key frames.
It may be easier to add the ability to mux additional audio tracks. Cutting H264 TS would also be nice.
Cutting H264 TS and demuxing is already supported when you use the latest HDTVPump Test Version 1.0.10 (http://forum.doom9.org/showthread.php?t=114357).
DreckSoft
14th August 2006, 16:47
Cutting H264 TS and demuxing is already supported when you use the latest HDTVPump Test Version 1.0.10 (http://forum.doom9.org/showthread.php?t=114357).
Great, will try this next time, thanks.
crypto
21st August 2006, 21:56
I did a little research to find out why the avi needs to be fixed in vdub after avc2avi, which is apparently the longest step.
AVC2AVI does write an index and even marks key frames correctly. But it uses the old 2GB limited avi format and that's why VirtualDub does not recognize the index. While recreating the index, the key frame flags are destroyed.
The fix is simple. I have modified AVC2AVI to write a segmented avi, which can be read by VirtualDub including the index and key frames. During mux the file can then be written in the new OML AVI format resulting in a perfectly playable stream.
Edit:
BBC HD Streams are now fully supported by AVC2AVI mod. The latest version of AVC2AVI mod can be found here (http://sourceforge.net/projects/avc2avi).
bond
21st August 2006, 21:57
fix is easy: dont use avi
crypto
21st August 2006, 22:14
> fix is easy: dont use avi
LOL, why? I see nothing which speaks against avi.
As pointed out in this thread, there is currently no other way. MP4 and MKV rely on mp4box, which is complaining about the streams not being compliant. Which is obviously not the case as the streams are from the Tandberg realtime encoder. MP4 also can't hold the AC3 audio.
bond
22nd August 2006, 18:50
mkv doesnt rely on mp4box. mkvmerge relies on mp4 input. you can also create mp4 files without using mp4box, eg by using mp4creator from mpeg4ip
apart from that i doubt your .264 is correct. there is surely garbage at the beginning
crypto
23rd August 2006, 10:48
Thanks, I will check on mp4creator.
> apart from that i doubt your .264 is correct. there is surely garbage at the beginning
The stream starts with the TS packet containing the SPS with all headers (TS, PES, ES) removed. That's what I call a raw stream. Mp4box doesn't seem to like the nal type 9, type 7 sequence header combination. I have no idea why the tandberg encoder adds the type 9 unit but I am sure, it is compliant.
Anyway, we need easy to use tools. The average user surely doesn't want to cut a stream prior to having a tool to accept it.
bond
23rd August 2006, 17:52
you need the sps and pps at the beginning of the stream to get a correct raw stream
DreckSoft
26th August 2006, 15:01
The fix is simple. I have modified AVC2AVI to write a segmented avi, which can be read by VirtualDub including the index and key frames. During mux the file can then be written in the new OML AVI format resulting in a perfectly playable stream.
http://dvbportal.dyn1.de/download.php?file=avc2avi_mod.zip
The generated AVIs are sometimes to big (using only -s). Using -s 2000 fixed the problem.
Btw: I couldn't figure out how to cut H264 TS with TSConverter. The file plays but no time slider. Maybe I'm missing some filter. The program also crashes if the file starts with some garbage at the beginning (which happens quite often).
crypto
26th August 2006, 15:38
The generated AVIs are sometimes to big (using only -s). Using -s 2000 fixed the problem.
The default is max 2048 MB per segment. The program checks if all nal units up to the next sps fit in the current segment, if not it switches to the next segment. How could you tell that the segment is to large. (symptoms?)
Did the rest work as expected? Are the index and keyframe flags intact?
Btw: I couldn't figure out how to cut H264 TS with TSConverter. The file plays but no time slider. Maybe I'm missing some filter. The program also crashes if the file starts with some garbage at the beginning (which happens quite often).
The time slider is on the mark in/mark out dialog. Set the marks, choose MPV+MPA output and then convert. The resulting elementary streams need to be renamed of course.
BTW. the current version has time overflow issues with very large recordings (whole day recordings). Edits also don't start always with a sps unit. I have fixed all that meanwhile. I am also planning to add the DD-Wave header to the demuxed audio and maybe avi output for the demuxed video.
DreckSoft
30th August 2006, 23:12
The default is max 2048 MB per segment. The program checks if all nal units up to the next sps fit in the current segment, if not it switches to the next segment. How could you tell that the segment is to large. (symptoms?)
Virtualdub started to reconstruct the index block. I stopped it and regenerated the avis with -s 2000 so I don't know if the rest was correct.
The time slider is on the mark in/mark out dialog. Set the marks, choose MPV+MPA output and then convert. The resulting elementary streams need to be renamed of course.
BTW. the current version has time overflow issues with very large recordings (whole day recordings). Edits also don't start always with a sps unit. I have fixed all that meanwhile. I am also planning to add the DD-Wave header to the demuxed audio and maybe avi output for the demuxed video.
I still used 2.02. I've upgraded to 2.0b4 now. First nothing worked, didn't even got a picture. After uninstalling Haali Media Splitter at least playback worked (with wrong colors) - but only if I choose MPEG-2 Demultiplexer as Preferred Demultiplexer. Any Push Demux and NVidia HDTV Demux won't work. But I never could convert the Stream, it says: Could not build graph to render source file. Please check the DirectX requirements for this application. (HRESULT = 8004154, Checkpoint = 0004, VideoAnalyser).
Maybe I don't have all necessary filters installed. HDTVPump, Nvidia Pure Video, ffdshow, CoreAVC are installed.
crypto
31st August 2006, 06:49
Virtualdub started to reconstruct the index block. I stopped it and regenerated the avis with -s 2000 so I don't know if the rest was correct.
You are right, then the segments are to large. I will reduce the default size.
Any Push Demux and NVidia HDTV Demux won't work. But I never could convert the Stream, it says: Could not build graph to render source file. Please check the DirectX requirements for this application. (HRESULT = 8004154, Checkpoint = 0004, VideoAnalyser).
I will remove that obsolete demuxer option, because all the other demuxers can't work with H.264 streams.
It seems that the VideoAnalyser is not a stock filter. I'll will add an apropriate custom filter. It is used to synch A/V
DreckSoft
31st August 2006, 11:25
Found the problem:
MPEG-2 Video Stream Analyzer,0x00200000,0,0,sbe.dll,
sbe.dll isn't part of Windows 2000.
get the dll from XP SP2, run regsvr32 sbe.dll and it works.
I'm currently testing if it really works, at least the error message is gone.
xlazom00
31st August 2006, 14:15
I always prefer YAMB & mp4box for muxing raw h264 to MP4
http://ffdshow.faireal.org//mirror/gpac/
I dont have any problem with right resolution(1920x1088) in leatest version.
ts->raw->mp4(no ac3 support)->mkv
it takes very long time with 9GB files :)
eggtimer
3rd September 2006, 23:30
Thanks DreckSoft and others, this is a very useful thread.
I have been trying unsuccessfully to get the BBCHD h.264 video into MP4 and so to a .mkv. Most MPEG2 demuxers produce a raw h.264 stream with junk at the front as you'd expect, but mencoder gives a clean-looking h.264 stream, certainly it starts correctly with the SPS (I think XMuxer Pro also gives a clean raw stream). That raw h.264 stream must be reasonably compliant as it is playable using the Cyberlink codec. But both MP4Box and MP4Creator reject it. I sent a sample to the author of YAMB but I'm not sure he was able to do much with it (probably not high on his list of priorities anyhow - no reason why it should be).
xlazom00, thanks for the suggestion of GPAC, I'll give it a try.
Otherwise it looks like DreckSoft's avi route is the only option for BBC sourced material?
drmih
5th September 2006, 11:26
I use mencoder to convert the BBC HD satellite broadcasts to XVID in one simple step and there doesn't seem to be much of an issue. It occassionally gives poor results but on the whole a useable solution. Takes several hours to convert a 1 hour (9Gb) file to approx 4GB, but atleast it's a solution. It'd be nice to have something as friendly as AGK, but you can't have it all.
CHP
7th September 2006, 17:48
4. Fix AVI File:
Open the AVI in VirtualDubMod, choose "Ask for extended options after this dialog", then choose "Re-derive keyframe flags".
It will take a looooong time till it's finished. Save the file with direct stream copy to a new avi, so you won't have to do the rekeying again if something goes wrong later. Note that VirtualDub most likely will display a lot of garbage when seeking but the resulting file will play fine
[/QUOTE]
Excuse me.
Can you tell me detailed place about that dialog?with a step4.. thanks!
crypto
7th September 2006, 18:47
Can you tell me detailed place about that dialog?with a step4.. thanks!
1. In VirtualDub choose Open Video File... from the file menu
2. Check the box "Ask for extended options after this dialog". It is located in the lower left corner of the file open dialog.
This step is currently only necessary for BBC HD streams. Streams from other H.264 HD stations are correctly indexed with key frames with the modded version of avc2avi.
CHP
8th September 2006, 05:45
1. In VirtualDub choose Open Video File... from the file menu
2. Check the box "Ask for extended options after this dialog". It is located in the lower left corner of the file open dialog.
This step is currently only necessary for BBC HD streams. Streams from other H.264 HD stations are correctly indexed with key frames with the modded version of avc2avi.
oh~!!!,i'm try is next time,thanks very much crypto!
_ck_
27th October 2006, 15:15
Just wanted to check back in this thread after a month and see if there are any more tools to make this process faster.
I've successfully followed the directions to demux the ac3 and raw h264 stream from BBC HD transport streams.
Beyond storing the video in a more useable format than .ts (which does play fine in media player classic) I'd like to be able to do screenshots of individual frames via virtualdub so I convert to the raw h264 to avi but when I load into vdub I must re-derive keyframes which takes a very long time (otherwise I just get completely black frames)
Is there a bug in avc2avi that could be improved to avoid the re-deviving of keyframes? Or possibly a better tool out there for conversion.
ps. I've never gotten tsconverter to work properly on my win2k system, even v2 gives me "failed to build graph" and the log just says "Graph build failed at check point: AddPump"
Pookie
27th October 2006, 18:19
I haven't tested it, I have no idea if it'll work, but perhaps AVImaster (commandline) can do it. :D
http://www.thozie.de/dnn/LinkClick.aspx?link=AVIMaster.zip&tabid=36&mid=388
crypto
27th October 2006, 18:28
Pookie:
the link doesn't seem to work. "Unable to determine IP address from host name for www.thozie.de"
Pookie
27th October 2006, 19:44
Maybe this?
http://www.thozie.de/dnn/Default.aspx
crypto
27th October 2006, 19:53
Thanks, got it now.
DrNein
28th October 2006, 05:33
What's the best method for demuxing AVC/AC3 from M2TS, then remuxing a different AC3 into M2TS or MKV?
ProjectX craps out on loading (buffer error) and MPEG Streamclip seems to hang (unless it intends to take an hour to load the 10GB file). :confused:
crypto
28th October 2006, 07:54
AVCHD uses a different packet size on which most programs choke.
drmpeg has found a way to demux. See the thread New HD Format - AVCHD (By Sony and Panasonic) (http://forum.doom9.org/showthread.php?p=891337#post891337). Having the raw streams, you can remux to avi as described here or to mp4 using a newer mp4box build.
bond
28th October 2006, 12:12
muxing avc streams with the same encoder options used will work with mp4box
DreckSoft
28th October 2006, 13:27
Beyond storing the video in a more useable format than .ts (which does play fine in media player classic) I'd like to be able to do screenshots of individual frames via virtualdub so I convert to the raw h264 to avi but when I load into vdub I must re-derive keyframes which takes a very long time (otherwise I just get completely black frames)
Is there a bug in avc2avi that could be improved to avoid the re-deviving of keyframes? Or possibly a better tool out there for conversion.
AVC2AVI doesn't work properly on BBC streams. It only creates one big file. I have no idea why the split option doesn't work with BBCHD.
JK1974
29th October 2006, 02:13
@crypto:
The Readme of xporthdmv says:n = demux all audio, don't try to cut the start of audio to match the start of video.
Doesn´t this mean in other words that audio is not being synced? So it still would not be an alternative to an hopefully updated ProjectX in the future...
crypto
29th October 2006, 10:05
Exactly, xporthdmv does not attempt to resync. However, the constant lag can be corrected when muxing.
tauka
9th November 2006, 13:33
hi, i need a little help :)
i have a ts with h264 and ac3.. i wanna demux the ac3 in any way, i tried it with mencoder, but it says mpthreadgc2.dll is missing, i got it from the net, tried to register, but no succes. With projectx i get an ac3 file, which has the same length as the movie, but it has 31.25fps !??!?! actually the movie is pal, 25fps..with dvbportal TSConverter i got 31.25fps ac3 audio again.. :( vdubmod of course cant open it, only with directshowsource, but then in the stream list the audio can be demuxed only as uncompressed. can somebody help me? Is it possible at all to get it right out from it with 25fps?
thanks
crypto
9th November 2006, 17:07
How can an AC3 file have a fps? (frames per second?)
tauka
9th November 2006, 17:57
How can an AC3 file have a fps? (frames per second?)
well, eek, your right :)
i tried to synch it to an encode i just made, but i didnt succed.. it looked like how the time passed, the sound was more and more out of sync.. then i took a look at the delaycut info:
====== INPUT FILE INFO ========================
File is ac3
Bitrate (kbit/s) 448
Act rate (kbit/s) 448.000
File size (bytes) 314854400
Channels mode 3/2: L+C+R+SL+SR
Sampling Frec 48000
Low Frec Effects LFE: Present
Duration 01:33:42.400
Frame length (ms) 32.000000
Frames/second 31.250000
Num of frames 175700
Bytes per Frame 1792.0000
Size % Framesize 0
CRC present: YES
=============================================
====== TARGET FILE INFO ======================
Start Frame 0
End Frame 175699
Num of Frames 175700
Duration 01:33:42.400
NotFixedDelay 0.0000
You can see there.. i dont know how it is counted at all, but correctly it should have 25"fps", and 40ms frame duration... :confused:
i have no idea really how it can be, i could encode the movie well with 25fps..
crypto
9th November 2006, 18:34
OK, I see it :)
AC3 frames and picture frames don't have to be in a 1:1 relation. So 31.25 "fps" for the AC3 file is perfectly OK.
Just an idea, does the original file contain commercial breaks? If so, there are changes in video from HD to SD and audio from 5.1 to 2.0 which explain the bad synch.
tauka
9th November 2006, 21:22
OK, I see it :)
AC3 frames and picture frames don't have to be in a 1:1 relation. So 31.25 "fps" for the AC3 file is perfectly OK.
Just an idea, does the original file contain commercial breaks? If so, there are changes in video from HD to SD and audio from 5.1 to 2.0 which explain the bad synch.
hm, this stuff about the 1:1 relation is interesting :)
well i dont know, cause my comp cant play 1080p avc :) but as i said, i wouldnt explain the longer and longer outofsync :sly: it looks like somehow its wrong or i dont know :s
popper
10th November 2006, 13:02
I use mencoder to convert the BBC HD satellite broadcasts to XVID in one simple step and there doesn't seem to be much of an issue. It occassionally gives poor results but on the whole a useable solution. Takes several hours to convert a 1 hour (9Gb) file to approx 4GB, but atleast it's a solution. It'd be nice to have something as friendly as AGK, but you can't have it all.
your converting a part 10(AVC) into a part 2(ASP) mpeg4. that seems like a backward step doesnt it, plus only part 10 can use lossless encode cant it?, so you would be loosing even more quality there wouldnt you?.
regarding MP4Box , is it known if the author has/is fixing/adding the needed options to make life easyer for the gui front end writer etc, surely thats a better option in the long term than having to use the old avi step?.
Oxygen
14th November 2006, 10:34
As i understand, there isn't much of a solution to transcode h264.ts files except memcoder via cli ?
I'll need to look deeper into the x264 cli to transcode the files with the same flexibility/quality as MeGUI manage to produce.
---
Well, seems like mencoder is trashing me out:
the -dumpaudio works fine, creates a nice ac3 file, but the mplayer -dumpvideo (or mencoder -ovc copy -of rawvideo) doesn't do much at all except creating an empty file and using 100% cpu, but not filling content.
to be exact, dumpvideo closes down the mplayer with a
TS_PARSE: COULDN'T SYNC
Core dumped ;)
Exiting... (End of file)
and mencoder via -ovc copy -of rawvideo loops on a 100% cpu task with nothing filling the output file.
oddball
14th November 2006, 18:37
If you want to dump the video you are better off using TSConverter or xportdev. The pain of it is avc2avi_mod creates an AVI file with no index or keyframe info so when you load it into VDub you have to reindex and reykeyframe which can take ages. But it's the only way I have found I can edit out adverts and corrupt portions.
Oxygen
14th November 2006, 19:30
Also, i don't want to process the file via vdub ;)
Anyway i managed to get a graphedit filter working, but at .8 to .4fps ! (i suppose MBAFF deinterlace don't help the decoding)
It's, wow.
vBulletin® v3.8.11, Copyright ©2000-2026, vBulletin Solutions Inc.