Audio delay issues when extracting part of ts file

Support forum for DGDecNV
Post Reply
DAE avatar
Agni451
Posts: 12
Joined: Fri Jan 28, 2011 3:09 am

Audio delay issues when extracting part of ts file

Post by Agni451 »

I recorded TV shows every so often, and I use DGIndexNV to extract the actual TV show and leave the commercials. It works great EXCEPT when there aren't any keyframes (I-frames) immediately after the tv show segment ends and the commercials begin. In this situation I have to decide whether to cut off a bit of the tv show or keep part of the commercial. Even if I decide to drop the last few frames of the tv segment, I am still missing a nice chunk of the audio at the end of the segment (ie, the audio cuts out before the video). I suspect this is because of the delay either at the beginning of the entire file or the delay at the I-frame at which I am trying to cut. Short of allowing for exact-frame cutting (instead of at keyframes), is there any way to have DGIndexNV keep an additional x ms of audio at the end (where x equals the delay)?

Edit: The TV shows are recorded as WTV files, but I convert them to MPEG-2 + multichannel AC3 .TS files (essentially just "re-containering" rather than re-encoding).
User avatar
admin
Posts: 4551
Joined: Thu Sep 09, 2010 3:08 pm

Re: Audio delay issues when extracting part of ts file

Post by admin »

I can think of a couple ways to handle this:

1. Don't do cuts in DGIndexNV. Index the entire stream and then do frame-accurate cuts in your script using Trim().

2. Cut keeping the few frames of the commercial and then do frame-accurate trim in the script.
DAE avatar
Agni451
Posts: 12
Joined: Fri Jan 28, 2011 3:09 am

Re: Audio delay issues when extracting part of ts file

Post by Agni451 »

Yes, I could do what you suggested. The only problem is that I need to split the audio from the video to do it, and since these are TV streams, they aren't always perfect. Sometimes there are audio gaps, and when the audio is split from the video, the timecodes no longer keep them in sync. What I need is a byte-for-byte copy of the frames I need, which I is why I use DGIndex. Then I use ffmpeg to insert silent audio when needed to keep the A/V synced throughout. Then I index that file with dgindex to frameserve into avisynth. Since the delays at the keyframes can be quite high (I've seen 2.5 sec, average is 700ms), having the audio cut off prematurely at the end by that much can be annoying.

I can gather the extra audio by hand by using dgindex to index the portion I want, look at the first and last offsets mentioned in the dgi file, then run ffprobe on the full file to get all frame offsets. Then I use a hex editor to highlight the extra audio and simply paste it to the end of the portion I want. However, when I have a number of tv shows that I need to extract the commercials from, I end up with an average of 6 smaller TS files cut from the original per episode. It is very impractical to find, copy and paste the extra audio for dozens of these files, which is why I was hoping your software might one day have the ability to keep delay amount of audio at the end of the file, even if those extra audio frames are just beyond the offset range of the video frames. If I could write a program to do this I would, but I simply am not that good.


BTW, the offsets mentioned in your dgi files ("SEQ xxxxxxxxxxx") is always 18 bytes off of the actual start of the TS packet (should start with sync byte 0x47). DGMPGDec lists them correctly.
User avatar
admin
Posts: 4551
Joined: Thu Sep 09, 2010 3:08 pm

Re: Audio delay issues when extracting part of ts file

Post by admin »

The offset is designed to point to the actual start code of the element not the start of the transport packet. This design is intentionally different from DGMPGDec.
DAE avatar
Agni451
Posts: 12
Joined: Fri Jan 28, 2011 3:09 am

Re: Audio delay issues when extracting part of ts file

Post by Agni451 »

Any chance you might consider adding this feature in future releases? I won't keep pushing it, I just thought it was a legitimate issue to look into.
User avatar
admin
Posts: 4551
Joined: Thu Sep 09, 2010 3:08 pm

Re: Audio delay issues when extracting part of ts file

Post by admin »

If you provide a sample source file that I can use to duplicate your issue, I can assess whether there is anything I can do to help.
DAE avatar
Agni451
Posts: 12
Joined: Fri Jan 28, 2011 3:09 am

Re: Audio delay issues when extracting part of ts file

Post by Agni451 »

I've posted a 7z file containing the example on a temporary MS Skydrive. Sign in at skydrive.live.com with email "jd_dgindex@live.com" and password "DGIndexNV". The 7z includes a readme that describes everything.
DAE avatar
barbatrukko
Posts: 6
Joined: Tue Feb 19, 2013 10:19 am

Re: Audio delay issues when extracting part of ts file

Post by barbatrukko »

If you have .TS file (with mpg2) you can use ProjectX for do an accurate cut of commercial. After this you have 2 choice:
1) save new file in TS and then use DGiNV to demux
2) save new file already demuxed by ProjectX. So you can index the video only and process it separately.
When you remux audio and video are in sync.

