mdhiggins / sonarr-sma

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

Support linuxserver/ffmpeg for building #49

Closed lizardfish0 closed 1 year ago

lizardfish0 commented 1 year ago

Hey again!

I've been attempting to get hardware encoding working. Had some success using SMA_USE_REPO, but it looks like vaapi HEVC encoding is broken in the version of ffmpeg that is installed by that repo (see intel-media-driver issue here).

linuxserver.io has abandoned their mono containers and now bases their images on 22.04 Jammy, so I was hopeful that jrottenberg's binaries would be an option. They are only built up to 20.04 though, and I'm getting all sorts of dkpg errors trying to use them within the latest sonarr image.

linuxserver/ffmpeg, however, is built for Jammy and includes vaapi, oneVPL, and nvenc support. My docker-fu isn't good enough to know if it's possible to use this the same way, but it would be a convenient alternative to having to mount a binary into the container and keep that updated manually.

lizardfish0 commented 1 year ago

I've got this working in a fork now, curious if you have an opinion on this, is there any reason not the make the linuxserver/ffmpeg image the default source of ffmpeg? Seems like a nice option since it'll share a maintainer and a base image with the sonarr container.

mdhiggins commented 1 year ago

Did not know linuxserver maintained an FFMPEG container, would be great to make this the main source of FFMPEG, care to turn your fork into a pull request?

mdhiggins commented 1 year ago

Looks like pull request is working for main branch but crashing for develop branch, suspect related to develop branch being alpine based, trying to sort out why currently

mdhiggins commented 1 year ago

Looks like the ttf-dejavu package was throwing an error

lizardfish0 commented 1 year ago

Does the develop tag not use the develop git branch to build? I didn't touch anything there but I'm likely misunderstanding how the git actions work.

lizardfish0 commented 1 year ago

And yeah, I figured there would be clear issues layering a 22.04 ffmpeg on alpine. Might be easiest to have the develop tag pull static binaries by default.

Then your options will be > Want hwaccel? Use latest. > Want sonarr 4.0? Use develop, but you won't get hwaccel. > Something else? Make it work with build + extra_packages.

mdhiggins commented 1 year ago

Merged the change in for now but I suspect this won't last very long since it looks like they intend to migrate to alpine anyway, all the radarr tags are on alpine already and develop has been on alpine for a bit, will probably need to revert back to the old approach once they move off ubuntu unless they update their ffmpeg container as well

lizardfish0 commented 1 year ago

Left a comment on the linuxserver/ffmpeg repo but it doesn't seem like they're interested in supporting this usecase. I may try and throw together an ffmpeg container that's usable here.

lizardfish0 commented 1 year ago

Thank you!

mdhiggins commented 11 months ago

Unfortunately looks like the time has come, they have rebased the main container to alpine so had to revert all this back

VampiricAlien commented 10 months ago

It could be a issue with me but I get the following errors

