Page 1 of 1

ffmpeg + h264 + Resolve

PostPosted: Mon Jul 23, 2018 3:13 pm
by Vortico
Hello. I cannot find a valid h264 format that Resolve (15 Beta, Windows) is able to load. I have tried invoking ffmpeg with
Code: Select all
ffmpeg -i input.mkv $FLAGS output.mp4

where FLAGS is among
Code: Select all
-c:v libx264 -crf 18 -pix_fmt yuv420p
-framerate 29.97 -c:v libx264 -crf 18 -pix_fmt yuv420p
-c:v libx264 -crf 18 -pix_fmt yuv420p -vf scale=1920:1080
-c:v libx264 -crf 18 -pix_fmt yuv422p
-c:v libx264 -crf 18 -pix_fmt yuv444p
-c:v libx264 -crf 18 -pix_fmt yuvj444p
-c:v libx264 -crf 0-pix_fmt yuv420p
-c:v libx264 -crf 26 -pix_fmt yuv420p
-c:v libx264 -crf 18 -pix_fmt yuv420p
-c:v libx264 -crf 18 -pix_fmt yuv420p -profile:v baseline -level 3.0
-c:v libx264 -crf 18 -pix_fmt yuv420p -an

Each of these files yields a Media Missing error when loading in Resolve.

I have successfully used ffmpeg's prores and dnxhd encoders, but for some applications like screen recording or live feeds, these files are several GB, while an h264 file is tens of MB.
Has someone successfully encoded an h264 file that has loaded in Resolve before? What format or ffmpeg flags did you use? The only information I could find about Resolve's codec support is the DaVinci Resolve Supported Formats and Codecs document, which simply says "H.264 YES".

Re: ffmpeg + h264 + Resolve

PostPosted: Mon Jul 23, 2018 5:53 pm
by Norman Black
I have not had any problem generating AVC output from ffmpeg that Resolve can load. All in an MP4 container.

full range fast decode edit settings
c:v libx264 -preset:v medium -profile:v high -crf 20 -bf 0 -g 8 -tune fastdecode -pix_fmt yuvj420p

studio range Fast decode edit settings
-c:v libx264 -preset:v medium -profile:v high -crf 20 -bf 0 -g 8 -tune fastdecode -pix_fmt yuv420p

Final playback type settings
-c:v libx264 -preset slow -profile:v high -crf 23 -pix_fmt yuv420p

Sample script.
Code: Select all
@echo off

call "%~dp0set_path.cmd"
set TempFile=%TEMP%\ffprobe.txt

:top
title transcode to AVC FD 420F - %1
call :GetAudio %1
if errorlevel 1 goto error
ffmpeg.exe -i %1 -c:v libx264 -preset:v medium -profile:v high -crf 20 -bf 0 -g 8 -tune fastdecode -pix_fmt yuvj420p -colorspace bt709 -color_primaries bt709 -color_trc bt709 %AUDIO% -chunk_size 64K "%~dpn1_fd.mp4"
if errorlevel 1 goto error
REM pause

shift
if NOT %1$==$ goto top
del %TempFile%
goto :EOF

:error
echo !!! error encoding !!!
pause
del %TempFile%
goto :EOF

:GetAudio
ffprobe.exe -v error -select_streams a:0 -show_entries stream=codec_long_name -of default=noprint_wrappers=1 %1 > %TempFile%
if errorlevel 1 goto ProbeError
find /C "PCM" %TempFile%
IF %ERRORLEVEL% EQU 0 (
  set AUDIO=-c:a aac -b:a 192k
) ELSE (
  set AUDIO=-c:a copy
)
exit /b 0
:ProbeError
echo !!! probe error !!!
exit /b 1

REM -ssim 1 -refs 2 -bf 0 -tune fastdecode
REM -profile:v high422 -pix_fmt yuv422p -pix_fmt yuvj422p
REM (aac 192k vbr) -c:a aac -q:a 1.7
REM (aac 192k)     -c:a aac -b:a 192k

Re: ffmpeg + h264 + Resolve

PostPosted: Mon Jul 23, 2018 7:22 pm
by sacherjj
I've been using ffmpeg to go to DNxHD if I'm coming into Resolve. This takes space, but is faster to process and less artifacts.

Re: ffmpeg + h264 + Resolve

PostPosted: Mon Jul 23, 2018 7:57 pm
by Cary Knoop
Vortico wrote:Hello. I cannot find a valid h264 format that Resolve (15 Beta, Windows) is able to load. I have tried invoking ffmpeg with
Code: Select all
ffmpeg -i input.mkv $FLAGS output.mp4

where FLAGS is among
Code: Select all
-c:v libx264 -crf 18 -pix_fmt yuv420p
-framerate 29.97 -c:v libx264 -crf 18 -pix_fmt yuv420p
-c:v libx264 -crf 18 -pix_fmt yuv420p -vf scale=1920:1080
-c:v libx264 -crf 18 -pix_fmt yuv422p
-c:v libx264 -crf 18 -pix_fmt yuv444p
-c:v libx264 -crf 18 -pix_fmt yuvj444p
-c:v libx264 -crf 0-pix_fmt yuv420p
-c:v libx264 -crf 26 -pix_fmt yuv420p
-c:v libx264 -crf 18 -pix_fmt yuv420p
-c:v libx264 -crf 18 -pix_fmt yuv420p -profile:v baseline -level 3.0
-c:v libx264 -crf 18 -pix_fmt yuv420p -an

Each of these files yields a Media Missing error when loading in Resolve.

