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 > Capturing and Editing Video > Avisynth Usage

Reply
 
Thread Tools Search this Thread Display Modes
Old 28th August 2013, 16:52   #1  |  Link
Stereodude
Registered User
 
Join Date: Dec 2002
Location: Region 0
Posts: 981
AVIsynth source plugin for MPEG-2 .ts files like Handbrake's input file handler?

Does anyone know of a source plugin that works with broadcast ATSC MPEG-2 .ts files that can accept a edited .ts file (with commercials removed) but keeps the audio and video in sync through the edit points?

I have used DGMPGDec for MPEG-2 content for years and years, but always found I had to process an edited .ts file in segments and join them all together at the end after carefully cutting or padding the .ac3 audio to match the video length in order to keep the audio and video in sync. It's doable, but it's pretty tedious and time consuming. Usually I found it worked better to take the original unedited .ts file and cut it into pieces rather than trying to use the edited file since the calculated AC3 delays won't necessarily be correct when cutting up an edited file, but are if you cut up an unedited file into segments to work with.

I tried Handbrake on a whim to convert something for my tablet and was surprised to see it will take a edited .ts file with the commercials removed and it will remove excess .ac3 audio or excess video frames around the edit points and spit out a file that's got perfect A/V sync without pretty much no effort. Of course it's not as versatile and I miss MCTD and other AVIsynth plugins, but boy it sure is easy.

So, back to my question... Is there a source plugin for AVIsynth that can do the same?

Last edited by Stereodude; 28th August 2013 at 16:55. Reason: typo
Stereodude is offline   Reply With Quote
Old 29th August 2013, 16:14   #2  |  Link
Guest
Guest
 
Join Date: Jan 2002
Posts: 21,923
DGDecNV can do it for AC3 audio.
Guest is offline   Reply With Quote
Old 29th August 2013, 22:00   #3  |  Link
Stereodude
Registered User
 
Join Date: Dec 2002
Location: Region 0
Posts: 981
Quote:
Originally Posted by neuron2 View Post
DGDecNV can do it for AC3 audio.
Hmm... I've been using DGDecNV for years, but admittedly haven't use it for much beyond Blu-Ray sources. So if I load one of my edited .ts files it will output an .ac3 file that's been trimmed or padded (with silence) appropriately at the edit points to stay in sync with the decoded video stream it serves into AVIsynth? I'm gonna have to try this when I get home.

Stereodude is offline   Reply With Quote
Old 30th August 2013, 04:26   #4  |  Link
Stereodude
Registered User
 
Join Date: Dec 2002
Location: Region 0
Posts: 981
Quote:
Originally Posted by Stereodude View Post
I'm gonna have to try this when I get home.
Okay, so I tried it. The audio and video don't stay in sync. It seems to do the same thing as DGMPGDec. Is there some setting that needs to be used?

I dropped the file in DGDecNV, enabled audio processing and saved the project. It demuxed the audio and I encoded the video with x264 and then put the two together in a .mkv. I used the delay generated by DGDecNV and the audio was good through the first segment (before the first edit point where a commercial was removed). The A/V sync got worse as the file went on.
Stereodude is offline   Reply With Quote
Old 30th August 2013, 04:38   #5  |  Link
Guest
Guest
 
Join Date: Jan 2002
Posts: 21,923
Sorry, I did a drive-by post without properly reading your post. DGDecNV can't help with that. I was thinking of loading multiple files and adjusting the joints.

Can somebody help Stereodude with this?
Guest is offline   Reply With Quote
Old 30th August 2013, 05:19   #6  |  Link
Stereodude
Registered User
 
Join Date: Dec 2002
Location: Region 0
Posts: 981
Just as a point of reference, here's some of what Handbrake has in the log for one of these files. I've highlighted the lines that indicate the sort behavior I'm hoping to replicate in an AVIsynth source filter.

