matanui159 / ReplaySorcery

An open-source, instant-replay solution for Linux
GNU General Public License v3.0
854 stars 27 forks source link

Possible to see what the ffmpeg command line equivalent would be? #98

Closed parkerlreed closed 3 years ago

parkerlreed commented 3 years ago

Describe the solution you'd like

A way to see what the ffmpeg command line would be for any particular config setup (for doing a continuous video stream/using in other applications)

I'm wanting to use KMSGRAB elsewhere but I haven't been able to get it working standalone.

Describe alternatives you've considered

Tried using the filter line from the replay-sorcery output with no luck. Replay Sorcery itself works great,

$ sudo ffmpeg -device /dev/dri/card1 -f kmsgrab -i - -vf 'hwmap=derive_device=vaapi,crop=1920:1080:0:0,scale_vaapi=1920:1080:nv12' -c:v libx264 out.mp4
[kmsgrab @ 0x56210ced4f00] Using plane 48 to locate framebuffers.
[kmsgrab @ 0x56210ced4f00] Template framebuffer is 84: 1920x1080 32bpp 24b depth.
Input #0, kmsgrab, from 'pipe:':
  Duration: N/A, start: 1611783814.911290, bitrate: N/A
    Stream #0:0: Video: wrapped_avframe, drm_prime, 1920x1080, 29.92 tbr, 1000k tbn, 1000k tbc
Stream mapping:
  Stream #0:0 -> #0:0 (wrapped_avframe (native) -> h264 (libx264))
Impossible to convert between the formats supported by the filter 'Parsed_scale_vaapi_2' and the filter 'auto_scaler_0'
Error reinitializing filters!
Failed to inject frame into filter network: Function not implemented
Error while processing the decoded data for stream #0:0
Conversion failed!
matanui159 commented 3 years ago

You need to use the h264_vaapi encoder. You can see all the options used inside vaapienc.c. Maybe I might add the options used to the logs as well.

parkerlreed commented 3 years ago

Ok I got it saving a video but it's nowhere near 60 fps

ffmpeg -y \
        -device /dev/dri/card1 -f kmsgrab -framerate 60 -i - \
        -vaapi_device /dev/dri/renderD129 -vf 'hwmap=derive_device=vaapi,crop=1920:1080:0:0,scale_vaapi=1920:1080:format=nv12' \
        -c:v h264_vaapi -qp:v 18 \
        out.mp4

The file metadata says it is but playback is about 10 (tried a few different players)

matanui159 commented 3 years ago

You need the framerate option before -i kmsgrab (FFmpeg is very specific about the order, you need input options before the input and output options before the output).

parkerlreed commented 3 years ago

Isn't it before -i already?

-device /dev/dri/card1 -f kmsgrab -framerate 60 -i -

or do you mean before the -f?

parkerlreed commented 3 years ago

Same thing

sudo ffmpeg -y -device /dev/dri/card1 -framerate 60 -f kmsgrab -i - -vaapi_device /dev/dri/renderD129 -vf 'hwmap=derive_device=vaapi,crop=1920:1080:0:0,scale_vaapi=1920:1080:format=nv12'  -c:v h264_vaapi -qp:v 18 out.mp4

Example https://streamable.com/lmpk19

matanui159 commented 3 years ago

it's possible that your system might not be able to keep up with 60 FPS but that's the only theory I have left.

parkerlreed commented 3 years ago

The weird part is 60 works great in replay-sorcery. That's what led me down the rabbit hole of recreating it.

Reading config file '/usr/etc/replay-sorcery.conf'...
Setting 'logLevel' to 'info'...
Setting 'traceLevel' to 'error'...
Setting 'recordSeconds' to '60'...
Setting 'videoInput' to 'hwaccel'...
Setting 'videoDevice' to 'card1:48'...
Setting 'videoX' to '0'...
Setting 'videoY' to '0'...
Setting 'videoWidth' to 'auto'...
Setting 'videoHeight' to 'auto'...
Setting 'videoFramerate' to '60'...
Setting 'videoEncoder' to 'vaapi'...
Setting 'videoProfile' to 'baseline'...
Setting 'videoPreset' to 'fast'...
Setting 'videoQuality' to '28'...
Setting 'videoBitrate' to 'auto'...
Setting 'videoGOP' to '60'...
Setting 'scaleWidth' to 'auto'...
Setting 'scaleHeight' to 'auto'...
Setting 'audioInput' to 'auto'...
Setting 'audioDevice' to 'auto'...
Setting 'audioSamplerate' to '48000'...
Setting 'audioEncoder' to 'auto'...
Setting 'audioProfile' to 'low'...
Setting 'audioBitrate' to 'auto'...
Setting 'controller' to 'auto'...
Setting 'keyName' to 'r'...
Setting 'keyMods' to 'ctrl+super'...
Setting 'outputFile' to '~/Videos/ReplaySorcery_%F_%H-%M-%S.mp4'...
Setting 'outputCommand' to 'notify-send ReplaySorcery "Saved replay as %s"'...