`vaapi hwaccel is supported by this ffmpeg build and will be used [hwaccels]. ===========WARNING=========== The encoder you have chosen h265vaapi (hevc_vaapi) is not listed as supported in your FFMPEG build, conversion will likely fail, please use a build of FFMPEG that supports hevc_vaapi or choose a different encoder. Other encoders your current FFMPEG build does support for codec hevc: ['hevc'] ===========WARNING=========== ===========WARNING=========== The encoder you have chosen srt (srt) is not listed as supported in your FFMPEG build, conversion will likely fail, please use a build of FFMPEG that supports srt or choose a different encoder. ===========WARNING===========

ffmpeg version 6.1 Copyright (c) 2000-2023 the FFmpeg developers built with gcc 13.2.1 (Alpine 13.2.1_git20231014) 20231014 configuration: --prefix=/usr --disable-librtmp --disable-lzma --disable-static --disable-stripping --enable-avfilter --enable-gpl --enable-ladspa --enable-libaom --enable-libass --enable-libbluray --enable-libdav1d --enable-libdrm --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-libharfbuzz --enable-libmp3lame --enable-libopenmpt --enable-libopus --enable-libplacebo --enable-libpulse --enable-librav1e --enable-librist --enable-libsoxr --enable-libsrt --enable-libssh --enable-libtheora --enable-libv4l2 --enable-libvidstab --enable-libvorbis --enable-libvpx --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxcb --enable-libxml2 --enable-libxvid --enable-libzimg --enable-libzmq --enable-lto=auto --enable-lv2 --enable-openssl --enable-pic --enable-postproc --enable-pthreads --enable-shared --enable-vaapi --enable-vdpau --enable-version3 --enable-vulkan --optflags=-O3 --enable-libjxl --enable-libsvtav1 --enable-libvpl libavutil 58. 29.100 / 58. 29.100 libavcodec 60. 31.102 / 60. 31.102 libavformat 60. 16.100 / 60. 16.100 libavdevice 60. 3.100 / 60. 3.100 libavfilter 9. 12.100 / 9. 12.100 libswscale 7. 5.100 / 7. 5.100 libswresample 5. 0.100 / 5. 0.100 libpostproc 57. 3.100 / 57. 3.100

Everything was working well before the V4 update as I was using my own Ubuntu ffmpeg build but I now use the default settings SMA_UPDATE: true SMA_USE_REPO: true SMA_HWACCEL: true

Reading commends and finding no answers or prebuild binaries, what can I do?

mdhiggins commented 10 months ago

Looks like the ffmpeg binary doesn't include VAAPI. You'll need to find an alternate source that has it and is alpine compatible

mdhiggins commented 10 months ago

Actually I just did a fresh pull and used SMA_USE_REPO and I'm getting a version of ffmpeg that supports hevc_vaapi

ffmpeg version 6.1 Copyright (c) 2000-2023 the FFmpeg developers
  built with gcc 13.2.1 (Alpine 13.2.1_git20231014) 20231014
  configuration: --prefix=/usr --disable-librtmp --disable-lzma --disable-static --disable-stripping --enable-avfilter --enable-gpl --enable-ladspa --enable-libaom --enable-libass --enable-libbluray --enable-libdav1d --enable-libdrm --enable-libfontconfig --enable-libfreetype --enable-libfribidi --enable-libharfbuzz --enable-libmp3lame --enable-libopenmpt --enable-libopus --enable-libplacebo --enable-libpulse --enable-librav1e --enable-librist --enable-libsoxr --enable-libsrt --enable-libssh --enable-libtheora --enable-libv4l2 --enable-libvidstab --enable-libvorbis --enable-libvpx --enable-libwebp --enable-libx264 --enable-libx265 --enable-libxcb --enable-libxml2 --enable-libxvid --enable-libzimg --enable-libzmq --enable-lto=auto --enable-lv2 --enable-openssl --enable-pic --enable-postproc --enable-pthreads --enable-shared --enable-vaapi --enable-vdpau --enable-version3 --enable-vulkan --optflags=-O3 --enable-libjxl --enable-libsvtav1 --enable-libvpl
  libavutil      58. 29.100 / 58. 29.100
  libavcodec     60. 31.102 / 60. 31.102
  libavformat    60. 16.100 / 60. 16.100
  libavdevice    60.  3.100 / 60.  3.100
  libavfilter     9. 12.100 /  9. 12.100
  libswscale      7.  5.100 /  7.  5.100
  libswresample   5.  0.100 /  5.  0.100
  libpostproc    57.  3.100 / 57.  3.100
Encoders:
 V..... = Video
 A..... = Audio
 S..... = Subtitle
 .F.... = Frame-level multithreading
 ..S... = Slice-level multithreading
 ...X.. = Codec is experimental
 ....B. = Supports draw_horiz_band
 .....D = Supports direct rendering method 1
 ------
 V..... h264_qsv             H.264 / AVC / MPEG-4 AVC / MPEG-4 part 10 (Intel Quick Sync Video acceleration) (codec h264)
 V..... h264_v4l2m2m         V4L2 mem2mem H.264 encoder wrapper (codec h264)
 V....D h264_vaapi           H.264/AVC (VAAPI) (codec h264)
...
 V..... hevc_qsv             HEVC (Intel Quick Sync Video acceleration) (codec hevc)
 V..... hevc_v4l2m2m         V4L2 mem2mem HEVC encoder wrapper (codec hevc)
 V....D hevc_vaapi           H.265/HEVC (VAAPI) (codec hevc)
VampiricAlien commented 10 months ago

(Alpine 13.2.1_git20231014) 20231014 is the same that I am using?

Are you able to upload ffmpeg here?

mdhiggins commented 10 months ago

Just run -encoders and confirm if it's there or not first

VampiricAlien commented 10 months ago
V....D hevc_vaapi           H.265/HEVC (VAAPI) (codec hevc)
S..... srt                  SubRip subtitle (codec subrip)
Hardware acceleration methods:
vdpau
vaapi
qsv
drm
vulkan

It's there and supported, something must get missed on setup?

mdhiggins commented 10 months ago

Not sure what would be getting missed if its there? You sure you're pointing to the same ffmpeg binary? That could potentially account for it, verify that the ffmpeg binary you're pointing to in autoProcess.ini is the same as which ffmpeg from what you're running

Cause the warning you were getting

The encoder you have chosen h265vaapi (hevc_vaapi) is not listed as supported in your FFMPEG build, conversion will likely fail, please use a build of FFMPEG that supports hevc_vaapi or choose a different encoder.
Other encoders your current FFMPEG build does support for codec hevc:
['hevc']

Comes from the script basically running -codecs command and check to make sure its there; if you're seeing that error then its not there. The -codecs output for HEVC should show

 DEV.L. hevc                 H.265 / HEVC (High Efficiency Video Coding) (decoders: hevc hevc_qsv hevc_v4l2m2m) (encoders: libx265 hevc_qsv hevc_v4l2m2m hevc_vaapi)

In which case you don't get the error above

mdhiggins commented 10 months ago

Actually, just kidding, this error is just wrong, looks like in newer builds of FFMPEG they removed a trailing space from the way this info is reported and it was breaking the regex string, just pushed an update that will fix the error, though the error itself was just a warning and shouldn't have impacted function

But either way, try and update SMA and see if its better

https://github.com/mdhiggins/sickbeard_mp4_automator/commit/50548e41ef237041eea04557b3f029e5474f7709

VampiricAlien commented 10 months ago

Does the fix apply to docker sma-mod? I think copying the new files is enough.

/usr/bin/ffmpegI used to have the path setup so it never downloaded it and just used local.

Is it possible to have everything local, within the mod? I am thinking that would be too much automation?

VampiricAlien commented 10 months ago
[out#0/matroska @ 0x7f937a19f140] Codec AVOption x265-params (set the x265 configuration using a :-separated list of key=value parameters) has not been used for any stream. The most likely reason is either wrong type (e.g. a video option with no video streams) or that it is a private option of some encoder which was not actually used for any stream.
[out#0/matroska @ 0x7f937a19f140] Codec AVOption preset (Encoding preset) has not been used for any stream. The most likely reason is either wrong type (e.g. a video option with no video streams) or that it is a private option of some encoder which was not actually used for any stream.

The "preset" option in ffmpeg allows you to choose a pre-defined set of encoding parameters that optimize the output quality and compression efficiency. By using a preset, you don't have to manually set all the individual options yourself, as the preset will take care of it for you.

To resolve this warning, you can try adding the "preset" option to your encoding command and specify a suitable preset (such as "medium" or "fast") that matches your desired trade-off between encoding speed and output quality. Here's an example of how you can specify the preset:

Manual.py worked without error but using postSonarr.py (auto) gives that error but i have it set,preset = veryslow

also this is from the logs Traceback (most recent call last): File "/usr/local/sma/resources/mediaprocessor.py", line 2316, in convert for timecode, debug in conv: File "/usr/local/sma/converter/__init__.py", line 329, in convert for timecode, debug in self.ffmpeg.convert(outfile, File "/usr/local/sma/converter/ffmpeg.py", line 798, in convert raise FFMpegConvertError('Exited with code %d' % p.returncode, cmd, converter.ffmpeg.FFMpegConvertError: <exception str() failed>

[vost#0:0/hevc_vaapi @ 0x7f937a1b0180] Error while opening encoder - maybe incorrect parameters such as bit_rate, rate, width or height. Error while filtering: Function not implemented [out#0/matroska @ 0x7f937a19f140] Nothing was wr

This only seems to happen using postSonarr.py?

mdhiggins commented 10 months ago

They all use the same mediaprocessor.py so no reason postSonarr should be different. Share some full logs of one that worked versus one that didn't cause it's probably something else

VampiricAlien commented 10 months ago

I've lost both logs as they were overwritten, I'll have to wait until it goes to auto process it.

I have notice `[90-sma-config] Updating SMA from github master

