mdhiggins / sonarr-sma

Sonarr docker based on linuxserver/sonarr with SMA built in using python3
MIT License
37 stars 18 forks source link

QSV not working on 620slim #34

Closed apeman76 closed 2 years ago

apeman76 commented 2 years ago

Hello,

I am trying to get sonarr-sma (and radarr-sma) working on my Synology 620slim which has great encoder support, but I cant seem to get it to work. I have tried:

-Added renderD128 to the docker export and imported it:

  "devices": [
    {
      "CgroupPermissions": "rwm",
      "PathInContainer": "/dev/dri/renderD128",
      "PathOnHost": "/dev/dri/renderD128"
    }
  ]

The permissions inside docker are: crw-rw---- 1 root videoHXcNH6jS 226, 128 Jan 27 17:21 renderD128

-Running as administrator user My PUID is set to 1026 (of my admin user) and GUID is set to the same group ID (100). This is working great for docker, it can edit all the files it needs. The docker is also run privileged

-Editing the autoProcess.ini file to: (not included lines are default)

[Converter]
hwaccel-decoders = h265qsv,hevc_vaapi,hevcqsv,hevc_qsv,h264qsv,h264vaapi,h264,x264,h265,x265,hevc
hwdevices = qsv:/dev/dri/renderD128

[Video]
codec = h265qsv,hevc_vaapi,hevcqsv,hevc_qsv

[Audio]
codec = ac3

(and added sonarr/radarr api keys)

With every edit (especially the video codec) it seems to work a little more, but the last step that I cant seem to fix is this error I get when a file is imported:

Failed to set value 'qsv=sma:/dev/dri/renderD128' for option 'init_hw_device': Cannot allocate memory

Full log:

2022-01-27 04:00:02 - resources.mediaprocessor - INFO - {
    "source": [
        "/tv/The Simpsons/Season 33/The.Simpsons.S33E03.1080p.WEB.H264-CAKES[rarbg].mkv"
    ],
    "format": "mp4",
    "video": {
        "codec": "h265qsv",
        "map": 0,
        "bitrate": 4162.1856,
        "crf": -1,
        "maxrate": null,
        "bufsize": null,
        "level": 0.0,
        "profile": null,
        "preset": null,
        "pix_fmt": null,
        "field_order": "progressive",
        "width": null,
        "filter": null,
        "params": null,
        "framedata": null,
        "title": "FHD",
        "debug": "video",
        "device": "sma"
    },
    "audio": [
        {
            "map": 1,
            "codec": "copy",
            "channels": 2,
            "bitrate": 256,
            "profile": null,
            "quality": 0,
            "filter": null,
            "samplerate": null,
            "sampleformat": "",
            "language": "eng",
            "disposition": "+default-dub-original-comment-lyrics-karaoke-forced-hearing_impaired-visual_impaired-captions",
            "bsf": null,
            "title": "Stereo",
            "debug": "universal-audio"
        }
    ],
    "subtitle": [
        {
            "map": 2,
            "codec": "mov_text",
            "language": "eng",
            "disposition": "-default-dub-original-comment-lyrics-karaoke-forced-hearing_impaired-visual_impaired-captions",
            "title": null,
            "debug": "subtitle.embed-subs"
        }
    ],
    "attachment": []
}
2022-01-27 04:00:02 - resources.mediaprocessor - INFO - Preopts
2022-01-27 04:00:02 - resources.mediaprocessor - INFO - [
    "-fix_sub_duration",
    "-init_hw_device",
    "qsv=sma:/dev/dri/renderD128"
]
2022-01-27 04:00:02 - resources.mediaprocessor - INFO - Postopts
2022-01-27 04:00:02 - resources.mediaprocessor - INFO - [
    "-threads",
    "0",
    "-metadata:g",
    "encoding_tool=SMA"
]
2022-01-27 04:00:02 - resources.mediaprocessor - INFO - Starting conversion.
2022-01-27 04:00:02 - resources.mediaprocessor - INFO - FFmpeg command:
2022-01-27 04:00:02 - resources.mediaprocessor - INFO - ======================
2022-01-27 04:00:02 - resources.mediaprocessor - INFO - /usr/local/bin/ffmpeg -fix_sub_duration -init_hw_device qsv=sma:/dev/dri/renderD128 -i "/tv/The Simpsons/Season 33/The.Simpsons.S33E03.1080p.WEB.H264-CAKES[rarbg].mkv" -vcodec hevc_qsv -map 0:0 -field_order progressive -vb 4162k -metadata:s:v BPS=4162000 -metadata:s:v BPS-eng=4162000 -metadata:s:v title=FHD -metadata:s:v handler_name=FHD -tag:v hvc1 -c:a:0 copy -map 0:1 -metadata:s:a:0 title=Stereo -metadata:s:a:0 handler_name=Stereo -metadata:s:a:0 language=eng -disposition:a:0 +default-dub-original-comment-lyrics-karaoke-forced-hearing_impaired-visual_impaired-captions -c:s:0 mov_text -map 0:2 -metadata:s:s:0 title= -metadata:s:s:0 handler_name= -metadata:s:s:0 language=eng -disposition:s:0 -default-dub-original-comment-lyrics-karaoke-forced-hearing_impaired-visual_impaired-captions -f mp4 -threads 0 -metadata:g encoding_tool=SMA -y "/tv/The Simpsons/Season 33/The.Simpsons.S33E03.1080p.WEB.H264-CAKES[rarbg].mp4"
2022-01-27 04:00:02 - resources.mediaprocessor - INFO - ======================
2022-01-27 04:00:02 - resources.mediaprocessor - ERROR - Error converting file, FFMPEG error.
Traceback (most recent call last):
  File "/usr/local/sma/resources/mediaprocessor.py", line 1825, in convert
    for timecode, debug in conv:
  File "/usr/local/sma/converter/__init__.py", line 334, in convert
    postopts=postopts):
  File "/usr/local/sma/converter/ffmpeg.py", line 736, in convert
    total_output, pid=p.pid)