Example from replay

https://streamable.com/llpvco

matanui159 commented 3 years ago

If you want the exact same settings you can try -profile:v baseline -coder cavlc -rc_mode CQP -q 28 -g 60

parkerlreed commented 3 years ago

That goes after the vf right? Doesn't like it

$ sudo ffmpeg -y -device /dev/dri/card1 -framerate 60 -f kmsgrab -i - -vaapi_device /dev/dri/renderD129 -vf 'hwmap=derive_device=vaapi,crop=1920:1080:0:0,scale_vaapi=1920:1080:nv12' -profile:v baseline -coder cavlc -rc_mode CQP -q 28 -g 60 -c:v h264_vaapi output.mp4
ffmpeg version n4.3.1 Copyright (c) 2000-2020 the FFmpeg developers
  built with gcc 10.2.0 (GCC)
  configuration: --prefix=/usr --disable-debug --disable-static --disable-stripping --enable-amf --enable-avisynth --enable-cuda-llvm --enable-lto --enable-fontconfig --enable-gmp --enable-gpl --enable-ladspa --enable-libaom --enable-libass --enable-libbluray --enable-libdav1d --enable-libdrm --enable-libfreetype --enable-libfribidi --enable-libgsm --enable-libiec61883 --enable-libjack --enable-libmfx --enable-libmodplug --enable-libmp3lame --enable-libopencore_amrnb --enable-libopencore_amrwb --enable-libopenjpeg --enable-libopus --enable-libpulse --enable-librav1e --enable-libsoxr --enable-libspeex --enable-libsrt --enable-libssh --enable-libtheora --enable-libv4l2 --enable-libvidstab --enable-libvmaf --enable-libvorbis --enable-libvpx --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxcb --enable-libxml2 --enable-libxvid --enable-nvdec --enable-nvenc --enable-omx --enable-openssl --enable-shared --enable-version3 --enable-libfdk_aac --enable-nonfree
  libavutil      56. 51.100 / 56. 51.100
  libavcodec     58. 91.100 / 58. 91.100
  libavformat    58. 45.100 / 58. 45.100
  libavdevice    58. 10.100 / 58. 10.100
  libavfilter     7. 85.100 /  7. 85.100
  libswscale      5.  7.100 /  5.  7.100
  libswresample   3.  7.100 /  3.  7.100
  libpostproc    55.  7.100 / 55.  7.100
[kmsgrab @ 0x55bc4b211a80] Using plane 48 to locate framebuffers.
[kmsgrab @ 0x55bc4b211a80] Template framebuffer is 97: 1920x1080 32bpp 24b depth.
Input #0, kmsgrab, from 'pipe:':
  Duration: N/A, start: 1611786695.551389, bitrate: N/A
    Stream #0:0: Video: wrapped_avframe, drm_prime, 1920x1080, 59.94 tbr, 1000k tbn, 1000k tbc
Stream mapping:
  Stream #0:0 -> #0:0 (wrapped_avframe (native) -> h264 (h264_vaapi))
[h264_vaapi @ 0x55bc4b215a40] [Eval @ 0x7ffc14d91d00] Undefined constant or missing '(' in 'baseline'
[h264_vaapi @ 0x55bc4b215a40] Unable to parse option value "baseline"
[h264_vaapi @ 0x55bc4b215a40] Error setting option profile to value baseline.
Segmentation fault
parkerlreed commented 3 years ago

It "works" with profile main but resulting video looks terrible and has the same framerate issue

matanui159 commented 3 years ago

I think it uses constrained_baseline.

I'm probably going to close this issue for now since it isn't really related to ReplaySorcery but here are some resources you can follow up on:

parkerlreed commented 3 years ago

I think I figured out what it was. The stutter is only in the beginning of the capture. Since replay-sorcery is on a continuous buffer, it never manifests itself. If I let

sudo ffmpeg -y -device /dev/dri/card0 -framerate 60 -f kmsgrab -i - -vaapi_device /dev/dri/renderD128 -vf 'hwmap=derive_device=vaapi,crop=1920:1080:0:0,scale_vaapi=1920:1080:nv12' -c:v h264_vaapi output.mp4

run for a bit longer it smooths out as expected.

Maybe a quirk with AMD, not sure.