Log in

View Full Version : Audio sync help


darkelf5
20th December 2017, 23:30
I've been wrestling with this the whole day.
I have a video that's perfectly synced when being played with MPC-HC
which i'm trying to trim and encode and it's always out of sync no matter
what i've tried. I've indexed it with ffms, lsmash, used fpsnum and fpsden, audiodelay without succes.

The only thing that trims and encodes it with audio synced is xvid4psp but i wanna add some filters that it doesn't support so i wanna do it on my own.

Can anyone try and help me with this? Here's the MediaInfo output:

Video
Count : 338
Count of stream of this kind : 1
Kind of stream : Video
Kind of stream : Video
Stream identifier : 0
StreamOrder : 0
Inform : 15.0 Mb/s, 3840*2160 (16:9), at 23.844 FPS, AVC (Main@L5.1) (CABAC / 3 Ref Frames)
ID : 1
ID : 1
Unique ID : 1
Format : AVC
Format/Info : Advanced Video Codec
Format/Url : http://developers.videolan.org/x264.html
Commercial name : AVC
Format profile : Main@L5.1
Format settings : CABAC / 3 Ref Frames
Format settings, CABAC : Yes
Format settings, CABAC : Yes
Format settings, RefFrames : 3
Format settings, RefFrames : 3 frames
Internet media type : video/H264
Codec ID : V_MPEG4/ISO/AVC
Codec ID/Url : http://ffdshow-tryout.sourceforge.net/
Codec : V_MPEG4/ISO/AVC
Codec : AVC
Codec/Family : AVC
Codec/Info : Advanced Video Codec
Codec/Url : http://ffdshow-tryout.sourceforge.net/
Codec profile : Main@L5.1
Codec settings : CABAC / 3 Ref Frames
Codec settings, CABAC : Yes
Codec_Settings_RefFrames : 3
Duration : 4545534.000000
Duration : 1 h 15 min
Duration : 1 h 15 min 45 s 534 ms
Duration : 1 h 15 min
Duration : 01:15:45.534
Duration : 01:15:15:23
Duration : 01:15:45.534 (01:15:15:23)
Bit rate mode : CBR
Bit rate mode : Constant
Bit rate : 14999552
Bit rate : 15.0 Mb/s
Width : 3840
Width : 3 840 pixels
Height : 2160
Height : 2 160 pixels
Sampled_Width : 3840
Sampled_Height : 2160
Pixel aspect ratio : 1.000
Display aspect ratio : 1.778
Display aspect ratio : 16:9
Frame rate mode : VFR
Frame rate mode : Variable
Frame rate : 23.844
Frame rate : 23.844 FPS
Original frame rate : 23.976
Original frame rate : 23.976 (24000/1001) FPS
FrameRate_Original_Num : 24000
FrameRate_Original_Den : 1001
Frame count : 108383
Resolution : 8
Resolution : 8 bits
Colorimetry : 4:2:0
Color space : YUV
Chroma subsampling : 4:2:0
Chroma subsampling : 4:2:0
Bit depth : 8
Bit depth : 8 bits
Scan type : Progressive
Scan type : Progressive
Interlacement : PPF
Interlacement : Progressive
Bits/(Pixel*Frame) : 0.076
Delay : 291
Delay : 291 ms
Delay : 291 ms
Delay : 291 ms
Delay : 00:00:00.291
Delay, origin : Container
Delay, origin : Container
Stream size : 8479208897
Stream size : 7.90 GiB (99%)
Stream size : 8 GiB
Stream size : 7.9 GiB
Stream size : 7.90 GiB
Stream size : 7.897 GiB
Stream size : 7.90 GiB (99%)
Proportion of this stream : 0.98694
Default : Yes
Default : Yes
Forced : No
Forced : No
Buffer size : 30000000