I have successfully used ffmpeg's prores and dnxhd encoders, but for some applications like screen recording or live feeds, these files are several GB, while an h264 file is tens of MB.
Has someone successfully encoded an h264 file that has loaded in Resolve before? What format or ffmpeg flags did you use? The only information I could find about Resolve's codec support is the DaVinci Resolve Supported Formats and Codecs document, which simply says "H.264 YES".

Odd, most of those formats should work.

Free version might not support yuv422p. The j-version should work as well (but note that Resolve probably expects them to be in Rec.601).

Did you verify the videos are fixed frame rate videos? vfr videos are problematic in Resolve.

Re: ffmpeg + h264 + Resolve

PostPosted: Mon Jul 23, 2018 9:12 pm
by Vortico
Thank you all for the help, especially Norman for his ffmpeg flags. I tried your suggestion on files recorded with three different screen recorders on Windows/Mac, but none of them load in Resolve still.

Here's one of the original videos (https://andrewbelt.name/files/1%20-%20D ... sphere.mov 34MB) with the following format, as reported by ffmpeg.
Code: Select all
    Stream #0:0(und): Video: h264 (libx264) (avc1 / 0x31637661), yuvj420p(pc), 1664x954 [SAR 1:1 DAR 832:477], q=-1--1, 60 fps, 15360 tbn, 60 tbc (default)

It fails to load in Resolve, so I tried your first suggestion
Code: Select all
   ffmpeg -y -i "$f" -c:v libx264 -preset:v medium -profile:v high -crf 20 -bf 0 -g 8 -tune fastdecode -pix_fmt yuvj420p "$f.converted.mp4"

which yields this video (https://andrewbelt.name/files/1%20-%20D ... verted.mp4 130MB) with the following format.
Code: Select all
    Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuvj420p(pc), 1664x954 [SAR 1:1 DAR 832:477], 20875 kb/s, 60 fps, 60 tbr, 15360 tbn, 120 tbc (default)

This also fails to load in Resolve.

Both open fine in VLC player and MPV. I believe they are fixed frame rate, but perhaps I'm not reading the ffmpeg output correctly. I've actually never used variable framerate formats before, so I don't know what to expect. Perhaps someone here can notice an issue at a glance?

Perhaps there is some quality about the original recording that is nonstandard and making its way into the re-encoded versions? If these are "bad" h264 files, could someone take a look at why?

Re: ffmpeg + h264 + Resolve

PostPosted: Mon Jul 23, 2018 9:36 pm
by Vortico
Just figured it out. The issue has nothing to do with video, although I learned a ton about ffmpeg's h264 encoder while trying to solve this. The issue is that all of my screen recordings have 7.1 surround sound audio for some unknown reason, despite not owning surround audio hardware. Regardless, an easy fix is to chop off all but the first two channels with
Code: Select all
-ac 2

since Resolve doesn't handle 7.1 surround AAC. This is strange because one of my tests in my first post include -an which removes audio, but for some reason it didn't load in Resolve. Perhaps -an inserts silence instead of removing the audio track? I don't know.

Hopefully this helps someone.

Re: ffmpeg + h264 + Resolve

PostPosted: Mon Jul 23, 2018 10:10 pm
by Norman Black
Your video is variable frame rate. MediaInfo text report.
Code: Select all
Video
ID                                       : 1
Format                                   : AVC
Format/Info                              : Advanced Video Codec
Format profile                           : Main@L4
Format settings                          : CABAC / 2 Ref Frames
Format settings, CABAC                   : Yes
Format settings, RefFrames               : 2 frames
Format settings, GOP                     : M=2, N=59
Codec ID                                 : avc1
Codec ID/Info                            : Advanced Video Coding
Duration                                 : 49 s 320 ms
Source duration                          : 49 s 433 ms
Bit rate                                 : 6 741 kb/s
Width                                    : 1 664 pixels
Height                                   : 954 pixels
Display aspect ratio                     : 16:9
Frame rate mode                          : Variable
Frame rate                               : 59.818 FPS
Minimum frame rate                       : 30.000 FPS
Maximum frame rate                       : 60.000 FPS
Color space                              : YUV
Chroma subsampling                       : 4:2:0
Bit depth                                : 8 bits
Scan type                                : Progressive
Bits/(Pixel*Frame)                       : 0.071
Stream size                              : 39.6 MiB (96%)
Source stream size                       : 39.6 MiB (96%)
Title                                    : Core Media Video
Encoded date                             : UTC 2018-07-22 01:34:03
Tagged date                              : UTC 2018-07-22 01:34:03
Color range                              : Limited
Color primaries                          : BT.709
Transfer characteristics                 : BT.709
Matrix coefficients                      : BT.709


Most(?) of the time with ffmpeg you will get a constant frame rate file from a VFR source video generated from screen capture software. It might be the frame rate you want. Then again, it might not. For example. Should the constant frame rate be 59.94 or 60.0. ffmpeg could think if should be one or the other, or just keep it variable.

So use MediaInfo to check your source and result media and if the result is not constant frame rate, or the wrong constant rate, then use an ffmpeg command to resample to a proper constant frame rate. If you just know you always want X then just let the command always set for X. It does something or not depending on the source.

For example:
-i %1 -filter:v fps=30000/1001 -c:v libx264

where fps is what you want. For NTSC I like to use the "proper" numerator/denominator values.
29.97 => 30000/1001, 59.94 => 60000/1001, 23.976 => 24000/1001
Others are obviously easier to specify.

filter fps and the -r command rate option may give the same result. I don't know. ffmpeg has duplicate ways to do some things.