Audio offset error fix for 4K video after render in kdenlive

Intro

If you've used kdenlive to work with 4K video, you likely experienced the problem when playing rendered 4K video clip, audio plays before the video. This feels pretty unsettling and ruins the video for anyone who tries to watch it.

Resizing down to 1080p

I've tired several workarounds like, working in 1080p projects while using 4K clips, or rescaling the final video to 1080p, but none of these approaches worked.

Kdenlive rendering configuration page

The most simple and reliable thing that wast to resize 4K clips down to 1080p and then work with them in a 1080p project.

Here is the command that given a file, outputs downscaled file_1080p.mp4 utilizing NVIDIA hardware.

#!/bin/bash

/usr/bin/ffmpeg -i ${1} \
    -c:v h264_nvenc \
    -vf scale=1920:1080 \
    -c:a aac \
    -preset lossless \
    -qmax 100 \
    -crf 0 \
    "${1%.*}"_1080p.mp4

This all meant it was not possible to produce or easily crop 4K video in kdenlive.

4K fix (workaround)

Turns out the bug is in MLT framework, and while the project is getting around to make a fix for the issue, Kdenlive 19.12.1 contains a workaround written by j-b-m, and I've tested it in MLT issue #231

The workaround consists of a new option in clip properties -- Audio sync. Setting audio sync to -170ms.

kdenlive clip properties including Audo Sync

You will need to do this for every clip in the project. In case you are using proxies, each proxy will be re-calculated every time Audio sync value is changed.

The results

On the left is the video with original problem, on the right the one with -170ms offset.

Side-by side comparison of 4k video using default parameters (left) and -170ms audio offset
Original -170ms offset

The sample was taken from the video What I've learned about Ultra HD with D. Hugh Redelmeier