converter.ffmpeg.FFMpegConvertError: <unprintable FFMpegConvertError object>
2022-01-27 04:00:02 - resources.mediaprocessor - ERROR - /usr/local/bin/ffmpeg -fix_sub_duration -init_hw_device qsv=sma:/dev/dri/renderD128 -i /tv/The Simpsons/Season 33/The.Simpsons.S33E03.1080p.WEB.H264-CAKES[rarbg].mkv -vcodec hevc_qsv -map 0:0 -field_order progressive -vb 4162k -metadata:s:v BPS=4162000 -metadata:s:v BPS-eng=4162000 -metadata:s:v title=FHD -metadata:s:v handler_name=FHD -tag:v hvc1 -c:a:0 copy -map 0:1 -metadata:s:a:0 title=Stereo -metadata:s:a:0 handler_name=Stereo -metadata:s:a:0 language=eng -disposition:a:0 +default-dub-original-comment-lyrics-karaoke-forced-hearing_impaired-visual_impaired-captions -c:s:0 mov_text -map 0:2 -metadata:s:s:0 title= -metadata:s:s:0 handler_name= -metadata:s:s:0 language=eng -disposition:s:0 -default-dub-original-comment-lyrics-karaoke-forced-hearing_impaired-visual_impaired-captions -f mp4 -threads 0 -metadata:g encoding_tool=SMA -y /tv/The Simpsons/Season 33/The.Simpsons.S33E03.1080p.WEB.H264-CAKES[rarbg].mp4
2022-01-27 04:00:02 - resources.mediaprocessor - ERROR - ffmpeg version 5.0-static https://johnvansickle.com/ffmpeg/  Copyright (c) 2000-2022 the FFmpeg developers
  built with gcc 8 (Debian 8.3.0-6)
  configuration: --enable-gpl --enable-version3 --enable-static --disable-debug --disable-ffplay --disable-indev=sndio --disable-outdev=sndio --cc=gcc --enable-fontconfig --enable-frei0r --enable-gnutls --enable-gmp --enable-libgme --enable-gray --enable-libaom --enable-libfribidi --enable-libass --enable-libvmaf --enable-libfreetype --enable-libmp3lame --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libopenjpeg --enable-librubberband --enable-libsoxr --enable-libspeex --enable-libsrt --enable-libvorbis --enable-libopus --enable-libtheora --enable-libvidstab --enable-libvo-amrwbenc --enable-libvpx --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxml2 --enable-libdav1d --enable-libxvid --enable-libzvbi --enable-libzimg
  libavutil      57. 17.100 / 57. 17.100
  libavcodec     59. 18.100 / 59. 18.100
  libavformat    59. 16.100 / 59. 16.100
  libavdevice    59.  4.100 / 59.  4.100
  libavfilter     8. 24.100 /  8. 24.100
  libswscale      6.  4.100 /  6.  4.100
  libswresample   4.  3.100 /  4.  3.100
  libpostproc    56.  3.100 / 56.  3.100