Audio
Count : 275
Count of stream of this kind : 1
Kind of stream : Audio
Kind of stream : Audio
Stream identifier : 0
StreamOrder : 1
Inform : 192 kb/s, 44.1 kHz, 2 channels, AAC (LC)
ID : 2
ID : 2
Unique ID : 2
Format : AAC
Format/Info : Advanced Audio Codec
Commercial name : AAC
Format profile : LC
Codec ID : A_AAC-2
Codec : AAC LC
Codec : AAC LC
Codec/Family : AAC
Duration : 4545676.000000
Duration : 1 h 15 min
Duration : 1 h 15 min 45 s 676 ms
Duration : 1 h 15 min
Duration : 01:15:45.676
Duration : 01:15:44:17
Duration : 01:15:45.676 (01:15:44:17)
Bit rate : 192000
Bit rate : 192 kb/s
Channel(s) : 2
Channel(s) : 2 channels
Channel positions : Front: L R
Channel positions : 2/0/0
ChannelLayout : L R
Samples per frame : 1024
Sampling rate : 44100
Sampling rate : 44.1 kHz
Samples count : 200464304
Frame rate : 43.066
Frame rate : 43.066 FPS (1024 SPF)
Frame count : 195409
Compression mode : Lossy
Compression mode : Lossy
Delay : 0
Delay : 00:00:00.000
Delay, origin : Container
Delay, origin : Container
Delay relative to video : -291
Delay relative to video : -291 ms
Delay relative to video : -291 ms
Delay relative to video : -291 ms
Delay relative to video : -00:00:00.291
Video0 delay : -291
Video0 delay : -291 ms
Video0 delay : -291 ms
Video0 delay : -291 ms
Video0 delay : -00:00:00.291
Stream size : 110799774
Stream size : 106 MiB (1%)
Stream size : 106 MiB
Stream size : 106 MiB
Stream size : 106 MiB
Stream size : 105.7 MiB
Stream size : 106 MiB (1%)
Proportion of this stream : 0.01290
Default : Yes
Default : Yes
Forced : No
Forced : No

wonkey_monkey
20th December 2017, 23:40
Not my strong suit, but I'm guessing that this:

Frame rate mode : Variable

is going to be a problem. Avisynth doesn't do variable frame rates.

GMJCZP
21st December 2017, 00:04
I think of the following, extract the audio and convert it to WAV, then join it to the video and do the trim, the audio that is going to be left should be synchronized. These problems happen when working with variable audio.

darkelf5
21st December 2017, 00:51
Nope, still the same issue.

GMJCZP
21st December 2017, 03:07
Try with DSS2 Mod for index the video, convert it to a lossless format, the audio in wav attach it and check if it is synchronized.

StainlessS
21st December 2017, 05:13
You could try below BAT file


setlocal

REM Where to Find ffmpeg
set FFMPEG="C:\BIN\ffmpeg.exe"

REM Where to get input file, No terminating Backslash, "." = current directory
set INDIR="."


REM Where to place output file, No terminating Backslash.
set OUTDIR="D:"


FOR %%A IN (*.wmv *.mpg *.avi *.flv *.mov *.mp4 *.m4v *.RAM *.RM *.mkv *.TS *.ogv) DO (
%FFMPEG% -i "%INDIR%\%%A" -vcodec utvideo -acodec pcm_s16le "%OUTDIR%\%%~nxA.AVI"

)

Pause

It dont like Spaces in filenames.
REM lines are comments
Output UT_Video video and PCM audio.

Perhaps something like this afterwards in avs Script

AssumeFPS(24000,1001,True)
ReSampleAudio(44100) # or 48000

darkelf5
21st December 2017, 20:02
Indexing the original video with DSS2 i get an Wait for FrameReady error.

Converting the video to utvideo with ffmpeg and loading it with DSS2 (which shows frame rate as 23.000) with the wav attached works but it's also out of sync when i try to trim it even with manually setting the fps either in DSS2 or via AssumeFPS.

It doesn't matter, thanks for your help guys.

StainlessS
21st December 2017, 21:27
Did you try with AviSource (always good idea to try first with AVI lossless), followed by the given script ?

EDIT: I thought ffmpeg as in given bat, converts to constant framerate (inserting frames as needed).

EDIT: Might also be worth trying VirtualDubFilterMod, and save as AVI with UT_Video and PCM audio (with Fast Recompress).

GMJCZP
21st December 2017, 21:52
darkelf5, try what TinMan tells you, in the past I solved problems of desynchronization with AVI with audio in MP3 VBR.

EDIT: did you use DDS2 or DSS2 Mod as I suggested?

darkelf5
22nd December 2017, 00:19
@StainlessS
Well it converts it to constant 23.000 fps. Just tried AviSource - same problem.

@GMJCZP
DSS2Mod

Gonna try VirtualDubFilterMod.

