ammen99 / wf-recorder

MIT License
859 stars 63 forks source link

How to adjust audio latency? #264

Open Botspot opened 3 months ago

Botspot commented 3 months ago

My application involves voice narration and a video feed of the webcam, inside a small mplayer window on-screen to be captured along with everything else. The problem is that the webcam previewer has a bit of lag, causing the audio to play about 0.5s ahead of the video.

Is there a easily known way to tell ffmpeg to add latency to the audio? I would rather not do this with post-processing.

soreau commented 3 months ago

Are you using gstreamer for the webcam preview as mentioned in the wiki?

Botspot commented 3 months ago

Are you using gstreamer for the webcam preview as mentioned in the wiki?

No, that seemed to perform very poorly, and the window could not be resized or moved. I was using this:

mplayer -vo xv tv:// -tv driver=v4l2:device=/dev/video0:width=640:height=480:fps=30

Edit: mplayer appears to be running under x11. That may be contributing to the latency.

soreau commented 3 months ago

Maybe try something like mpv --gpu-context=wayland --hwdec=vaapi.

Botspot commented 3 months ago

I've asked for help here: https://forums.raspberrypi.com/viewtopic.php?t=371908

soreau commented 3 months ago

For what it's worth, you should be able to resize the gst-launch window with the wayfire resize binding, the default binding is here. (Super+Right Click Drag)

Botspot commented 3 months ago

mplayer appears to be running under x11. That may be contributing to the latency.

It was, and also made the entire recording lag. This works extremely well.

mpv av://v4l2:/dev/video0 --profile=low-latency --untimed=yes --video-latency-hacks=yes --wayland-disable-vsync=yes --video-sync=display-desync

I would still like to delay the audio just slightly, so not closing this issue quite yet, but I suggest you add this to the wiki in case it helps others.

Botspot commented 3 months ago

Is there a way to pass arbitrary flags to ffmpeg? I think the -itoffset flag does what I want.

Things I've tried:

yes | wf-recorder -a -f /tmp/video.mkv -c libx264 -p preset=ultrafast -r 30 -F=-itsoffset=5
#fails
yes | wf-recorder -a -f /tmp/video.mkv -c libx264 -p preset=ultrafast -r 30 -P=-itsoffset=5
#has no effect
soreau commented 3 months ago

I think you might want --codec-param="-itsoffset=5"

Botspot commented 3 months ago

I think you might want --codec-param="-itsoffset=5"

No change. For ffmpeg, -itsoffset must be specified before the input you want to offset, in case that detail becomes important.