Device creation failed: -12.
Failed to set value 'qsv=sma:/dev/dri/renderD128' for option 'init_hw_device': Cannot allocate memory
Error parsing global options: Cannot allocate memory

2022-01-27 04:00:02 - SonarrPostProcess - INFO - Processing returned False.
2022-01-27 04:00:02 - SonarrPostProcess - ERROR - Error processing file.
Traceback (most recent call last):
  File "/usr/local/sma/postSonarr.py", line 322, in <module>
    sys.exit(1)
SystemExit: 1

Do you know a fix for this?

mdhiggins commented 2 years ago

Are you using a build of FFMPEG that supports hardware acceleration in these containers? The base FFMPEG binaries included in sonarr-sma/radarr-sma do not and it looks like hwaccel options aren't being applied to your final command which would happen if the build doesn't support it (the script checks against your current FFMPEG build before applying those options)

This error check is done at the decoder level previously but not encoders but I just included an update that checks the encoders you've chosen as well and puts up a warning if not supported

9cef08221d35622a85b44c800fc6e20f9d002e0e

You'll need to use the build tag which will build your docker container locally and also build FFMPEG for you to support whichever hardware acceleration you're trying to achieve (QSV in this case)

apeman76 commented 2 years ago

Hello,

I have tried building a new docker container with the example docker-compose file:

services:
  sonarr:
    container_name: sonarr
    build:
      context: https://github.com/mdhiggins/sonarr-sma.git#build
      args:
        - ffmpeg_tag=4.2-vaapi

This all works and everything is functioning except I get a different error now:

2022-01-28 02:24:37 - SonarrPostProcess - INFO - Sonarr extra script post processing started.
2022-01-28 02:24:42 - SonarrPostProcess - INFO - Sonarr extra script post processing started.
2022-01-28 02:34:04 - SonarrPostProcess - INFO - Sonarr extra script post processing started.
2022-01-28 02:34:04 - resources.readsettings - INFO - /usr/local/sma/venv/bin/python3
2022-01-28 02:34:04 - resources.readsettings - INFO - Loading config file /usr/local/sma/config/autoProcess.ini.
2022-01-28 02:34:04 - resources.mediaprocessor - INFO - File /tv/The Simpsons/Season 33/The.Simpsons.S33E05.WEB.x264-TORRENTGALAXY[TGx].mkv is not valid
2022-01-28 02:34:04 - SonarrPostProcess - INFO - Processing returned False.
2022-01-28 02:34:04 - SonarrPostProcess - ERROR - Error processing file.
Traceback (most recent call last):
  File "/usr/local/sma/postSonarr.py", line 322, in <module>
    sys.exit(1)