lansing
22nd December 2017, 01:16
Is your video vfr?

darkelf5
22nd December 2017, 01:55
MediaInfo says it is.


Frame rate mode : VFR
Frame rate mode : Variable
Frame rate : 23.844
Original frame rate : 23.976

poisondeathray
22nd December 2017, 02:22
You would need to convert to CFR before editing. This is true for almost everything (other video editing applications), not just avisynth

I'm guessing you already did that with xvid4psp, the only thing that "worked" for you

The only thing that trims and encodes it with audio synced is xvid4psp but i wanna add some filters that it doesn't support so i wanna do it on my own.


Have a look at the script it used . Look at the log files. I bet it was something like DirectShowSource("video.ext" , fps=23.976, convertfps=true)

But it's not "safe" to use temporal filters with directshow . So if you were planning on those types of filters, I would use that to encode an intermediate first

lansing
22nd December 2017, 02:33
MediaInfo says it is.

That mediainfo you posted looks very confusing to me. By VFR it usually means that the video change frame rate something like 30fps to 24fps on playback, 23.844 to 23.976 is probably some false alarm.

You'll have to play the original file and check it yourself. For example, play it with media player classic, turn on view->statistics, jump around in the video and look at the playback rate.

darkelf5
22nd December 2017, 03:07
MPC statistics shows that frame rate is about 23 ,so it must be the audio issue, right?
xvid4psp converts it to 23.976 but i don't think that the new versions use AviSynth so there's no script to inspect.

poisondeathray
22nd December 2017, 03:55
MPC statistics shows that frame rate is about 23 ,so it must be the audio issue, right?


Not necessarily


xvid4psp converts it to 23.976 but i don't think that the new versions use AviSynth so there's no script to inspect.


It doesn't matter. It's still the right answer and source filter if you want to edit and use avisynth . The other methods are slightly less accurate and can have slight problems with frame placement. But this assumes your directshow system is configured correctly.


In fact, you can just check it really quickly in MPCHC , jump around a bit. If it's in sync there (assuming you can playback UHD in realtime), then it's almost 100% guarenteed to work if you encode an intermediate first before editing / other filters


What lansing is probably suggesting by "false alarm" , is that there is a small chance you can just assume a CFR framerate by overwriting timecodes, e.g. with mp4fpsmod. Because it looks like it's minimally variable if you assume mediainfo is correct. You would have to extract the timecodes and examine it to be sure. The benefit would be no large intermediate file, and you could use "normal" source filter like lsmashvideosource)

lansing
22nd December 2017, 03:56
MPC statistics shows that frame rate is about 23 ,so it must be the audio issue, right?
xvid4psp converts it to 23.976 but i don't think that the new versions use AviSynth so there's no script to inspect.

To check where the problem came from, convert your audio to WAV and then open it with the original video, the audio should be out of sync by some milliseconds, just adjust it with the + and - key in number pad if you're playing with mpc, and then remember that delay.

Then did a quick convert of the video to create a test video, like encode it with ultrafast mode in x264 and resolution 640x360, and then play that test video with the wav, use the same delay you found before. If they're in sync and the whole time, then the problem should be just the audio delay. If they're out of sync, the problem is the video.

tebasuna51
22nd December 2017, 11:44
I can try other approach to preserve A/V sync. For that I need:

1) I don't know your source container from the initial MediaInfo.
Please obtain a Source.mkv from it, renaming or converting with MkvToolNix.

2) Extract the timestamps from video and audio tracks of Source.mkv:
..."mkvextract.exe" "Source.mkv" timestamps_v2 0:"Source.mkv_ 0.txt" 1:"Source.mkv_ 1.txt"

zip "Source.mkv_ 0.txt" and "Source.mkv_ 1.txt" and upload here (Attach)

3) Put here your avs, at least with Trim's an any other function than change fps.

sneaker_ger
22nd December 2017, 13:02
what i've tried. I've indexed it with ffms, lsmash, used fpsnum and fpsden, audiodelay without succes.
Are you running both video and audio through the script or just video, but audio separately?

E.g.:

a=ffaudiosource("source.mkv")
v=ffvideosource("source.mkv", fpsnum=24000, fpsden=1001)
AudioDub(v, a)

If you are running both through the script and it async (but playing the original file in e.g. MPC-HC is sync) then its an ffms bug and should be reported on their bug tracker (along with the file).