Best regards
DAE avatar
Agni451
Posts: 12
Joined: Fri Jan 28, 2011 3:09 am

Re: Audio delay issues when extracting part of ts file

Post by Agni451 »

barbatrukko wrote:If you have .TS file (with mpg2) you can use ProjectX for do an accurate cut of commercial. After this you have 2 choice:
1) save new file in TS and then use DGiNV to demux
2) save new file already demuxed by ProjectX. So you can index the video only and process it separately.
When you remux audio and video are in sync.

Best regards
Thanks, but ProjectX simply can't handle the HD video I work with.
DAE avatar
Agni451
Posts: 12
Joined: Fri Jan 28, 2011 3:09 am

Re: Audio delay issues when extracting part of ts file

Post by Agni451 »

Any thoughts? Did you get the sample I posted?
User avatar
admin
Posts: 4551
Joined: Thu Sep 09, 2010 3:08 pm

Re: Audio delay issues when extracting part of ts file

Post by admin »

I have the file but the instructions are baffling me. All this ffprobe stuff and CSV files! I don't know what ffprobe is or why it is used here. Can you give me a simple process using just my software that illustrates your problem. I'll try to help but I can't make a big research project out of it. Just understanding what you gave me looks like several hours of work.
DAE avatar
Agni451
Posts: 12
Joined: Fri Jan 28, 2011 3:09 am

Re: Audio delay issues when extracting part of ts file

Post by Agni451 »

Sorry for the late reply; I've been away from my main computer for a week now. Anyway, I have a new example to show you without all the ffprobe stuff (ffprobe comes with ffmpeg and prints out pts and offset info for each frame of video or audio in a file).

In the 7z file I uploaded ("NewExample.7z") to the skydrive account mentioned earlier, there is a picture that should indicate exactly what the problem is that I'm trying to get at. For the example,I did not include the "FULL Clip" mentioned in the picture because it is very large. "Piece" is the ts file I trimmed from "FULL Clip" using DGIndexNV and it lacks some audio at the end. I used a hex editor to copy/paste the next 22 audio frames that came after "Piece" in "FULL Clip" to the end of "Piece" to create "Piece Fixed". As you can see, the audio and video now end at the same time. I can copy the next 22 frames manually given the offsets I get from ffprobe, but you know the TS file structure and I'm hoping DGIndexNV can seek to and append the missing 22 audio frames to the end of a trimmed ts file. The number of missing audio frames is calculated as indicated in the picture.

Also, there was some confusion in the previous upload where I wondered if I needed to calculate the number of missing audio frames from the delay at the beginning of the FULL Clip OR using the delay at the I frame where I am trying to cut. The answer is to use the delay from the beginning of the FULL Clip, which should simplify things a bit.

Thanks for looking into this.
User avatar
admin
Posts: 4551
Joined: Thu Sep 09, 2010 3:08 pm

Re: Audio delay issues when extracting part of ts file

Post by admin »

Somehow you are going to have to give me a full sample that I can cut and produce your results. Giving me the already cut pieces will not allow me to do the analysis, implementation, and testing that is needed. Can't you cut the front of the stream away using output trimmed TS, giving me a sample centered around the end cut that you are making?

In general, I can say that cutting a TS can in theory produce this issue if there is an offset between the video and audio in the stream. One thing that can be done is to cut the TS past the point of the last audio sample that is needed and then complete the trimming in the avisynth script.

Beyond that, I need a proper sample to do anything more with your issue.
DAE avatar
Agni451
Posts: 12
Joined: Fri Jan 28, 2011 3:09 am

Re: Audio delay issues when extracting part of ts file

Post by Agni451 »

Okay, I've uploaded a clip "Full Clip" that contains "Piece" from the previous upload. The initial delay of "Full Clip" is the same -682ms mentioned in the picture (according to dgmpgdec's "analyze sync" tool).
User avatar
admin
Posts: 4551
Joined: Thu Sep 09, 2010 3:08 pm

Re: Audio delay issues when extracting part of ts file

Post by admin »

Thank you. Can you please tell me how to process it so as to produce your issue?
DAE avatar
Agni451
Posts: 12
Joined: Fri Jan 28, 2011 3:09 am

Re: Audio delay issues when extracting part of ts file

Post by Agni451 »

You can set the start of the project to anywhere in DGIndexNV, but you should try to set the end in the middle of people talking (try to make the project range >=5 secs). Then trim that piece out, and play it to the end with VLC or MPC. You'll see that the audio stops before the video (by 682ms) no matter where you set the end.
User avatar
admin
Posts: 4551
Joined: Thu Sep 09, 2010 3:08 pm

Re: Audio delay issues when extracting part of ts file