[01:06:38] file is MPEG Transport Stream with 188 byte packets offset 0 bytes
[01:06:38] reader: first SCR 1384890927 id 0x11 DTS 1384890927
[01:06:38] encx264: min-keyint: 30, keyint: 300
[01:06:38] encx264: Encoding at constant RF 21.000000
x264 [warning]: --psnr used with psy on: results will be invalid!
x264 [warning]: --tune psnr should be used if attempting to benchmark psnr!
x264 [info]: using cpu capabilities: MMX2 SSE2Fast SSSE3 SSE4.2 AVX AVX2 FMA3 LZCNT BMI1 BMI2
x264 [info]: profile High, level 3.1
[01:06:38] output track 1: ac3 in sync after skipping 592 bytes
[01:06:38] mpeg2: "Chapter 1" (1) at frame 0 time 9009
[01:06:38] sync: first pts is 9009
[01:22:39] output track 1: ac3 in sync after skipping 1736 bytes
[01:22:39] output track 1: ac3 in sync after skipping 616 bytes
[01:32:11] output track 1: ac3 in sync after skipping 1024 bytes
[01:32:11] output track 1: ac3 in sync after skipping 432 bytes
[01:32:11] sync: adding 110 ms of silence to audio 0x14 start 75638706, next 75628800
[01:43:55] output track 1: ac3 in sync after skipping 1696 bytes
[01:43:55] sync: audio 0x14 time went backwards 98 ms, dropped 3 frames (start 111266656, next 111275520)
[01:43:58] sync: video time didn't advance - dropped 11 frames (delta 360 ms, current 111355134, next 111355693, dur 559)
[01:55:24] output track 1: ac3 in sync after skipping 1760 bytes
[01:55:24] sync: audio 0x14 time went backwards 72 ms, dropped 2 frames (start 145065800, next 145072320)
[01:55:27] sync: video time didn't advance - dropped 7 frames (delta 200 ms, current 145142446, next 145145396, dur 2950)
[02:08:06] output track 1: ac3 in sync after skipping 1416 bytes
[02:08:06] output track 1: ac3 in sync after skipping 616 bytes
[02:08:06] sync: audio 0x14 time went backwards 46 ms, dropped 1 frames (start 179032017, next 179036160)
[02:08:09] sync: video time didn't advance - dropped 5 frames (delta 142 ms, current 179109326, next 179111559, dur 2233)

[02:27:08] hb_ts_stream_decode - eof
[02:27:08] reader: done. 13 scr changes
[02:27:13] work: average encoding speed for job is 15.941417 fps
[02:27:13] sync: got 77051 frames, 79286 expected
[02:27:14] render: lost time: 43357314 (0 frames)
[02:27:14] render: gained time: 43357314 (57740 frames) (0 not accounted for)
[02:27:14] mpeg2 done: 77074 frames
Stereodude is offline   Reply With Quote
Old 2nd September 2013, 11:22   #7  |  Link
Mole
Registered User
 
Mole's Avatar
 
Join Date: Oct 2001
Location: Thailand
Posts: 260
Have you tried editing it in avidemx?

You should be able to load that unedited .ts into avidemux, then cut out whatever portions you don't want (like in virtualdub), then save out to a new .ts.

Both audio and video should be perfectly in synch.

Then you can just frame serve that new video with DGMPGDec/DGDecNV to avisynth and do whatever.
Mole is offline   Reply With Quote
Old 3rd September 2013, 17:18   #8  |  Link
Stereodude
Registered User
 
Join Date: Dec 2002
Location: Region 0
Posts: 981
Quote:
Originally Posted by Mole View Post
Have you tried editing it in avidemx?

You should be able to load that unedited .ts into avidemux, then cut out whatever portions you don't want (like in virtualdub), then save out to a new .ts.

Both audio and video should be perfectly in synch.

Then you can just frame serve that new video with DGMPGDec/DGDecNV to avisynth and do whatever.
No, I haven't. I'm trying it though I can tell I already don't really like it. It wants to index the file before I can do anything with it. That's a ~7 minute operation for an hour long .ts recording. Normally I've been using hdtvtompeg2 which will open the file instantly without any sort of indexing. It's also a bit more tedious to use, but I suppose that gripe goes away with practice.

FWIW, it appears that AVIdemux isn't really editing the native TS. It seems to demux the streams, cut them up, and then make a new .ts file with all new timecode, PIDs, etc all in one step.

However, I edited a new recording with it and will see if the audio stays in sync. If it works, I suppose this is an option, but doesn't do me any good for recordings that were already edited.
Stereodude is offline   Reply With Quote
Old 3rd September 2013, 18:10   #9  |  Link
Stereodude
Registered User
 
Join Date: Dec 2002
Location: Region 0
Posts: 981
I think I have overlooked a relatively simple solution (assuming it works). Extract the AC3 stream from the file Handbrake creates and generate an alternate video stream done just the way I want using DGMPGDec + AVIsynth. I figure this should be possible since it seems Handbrake makes all the time sync adjustments to the audio stream.
Stereodude is offline   Reply With Quote
Old 3rd September 2013, 23:57   #10  |  Link
Stereodude
Registered User
 
Join Date: Dec 2002
Location: Region 0
Posts: 981
Quote:
Originally Posted by Stereodude View Post
I think I have overlooked a relatively simple solution (assuming it works). Extract the AC3 stream from the file Handbrake creates and generate an alternate video stream done just the way I want using DGMPGDec + AVIsynth. I figure this should be possible since it seems Handbrake makes all the time sync adjustments to the audio stream.
Well, this idea sure didn't work. It seems DGMPGDec and the video decoder in Handbrake don't spit out the same amount of video frames when fed the same .TS file.