SystemExit: 1
2022-01-28 02:39:00 - SonarrPostProcess - INFO - Sonarr extra script post processing started.
2022-01-28 02:39:00 - resources.readsettings - INFO - /usr/local/sma/venv/bin/python3
2022-01-28 02:39:00 - resources.readsettings - INFO - Loading config file /usr/local/sma/config/autoProcess.ini.
2022-01-28 02:39:00 - resources.mediaprocessor - INFO - File /tv/The Simpsons/Season 32/The.Simpsons.S32E22.WEB.x264-PHOENiX[TGx].mkv is not valid
2022-01-28 02:39:00 - SonarrPostProcess - INFO - Processing returned False.
2022-01-28 02:39:00 - SonarrPostProcess - ERROR - Error processing file.
Traceback (most recent call last):
  File "/usr/local/sma/postSonarr.py", line 322, in <module>
    sys.exit(1)
SystemExit: 1

Sorry i am not really that familiar with docker-compose and building containers :/

mdhiggins commented 2 years ago

Looks like your build probably isn't working, if you bash into the container and try to run ffprobe or ffmpeg I'm betting you're going to get a compile error

This is usually a result of using different versions of ubuntu/linux across the FFMPEG build container and the host (sonarr) container.

You can bash into your sonarr container and run

cat /etc/issue which should show you the Ubuntu version, for me its 20.04

In which case adjust your build tag to match

services:
  sonarr:
    container_name: sonarr
    build:
      context: https://github.com/mdhiggins/sonarr-sma.git#build
      args:
        sonarr_tag: develop
        ffmpeg_tag: 4.4-vaapi2004

and while VAAPI and QSV are similar, they aren't exactly the same so you may need to choose your encoders/decoders to support VAAPI instead of quicksync specifically (h265vaapi instead of h265qsv/hevcqsv) though you can check your specific build of FFMPEG to be certain by running ffmpeg -encoders and see if its listed

Edit: Quick check - looks like the develop version of sonarr is on 2004 and the latest tag is still on 1804 so match accordingly and check your own setup, updated example, also arg syntax shouldn't have - on newer versions of docker-compose so I updated that as well

apeman76 commented 2 years ago

Yes, thank you so much!! It works perfectly now after rebuilding the image!

mdhiggins commented 2 years ago

Out of curiosity just in case I encounter this in the future, could you share your final settings for hwaccel related options and which encoder your chose?

Thanks

apeman76 commented 2 years ago

Yes ofcourse:

[Converter]
hwaccel-decoders = hevc_qsv, h264_qsv, hevc_vaapi, h264_vaapi, h264_cuvid, mjpeg_cuvid, mpeg1_cuvid, mpeg2_cuvid, mpeg4_cuvid, vc1_cuvid
hwdevices = qsv:/dev/dri/renderD128
hwaccel-output-format = vaapi:vaapi

[Video]
codec = hevc_vaapi

It is currently running privileged, and with the 'devices' block added to the docker container

This gives me no errors at all 👍

mdhiggins commented 2 years ago

Your setting hwdevices = qsv:/dev/dri/renderD128 is probably not being applied here, should change that to

hwdevices = vaapi:/dev/dri/renderD128

since you switched to VAAPI (though the default device is /dev/dri/renderD128 which is why its probably still working though you might be getting some warnings)

apeman76 commented 2 years ago

Thank you, I have edited that and it still works! I have 1 issue left that I cant seem to fix, I have really bad quality output but havent set any limitations or changes to the source's quality. Any idea why this could be?

[Video]
codec = hevc_vaapi
max-bitrate = 0
bitrate-ratio = 
crf = -1
crf-profiles = 
preset = 
codec-parameters = 
dynamic-parameters = True
max-width = 0
profile = 
max-level = 0.0
pix-fmt = 
filter = 
force-filter = False

This is from a 1080p 1.02GB h264 file to a 170MB h265 file https://i.postimg.cc/kMfYxQHX/Screenshot-2022-02-01-181200.png

mdhiggins commented 2 years ago

I mean you basically have no quality settings chosen here and FFMPEG in general defaults to pretty poor settings Try setting your crf (which for VAAPI gets translated to the -qp parameter) to something like 22 to 25 though these are more basic FFMPEG questions you should really be researching on your own

You can also mess around with the crf-profiles / profile / and bitrate-ratio options if you want to fine tune things, see the wiki for how these work

apeman76 commented 2 years ago

I will try out some settings and see the result. Thanks for all the help!!