Post by admin »

When you say "trim that piece out" do you mean by using "Output Trimmed TS" or by setting a project range and then making a project and playing the script?

Please try to be complete and precise in your instructions so we can avoid 20 questions and I can get to work on the sample.
DAE avatar
Agni451
Posts: 12
Joined: Fri Jan 28, 2011 3:09 am

Re: Audio delay issues when extracting part of ts file

Post by Agni451 »

1. Open DGIndexNV and open "Full Clip.ts"
2. Set project start anywhere with "["
3. Set project end a bit further on with "]"
4. File --> Output Trimmed TS and save as "temp.ts"
5. Play "temp.ts" in VLC or MPC and see that audio ends before video.

Setting the endpoint in step 3 to a different point and following steps 4 and 5 to create "temp2.ts" will still yield the same amount of missing audio at the end.

A project file (.dgi) is not saved at this point; I simply cut out the pieces I want from the show and save them as "1.ts", "2.ts", etc. Usually there are 5 or 6 pieces per 1 hour show. Unfortunately, all of these pieces are missing a small amount of audio at the end so that the audio cuts out before the video. This small amount of audio is equal to the initial delay of "Full Clip," and this averages around 700ms so it's pretty significant.

I hope this helps reproduce the issue.
User avatar
admin
Posts: 4551
Joined: Thu Sep 09, 2010 3:08 pm

Re: Audio delay issues when extracting part of ts file

Post by admin »

Thank you, that is perfect. Investigating...
DAE avatar
BarryW
Posts: 56
Joined: Fri Mar 18, 2011 7:50 pm

Re: Audio delay issues when extracting part of ts file

Post by BarryW »

I use the 2nd option - add a few seconds of the commercial on at the end.
This guarentees that the audio isn't chopped off due to it being offset by about -1000ms.
After encoding and muxing in the audio I use mkvmerge GUI's split function to cut on the scene change.

The x264 encoded 'I' frame at the scene change can only occur if there's a definite change of picture when the commercial starts.
If the commercial starts with a black frame and the last few frames of the programme are also black then it's very unlikely that an 'I' frame will be created at the scene change.
Also if the commercial starts with a white frame and the last few frames of the programme are black then x264 sometimes creates a 'P' frame where the 'I' should be.
To get an 'I' frame on the scene change the commercial needs to start with a picture that's completely different to the last frame of the programme.
It's a good idea to lower the Minimum GOP size to 10 or 15 to have a better chance of getting an 'I' frame on the scene change.

If this doesn't work the other possibility is to encode a 1080p (Intra) by setting both GOP factors to 1.
It's then possible to cut on any frame using mkvmerge GUI's split function.
Once you have the split file then encode to a standard 720p.

Once you have all the parts encoded use the append function of mkvmerge to glue all the bits together.

TS Packet Editor also has a Timecode correction tick box - this reduces the audio offset from -1000ms down to about -40ms in the edited TS file but it can cause macro-blocking at the start of the TS file. It can also cause audio sync error so it's best avoided.
DAE avatar
Agni451
Posts: 12
Joined: Fri Jan 28, 2011 3:09 am

Re: Audio delay issues when extracting part of ts file

Post by Agni451 »

BarryW wrote:I use the 2nd option - add a few seconds of the commercial on at the end.
This guarentees that the audio isn't chopped off due to it being offset by about -1000ms.
After encoding and muxing in the audio I use mkvmerge GUI's split function to cut on the scene change.
It's a good idea to lower the Minimum GOP size to 10 or 15 to have a better chance of getting an 'I' frame on the scene change.

If this doesn't work the other possibility is to encode a 1080p (Intra) by setting both GOP factors to 1.
It's then possible to cut on any frame using mkvmerge GUI's split function.
Once you have the split file then encode to a standard 720p.