Now to try the AVIdemux created .TS file.
Stereodude is offline   Reply With Quote
Old 4th September 2013, 02:16   #11  |  Link
Stereodude
Registered User
 
Join Date: Dec 2002
Location: Region 0
Posts: 981
Quote:
Originally Posted by Stereodude View Post
Now to try the AVIdemux created .TS file.
Converting the .TS file from AVIdemux using AVIsynth for video and using the demuxed source .ac3 file worked. The A/V sync was maintained. I did encounter some odd behaviour where DGMPGDec wouldn't extract the AC3 audio from the .TS. EAC3to extracted it fine though.
Stereodude is offline   Reply With Quote
Old 4th September 2013, 11:33   #12  |  Link
Guest
Guest
 
Join Date: Jan 2002
Posts: 21,923
Quote:
Originally Posted by Stereodude View Post
I did encounter some odd behaviour where DGMPGDec wouldn't extract the AC3 audio from the .TS.
I can look at that if you'd like to post a sample.
Guest is offline   Reply With Quote
Old 4th September 2013, 13:33   #13  |  Link
Stereodude
Registered User
 
Join Date: Dec 2002
Location: Region 0
Posts: 981
Quote:
Originally Posted by neuron2 View Post
I can look at that if you'd like to post a sample.
Nevermind, seems it was user error. I mistakenly concluded that it would automatically extract track 0 when I had it set to "Demux Tracks" based on the wording in the User Manual.
Quote:
Note that for transport and PVA streams, only one audio stream can be demultiplexed at a time, so audio id = 0 is always used. In fact, in these cases, it makes little sense to specify the track individually, and Demux All Tracks is more convenient...
Once I explicitly set it to 0 instead of leaving it blank or tried "Demux All Tracks" it extracted the audio.
Stereodude is offline   Reply With Quote
Old 14th January 2014, 22:52   #14  |  Link
Stereodude
Registered User
 
Join Date: Dec 2002
Location: Region 0
Posts: 981
Quote:
Originally Posted by Stereodude View Post
Converting the .TS file from AVIdemux using AVIsynth for video and using the demuxed source .ac3 file worked. The A/V sync was maintained.
I wanted to come back and touch on this again since I found that some of the .TS files AVIdemux creates have AC3 timestamp gaps in the audio that are audible and annoying. This only seems to happen for recordings made from the local NBC affiliate. Recordings from other channels seem fine.

If I run the AVIdemux created .ts file through MPEG2Repair I get a log file full of these sorts of messages.
Code:
Sequence Frame 38(9-B) / Time 0:00:01 :
AudioWarning: Timestamp gap of 0.032000 sec. ending at file offset 2093966

Sequence Frame 423(4-B) / Time 0:00:14 :
AudioWarning: Timestamp gap of 0.032000 sec. ending at file offset 25459922

Sequence Frame 1127(3-B) / Time 0:00:37 :
AudioWarning: Timestamp gap of 0.032000 sec. ending at file offset 66644894

Sequence Frame 1831(5-P) / Time 0:01:01 :
AudioWarning: Timestamp gap of 0.032000 sec. ending at file offset 110341734
The same recording processed with HDTVtoMPEG2 does not have those gaps, and plays back fine from an audio continuity standpoint (it still has the original problem with A/V sync when using the audio and video from the .ts file).

However, I've subsequently tried outputting to mkv from AVIdemux instead of .ts and haven't found any issues with the AC3 audio during playback with MPC-HC. Unfortunately .mkv files can't be handled by DGMPGDec (though it will open them). DGDecNV, which can reportedly handle a .mkv, doesn't have deblocking capability for MPEG-2 like DGMPGDec does, so I still haven't really found a way to edit the .ts recording to a file I can both play back using MPC-HC to my TV and feed into AVIsynth with good audio sync. I know the MPEG-2 stream can be extracted from the .mkv but that's an extra step that makes things more complicated. I also still need to see if the extracted streams from the .mkv stay in sync or not after re-encoding and muxing.

Last edited by Stereodude; 18th January 2014 at 22:12.
Stereodude is offline   Reply With Quote
Old 18th January 2014, 22:13   #15  |  Link
Stereodude
Registered User
 
Join Date: Dec 2002
Location: Region 0
Posts: 981
Quote:
Originally Posted by Stereodude View Post
I also still need to see if the extracted streams from the .mkv stay in sync or not after re-encoding and muxing.
FWIW, in the one test I've tried so far the extracted MPEG-2 and AC3 streams from a .mkv output by AVIdemux (from a edited ATSC .TS file) do stay in sync after the video has been run through AVIsynth, compressed, and remux'd with the AC3 audio stream spliced by AVIdemux.

Last edited by Stereodude; 18th January 2014 at 23:31. Reason: clarify
Stereodude is offline   Reply With Quote
Reply

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 18:00.


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