Log in

View Full Version : Audio Sync problems with FFmpegSource2


Roemer
7th November 2020, 23:34
Hello all

I am doing some VHS -> mkv conversions. For that, I use an USB-Grabber and ffmpeg to record the video from a real VHS-player, then add some trimming and filters in Avisynth+ and re-encode again with ffmpeg to the final format.

The problem I am now facing is that after recording (with like:
ffmpeg -f dshow -rtbufsize 2048M -video_size 720x480 -i video="AV TO USB2.0":audio="Mikrofon (USB2.0 MIC)" -c:v libx264 -crf 6 -preset medium -tune grain -c:a libmp3lame -b:a 320k -f tee -map 0:v -map 0:a "E:/Recordings/video_x.mp4|[f=nut]pipe:" | ffplay pipe:
I get a working mp4 which plays absolutely fine in ffplay or vlc.
Now when I open that file in avisynth and play that in ffmpeg, the audio is getting more and more async as the video progresses.

I tried checking if the video has a variable framerate with ffmpeg -i "E:\Recordings\file.mp4" -vf vfrdet -f null - but that shows something like VFR:0.000000 (0/53131) so I guess it is not variable.
I also tried adjusting the FPS in avisynth with AssumeFPS(FrameCount()/AudioLengthF() * c.AudioRate(), False) which also does not really help.

I am now ending up just using TimeStretch and just try increasing or decreasing the tempo by small steps until the video and audio at the end seems about right. But that is cumbersome and far from perfect.

Does anyone have the same issue? Or could help me find the problem?
Help would be greatly appreciated as I have dozens of VHS with Camera-Recordings of my family to convert.

Many thanks in advance
Roman

FranceBB
13th November 2020, 23:13
Uhm... let me get this straight, you record a video from a capture card, the video is fine when you use ffplay or other players, but when you index it with ffms2 in Avisynth, audio is out of sync?
Have you tried specifying fpsnum and fpsden in FFMpegSource2()?
Besides,
x264 -crf 6 -preset medium -tune grain
isn't the easiest thing to encode and it may drop frames. How about going to a lightweight lossless codec first and then re-encode to your target destination?
Also 'cause, even though crf 6 is pretty low, it's not lossless, so you're essentially going to have the lossy of the lossy.
I think using utvideo or huffyuv would be far better and it should also solve your audio desync issue.
About the audio itself,
-c:a libmp3lame
is a very poor choice.
Nobody should be using MP3 in 2020...
Just save it as PCM lossless 24bit (or 32bit) and then re-encode it to whatever you want (and if it has to be lossy, AAC would outperform MP3 in everything).

wonkey_monkey
14th November 2020, 00:38
Have you tried an MKV container instead of MP4?

poisondeathray
14th November 2020, 00:43
Which ffms2 version? Have you tried a different one ?

How about LSmash instead of ffms2 ?

Roemer
16th November 2020, 09:36
Hello all

Many thanks for the answers and sorry I didn't get back to you faster:

Yes, I record with an USB grabber. When recording, ffmpeg shows about 1 dropped frame per hour.
Exactly, only when loading the file in avisynth/ffms2 it is asynchronous. Every other player does not have any issues playing it. I also tried an mkv container, same issue.

I use ffms2-2.40 and also tried ffms2-2.23.1, same problem. Never heard of LSmash, but I just tried it and for the audio (LSMASHAudioSource), I always get no valid audio frame.

I fear lossless recording as that might take too much diskspace? I just did a minute recording with the following:
ffmpeg -f dshow -rtbufsize 2048M -video_size 720x480 -i video="AV TO USB2.0":audio="Mikrofon (USB2.0 MIC)" -c:v huffyuv -c:a copy -f tee -map 0:v -map 0:a "E:/Recordings/video_x.avi|[f=nut]pipe:" | ffplay pipe:
The input audio seems to be Audio: pcm_s16le, 44100 Hz, stereo, s16, 1411 kb/s so I guess c:a copy should be fine right?

A minute of recording then yielded to a 460mb file. So that whould be a bit more than 100 GB for a 4h VHS, which would be fine. I think I will give that a try.

Thanks so far, I report back about the lossless recording.

kedautinh12
16th November 2020, 10:42
Are you try?
https://forum.doom9.org/showthread.php?p=1925683#post1925683

Roemer
16th November 2020, 13:33
Are you try?
https://forum.doom9.org/showthread.php?p=1925683#post1925683

I tried this one (with deleting the index of course) => no change

Roemer
16th November 2020, 22:59
I now tried the raw recording and it is actually way worse than the mp4. It now has a massive delay from the start on (almost a second) and and the end of the video it is even worse. I have absolutely no idea what is going on.

hello_hello
18th November 2020, 09:03
If it's VFR, the only way to convert it to CFR with Avisynth without messing up the A/V sync is to tell the source filter to decode at the the correct frame rate. If it's VFR, FFMS2 and lsmash default to the average frame rate. Try adding the following FFMS2 arguments, specifying the expected frame rate. FFMS2 will add/drop frames as required while decoding.

FFMS2("source", fpsnum=30000, fpsden=1001)

AssumeFPS and TimeStretch etc won't fix it if it's VFR.
Try adding Info() to the end of the script to see what Avisynth reports for the frame rate. If the total frame count changes after adding the frame rate conversion, it's probably VFR (assuming you specified the correct rate).