Once you have all the parts encoded use the append function of mkvmerge to glue all the bits together.
I like to do things in one pass if possible. For now, I'm just including extra black frames from the recording at the end of each actual TV segment in order to obtain the last bits of audio. However, some channels simply don't have that fade to black bit before commercials, so I've been just been cutting the audio short because I don't have enough time in my day to go back for a second pass at cutting. I would use ffmpeg to re-encode the mpeg2 so that it uses only I-frames (the GOPs vary widely; I've seen as low as 15 and as high as 60, which is terrible), but that would increase the file size by at least a factor of 2 and I don't have the HDD space for that (these HD files can be 4GB for 720p and over 8GB for 1080i as recorded).

After all the commercials are cut out, I run it through ffmpeg to get rid of the initial delay as well as fill in any missing video or audio frames to maintain sync since I'll be losing the timestamps from the mpeg2 stream once I start re-encoding. Then I use a little program I wrote to generate the scripts needed to automate the indexing of the individual pieces and re-encode them (2-pass constant bitrate AVC). At this point my program calculates how many extra black frames of video and ms of audio are needed to have both streams be exactly the same length (average 1ms difference) in terms of time. It then adds the extra black frames through the avisynth script, and uses delaycut to pad the end of the audio. This way, I can use mkvmerge to append the files without worrying about delays between each piece, and if the mkv file were ever demuxed into its .264 and .ac3 files and remuxed into a different container, there would be no A/V sync issues.

I've spent months perfecting my workflow for this in my free time, and the only thing that isn't quite right is this loss of a small chunk of audio when cutting with DGIndexNV, so I'm hoping admin might be able to find a way to add it when trimming. :)
DAE avatar
BarryW
Posts: 56
Joined: Fri Mar 18, 2011 7:50 pm

Re: Audio delay issues when extracting part of ts file

Post by BarryW »

Re: Trim Method

Here's 9 seconds of HD with the start of an advert - the advert starts on frame 175.
Filename monsterpython.ts
http://db.tt/x9h9URDt

I pre-converted the AC3 to WAV using Audacity 2.0.3

Here's the AVS script:
loadplugin("C:\Program Files\megui\tools\dgindexnv\DGDecodeNV.dll")
DGSource("D:\DVD\monsterpython.dgi", deinterlace=1)
V = Spline64Resize(1280,720).trim(0,174)
A = WAVSource("D:\DVD\monsterpython.wav")
AudioDub(V,A)
#DelayAudio(-0.552)

The MKV has no audio.
The DelayAudio() was commented out so the audio (if it existed) would be out-of-sync.
Trim isn't a problem but I'd like to know how to get audio that's cut at the same point as the video.
DAE avatar
Agni451
Posts: 12
Joined: Fri Jan 28, 2011 3:09 am

Re: Audio delay issues when extracting part of ts file

Post by Agni451 »

Just wondering of there is an update on this issue? I tested 2045 but it still cuts the audio off at the end, no matter which "mismatched audio" option is used.
User avatar
admin
Posts: 4551
Joined: Thu Sep 09, 2010 3:08 pm

Re: Audio delay issues when extracting part of ts file

Post by admin »

In the provided stream the audio is half a second behind. So if you want to cut the TS and have all the audio then you have to cut where the audio ends. Then you have the problem of how to get rid of the extra video.

I cannot make a new TS with zero delay in DGDecNV! It is a decoder, not a muxer. Furthermore, DGDecNV is intended for serving decoded video and audio, and it is easy to solve your problem in that domain. Lastly, the TS cutter is there to facilitate getting samples from longer streams. It is not intended for TS domain editing, and I'm not interested in writing a TS editor.

You can always demux, cut half a second off the front of the audio (e.g., with delaycut or equivalent), and then remux.

The audio mismatch function is not related to your problem. It refers to cases where the detected audio type does not match the type declared in the PMT.
DAE avatar
BarryW
Posts: 56
Joined: Fri Mar 18, 2011 7:50 pm

Re: Audio delay issues when extracting part of ts file

Post by BarryW »

If you don't want to use TS Packet Editor's Timecode correction tick box to reduce the audio offset and you don't want to add a couple of seconds on of the commercial and use one of the other 3 solutions:
1) pre-cutting the audio with Audacity and muxing with trimmed video
2) split on the 'I' frame at the scene change using mkvmerge GUI
3) encode as 1080p intra - post split with mkvmerge GUI and re-encode back to 720p
There's another solution but it's a bit of a bodge.
With Audacity, highlight the last few seconds of audio and use the fade-out function to bring the audio level to zero at the end. When the audio is muxed into the final MKV it'll be shifted to the left (assuming the audio offset is negative) causing no audio at the end. The fade-out disguises the audio being suddenly chopped off. Also if the first cut (of the edited TS file) is late you might want to Investigate the use of Audacity's fade-in at the start of the audio file.

Another subtle problem with BBC HD (or BBC2 HD as it's now called) is audio offset in 6 track AC3. The first cut could be made when only 2 tracks are being broadcast. When the audio is demuxed by DGIndexNV from the TS file it's possible for tracks 3,4,5,6 to be shifted to the left because these tracks didn't exist at the first cut point. To get around this the audio of tracks 3,4,5,6 can be shifted slightly to the right by Audacity to bring them in sync with tracks 1 and 2. The other option is to cut late at the start when all 6 tracks are available and use fade-in. This is a rare problem as 99% of the time only 2 track AC3 is used these days.

It's worth installing Audacity just to take a look at the audio. With Audacity's FFMPEG audio extension library installed it's possible to import and export AC3. Audacity's default export behaviour for 6 track AC3 is to downmix to 2 track - this can be changed in the properties menu to leave it as 6 track if you wish.
Post Reply