darkelf5
22nd December 2017, 13:07
Manually adjusting the sync works although it gets desynced near the end.


v = LWLibavVideoSource("video.mkv", fpsnum=24000, fpsden=1001)
a = WAVSource("audio.wav")
AudioDub(v, a)
Dissolve(Trim(0, 19478), Trim(25962, 55793), 18)
FadeIO(1)

Can't attach the timecodes as they exceed the 300kb limit
http://www12.zippyshare.com/v/d7CYgXUP/file.html

@sneaker_ger
I've tried importing both external wav and internal audio. The results are the same.

sneaker_ger
22nd December 2017, 13:20
So it's async even with simple script like:
a=ffaudiosource("source.mkv")
v=ffvideosource("source.mkv", fpsnum=24000, fpsden=1001)
AudioDub(v, a)
?
Also tested ffms2000 (https://forum.doom9.org/showthread.php?t=174469)?
Then please report to ffms devs: https://github.com/FFMS/ffms2/issues

But if it's the same problem with all source filters .... fishy.

tebasuna51
22nd December 2017, 15:04
Can't attach the timecodes as they exceed the 300kb limit
http://www12.zippyshare.com/v/d7CYgXUP/file.html

Sorry, I can't help you with that kind of timestamps (irregulars in video and audio).
I don't know how can be managed inside AviSynth.

poisondeathray
22nd December 2017, 16:14
I've edited thousands of vfr files. In avisynth and other programs. I'm telling you from a lot of experience, this is the 1 case where directshow is 10x better than everything else . (Usually you should avoid it like the plague). Much more consistent and places frames more accurately than the others.


DirectShowSource("video.ext" , fps=23.976, convertfps=true)


Whether or not you can use Trim() directly in that script, depends on your directshow filters, your splitter and decoder. The safest thing to do would be to encode that to an I-frame intermediate first, then apply whatever filters you want.

manolito
22nd December 2017, 16:48
Basically I do agree, but there are some misconceptions about DSS2Mod here...

DSS2Mod is DirectShow based. It will always convert VFR to CFR, i.e. "convertfps=true" is not needed. It will NOT index the source, but there is a "preroll" parameter which is extremely useful for difficult sources (default is 0, a value of 15 works well for most sources, and it should be raised to 100 or even higher for difficult sources). In my experience it works best when LAV Filters are used as the DirectShow source filters.

DSS2Mod does not handle audio, DirectShowSource should be used for audio. IMO this combination beats ffms2 for most sources (it probably does not beat DG's tools).


Cheers
manolito

poisondeathray
22nd December 2017, 17:17
Basically I do agree, but there are some misconceptions about DSS2Mod here...

DSS2Mod is DirectShow based. It will always convert VFR to CFR, i.e. "convertfps=true" is not needed. It will NOT index the source, but there is a "preroll" parameter which is extremely useful for difficult sources (default is 0, a value of 15 works well for most sources, and it should be raised to 100 or even higher for difficult sources). In my experience it works best when LAV Filters are used as the DirectShow source filters.




The problem with DSS2Mod, is it often drops one or two frames on some sources (usually at the end) , sometimes green frame at the beginning , regardless of preroll. This is repeatable behaviour.

I've literally done thousands (closer to tens of thousands) of tests on several different setups from different sources - capture sources, camera sources, web source - This is a huge sample size. I'm very confident making these claims




And for FFMS2 / lsmash - the conversion usually puts frames in not quite the right spots . You can tell in a NLE when looking at the audio waveform. Directshow is closer to correct. Again tested on thousands of sources

Midzuki
22nd December 2017, 20:17
When ffms2 gives me sync issues, DirectShowSource on the graphs themselves is the solution.
Together with LAV Splitter, LAV Video and LAV Audio, bien entendu.


vide0=DirectShowSource("video.grf", audio=false)
audi0=DirectShowSource("audio.grf", video=false)
AudioDub(vide0, audi0)

Midzuki
22nd January 2018, 11:43
For the record...

1) Last week I messed around with some ancient ASF files, and I have to say that FFMS2 is unable to deal with them properly;

2) This morning I downloaded a couple of .WEBMs from YouTube and verified that they too are "problematic" to FFMS2 :/

But again, GraphStudioNext and DirectShowSource() saved the day.