fatal: detected dubious ownership in repository at '/usr/local/sma'

To add an exception for this directory, call:

git config --global --add safe.directory /usr/local/sma`

That's from the docker update from 2 days ago.

And this time it never downloaded or setup ffmpeg or python, 30 minutes into it there is nothing in the container logs. I always see the set up within the first 10 minutes of a restart. using /usr/local/sma/venv/bin/python3 /usr/local/sma/config/manual.py works without issue.

I saw this in sornarr v4 change log ffprobe has replaced MediaInfo ffprobe would already be installed in the container? I always thought ffmpeg and ffprobe was together?

mdhiggins commented 10 months ago

Manual.py can't function without FFMPEG so not sure how that's possible unless FFMPEG is in fact present as well. They are typically downloaded together though I'm not sure where or how Sonarr installs it but it's possible it's included with the parent container now but if manual.py works then FFMPEG must be present

VampiricAlien commented 10 months ago

That's what I am also thinking. furthermore, the path for ffmpeg is /usr/local/bin/ffmpeg, if I was to set the path in the container to /usr/local/sma/ffmpeg and leave the folder empty then it should trigger a download of ffmpeg and setup with the needed files. /usr/local/bin/ffmpeg -> /usr/local/sma/ffmpeg

I won't be able to try that until much later.

VampiricAlien commented 10 months ago

Setting ffmpeg path to empty folder on host: ERROR: ffmpeg-6.1-r1: failed to rename usr/bin/.apk.6ef7f74d5360a18facabe469aca9c6dadb6c7c153559ac2d to usr/bin/ffmpeg.

Downloaded ffmpeg after path set

ffmpeg version 6.1-static https://johnvansickle.com/ffmpeg/  Copyright (c) 2000-2023 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      58. 29.100 / 58. 29.100
  libavcodec     60. 31.102 / 60. 31.102
  libavformat    60. 16.100 / 60. 16.100
  libavdevice    60.  3.100 / 60.  3.100
  libavfilter     9. 12.100 /  9. 12.100
  libswscale      7.  5.100 /  7.  5.100
  libswresample   4. 12.100 /  4. 12.100
  libpostproc    57.  3.100 / 57.  3.100
Hardware acceleration methods:
vdpau

No VAAPI support in that build, Support should have been added during setup.

Sonarr does have it's own copy of ffmpeg, you can see that in the output

ffmpeg version 6.1-static https://johnvansickle.com/ffmpeg/  Copyright (c) 2000-2023 the FFmpeg developers
  built with gcc 8 (Debian 8.3.0-6)
ffmpeg version 6.1 Copyright (c) 2000-2023 the FFmpeg developers
  built with gcc 13.2.1 (Alpine 13.2.1_git20231014) 20231014
mdhiggins commented 10 months ago

Yeah they probably started including it in the base image since Sonarr is using ffprobe

I tweaked the startup script which would previously not try to download ffmpeg if which ffmpeg returned valid results to now checking if the SMA_FFMPEG_PATH and SMA_PROBE_PATH variables have files that exist instead, this should let you set an alternative path if you didn't want to use the included one

VampiricAlien commented 10 months ago

If those paths are not set, the following error:

mv: cannot overwrite directory '' with non-directory
chmod: cannot access '': No such file or directory
ERROR: unable to select packages:
  py3-virutalenv (no such package):
    required by: world[py3-virutalenv]
ERROR: unable to select packages:
  so:libtinyxml2.so.10 (no such package):
    required by: libmediainfo-23.11-r1[so:libtinyxml2.so.10]
[90-sma-config] Creating python virtual environment and installing requirements
/etc/s6-overlay/s6-rc.d/init-sma-config/run: line 91: python3: command not found
/etc/s6-overlay/s6-rc.d/init-sma-config/run: line 92: /usr/local/sma/venv/bin/python3: No such file or directory
/etc/s6-overlay/s6-rc.d/init-sma-config/run: line 93: /usr/local/sma/venv/bin/pip: No such file or directory

while the container was started, ran this commandapk add --no-cache python3 py3-cryptography py3-pip py3-virtualenv this can not happen each time.

The script isn't adding VAAPI or other HA to ffmpeg like it used too.

SMA_FFMPEG_PATH     /usr/local/sma/config/ff/ffmpeg (is located in folder)
SMA_HWACCEL     true
SMA_PATH    /usr/local/sma
SMA_PROBE_PATH  /usr/local/sma/config/ff/ffprobe (Not found in folder)
SMA_UPDATE  true
SMA_USE_REPO    true
mdhiggins commented 10 months ago

Paths should never be nothing, having it set to nothing will break things down stream

ERROR: unable to select packages:
  py3-virutalenv (no such package):
    required by: world[py3-virutalenv]

I fixed this error today, make sure you're using the latest pull

Script never added 'vaapi', that has to come from your ffmpeg binary and the johnvansickle ffmpeg builds never supported vaapi Using the one from the repo should include vaapi but it looks like the run script wasn't detecting the repo ffmpeg so both were being installed (one at /usr/local/bin and one at /usr/bin) so I've fixed this. Container rebuilding now

VampiricAlien commented 10 months ago

Brings back to the first error

out#0/matroska @ 0x7f49cf8e2940] Codec AVOption preset (Encoding preset) has not been used for any stream. The most likely reason is either wrong type (e.g. a video option with no video streams) or that it is a private option of some encoder which was not actually used for any stream.
Stream mapping:
  Stream #0:0 -> #0:0 (h264 (native) -> hevc (hevc_vaapi))
  Stream #0:1 -> #0:1 (aac (native) -> eac3 (native))
  Stream #1:0 -> #0:2 (subrip (srt) -> subrip (srt))
Press [q] to stop, [?] for help
[hevc_vaapi @ 0x7f49cf840c00] No usable encoding profile found.
[vost#0:0/hevc_vaapi @ 0x7f49cf8febc0] Error while opening encoder - maybe incorrect parameters such as bit_rate, rate, width or height.
Error while filtering: Function not implemented
[Video]
codec = h265vaapi
max-bitrate = 0
bitrate-ratio = 0.7
max-width = 0
preopts = -extra_hw_frames, 10
crf = -0
preset = slow
codec-parameters = 
dynamic-parameters = True
profile = main10
max-level = 5.2
pix-fmt =
prioritize-source-pix-fmt = True
filter = 
force-filter = False
crf-profiles = 

I have changed all video settings but no effect, same error. Using ffmpeg -hwaccel vaapi -hwaccel_output_format vaapi -hwaccel_device /dev/dri/renderD128 -i 'input' -vf 'format=nv12|vaapi,hwupload' -c:v hevc_vaapi 'output.mkv' worked but isn't apart of the script. I am out of ideas, open to suggestions.

mdhiggins commented 10 months ago

Does removing the bitrate ratio setting have any impact?

mdhiggins commented 10 months ago

Also post the full command that's failing if you could

VampiricAlien commented 10 months ago

pix-fmt = p010le, yuv420p10le makes the manual way work again but the error message never said about format? I also in my testing left this blank for the script to fill in the blank, not sure if that ever happened.

The changes you've made in the past few days wasn't a loss, it should help minimise errors later on with all users.

mdhiggins commented 10 months ago

Sweet, well that's progress then. The way the vaapi class currently works does default to nv12 for the inter-hardware upload format if nothing is specified, but having the prioritize-source-pix-fmt = True might have been passing the original pix-fmt of the source material and thus blocking that nv12 default (without the logs its hard to say)

By manually setting a pix-fmt you've probably changed the value that gets put in there (again logs would be helpful to see what finally got passed to ffmpeg)

VampiricAlien commented 10 months ago

I notice when looking at the logs that yuv420p was listed in output and VAAPI on my device doesn't support yuv420p. The reason yuv420p was on the list is so if something was already in HEVC with pix format yuv420p, it would copy and not redo. The script as you know will pick the first option if the option is not listed in settings.

the odd thing was yuv420p was added to settings months ago (at the end of the list) but only started to have the issue this week.

This supports what you said about nv12 not being passed on. I was stuck trying to understand the error message and never looked closely at the output.

With error:

    "format": "mkv",
    "video": {
        "codec": "h265vaapi",
        "map": 0,
        "bitrate": 5064.6533,
        "crf": 0,
        "maxrate": null,
        "bufsize": null,
        "level": 5.2,
        "profile": "main",
        "preset": "slow",
        "pix_fmt": null,
        "field_order": "progressive",
        "width": null,
        "filter": null,
        "params": null,
        "framedata": {
            "pix_fmt": "yuv420p"
        },
        "bsf": null,
        "debug": "video.profile",
        "title": "FHD",
        "device": "sma"

-vf "format=yuv420p|vaapi,hwupload,scale_vaapi=format=yuv420p"

Without error:

    "format": "mkv",
    "video": {
        "codec": "h265vaapi",
        "map": 0,
        "bitrate": 4531.359399999999,
        "crf": 0,
        "maxrate": null,
        "bufsize": null,
        "level": 5.2,
        "profile": "main10",
        "preset": "slow",
        "pix_fmt": "p010le",
        "field_order": "progressive",
        "width": null,
        "filter": null,
        "params": null,
        "framedata": null,
        "bsf": null,
        "debug": "video.profile.pix_fmt",
        "title": "FHD",
        "device": "sma"

-vf "format=p010le|vaapi,hwupload,scale_vaapi=format=p010le"
mdhiggins commented 10 months ago

Sounds like you need to take yuv420p off your pix_fmt list

VampiricAlien commented 10 months ago

Already done, not sure why it started to play up now and not months ago but it's working again. Thank you.

I was looking around and found that nothing comes close (in terms of this script) to what this script you've created can do!

mdhiggins commented 10 months ago

Happy to help, the hardware acceleration stuff is just particularly challenging as the options are quite complex and lots of variability between hardware. Also very hard to test since I often don't have the hardware readily available

VampiricAlien commented 10 months ago

Yes, you're right. Hardware acceleration is indeed challenging plus the availability and support for hardware acceleration features can differ across devices as devices soon become outdated. You always make it work no matter the issue.