mdhiggins / sickbeard_mp4_automator

Automatically convert video files to a standardized format with metadata tagging to create a beautiful and uniform media library
MIT License
1.53k stars 200 forks source link

Issue with max-width and NVIDIA hardware acceleration. #1660

Closed henryjb99 closed 1 year ago

henryjb99 commented 1 year ago

Jump to https://github.com/mdhiggins/sickbeard_mp4_automator/issues/1660#issuecomment-1586292422 for more information on the issue.

Errors when using manual.py, but works if calling ffmpeg directly

I recently installed an NVidia 1070TI and installed the Red Hat package for FFMPEG including the cuda hardware decoder. I am getting errors when using SMA, but it works fine if run as an ffmpeg command without the extra switches.

Running manual.py manual.txt

autoProcess.ini settings [Converter] ffmpeg = ffmpeg ffprobe = ffprobe threads = 0 hwaccels = nvenc, cuvid hwaccel-decoders = h264_cuda, hevc_cuda, h264_cuvid, mjpeg_cuvid, mpeg1_cuvid, mpeg2_cuvid, mpeg4_cuvid, vc1_cuvid, hevc_qsv, h264_qsv, hevc_vaapi, h264_vaapi hwdevices = cuda:0 hwaccel-output-format = cuda:0 output-directory = /SWAP output-format = mp4 output-extension = mp4 temp-extension = minimum-size = 0 ignored-extensions = nfo, dsstore copy-to = move-to = delete-original = True sort-streams = True process-same-extensions = False force-convert = True post-process = False wait-post-process = False detailed-progress = False preopts = postopts = bypass-if-copying-all = False opts-separator = , regex-directory-replace = [^\w-. ] output-directory-space-ratio = 0.0

[Permissions] chmod = 0777 uid = 1000 gid = 1000

[Metadata] relocate-moov = True full-path-guess = True tag = True tag-language = eng download-artwork = thumbnail sanitize-disposition = strip-metadata = False keep-titles = False

[Video] codec = hevc, x265, h265 max-bitrate = 10000 crf = -1 crf-profiles = preset = codec-parameters = dynamic-parameters = False max-width = 2160 profile = max-level = 0.0 pix-fmt = filter = force-filter = False bitrate-ratio = prioritize-source-pix-fmt = True

[HDR] pix-fmt = space = bt2020nc transfer = smpte2084 primaries = bt2020 preset = codec-parameters = filter = force-filter = False codec = profile =

[Audio] codec = aac languages = eng default-language = eng first-stream-of-language = False allow-language-relax = True channel-bitrate = 192 max-bitrate = 0 max-channels = 0 prefer-more-channels = True filter = force-filter = False sample-rates = copy-original = False aac-adtstoasc = False ignore-truehd = mp4, m4v ignored-dispositions = unique-dispositions = False include-original-language = True variable-bitrate = 0 profile = sample-format = force-default = False stream-codec-combinations = default-more-channels = True

[Universal Audio] codec = aac channel-bitrate = 196 first-stream-only = False filter = force-filter = False variable-bitrate = 0 profile = move-after = False

[Subtitle] codec = mov_text codec-image-based = languages = default-language = first-stream-of-language = False encoding = burn-subtitles = False burn-dispositions = download-subs = False download-hearing-impaired-subs = False download-providers = embed-subs = True embed-image-subs = False embed-only-internal-subs = False filename-dispositions = forced ignore-embedded-subs = False ignored-dispositions = unique-dispositions = False attachment-codec = include-original-language = False force-default = False remove-bitstream-subs = False

[Sonarr] host = XXXXXXXXXXXXXXXXX port = 8990 apikey = XXXXXXXXXXXXXXXX ssl = False webroot = force-rename = False rescan = True block-reprocess = False in-progress-check = True

[Radarr] host = XXXXXXXXXXXXXX port = 7878 apikey = XXXXXXXXXXXXXXXXXX ssl = False webroot = force-rename = False rescan = True block-reprocess = False in-progress-check = True

[Sickbeard] host =XXXXXXXXXXXXXXX port = 8081 ssl = False apikey = webroot = username = password =

[Sickrage] host = XXXXXXXXXXXXX port = 8081 ssl = False apikey = webroot = username = password =

[CouchPotato] host = XXXXXXXXXXXXXXX port = 5050 username = password = apikey = delay = 65 method = renamer delete-failed = False ssl = False webroot =

[SABNZBD] convert = True sickbeard-category = sickbeard sickrage-category = sickrage couchpotato-category = couchpotato sonarr-category = sonarr radarr-category = radarr bypass-category = bypass output-directory = path-mapping =

[Deluge] couchpotato-label = couchpotato sickbeard-label = sickbeard sickrage-label = sickrage sonarr-label = sonarr radarr-label = radarr bypass-label = bypass convert = True host = XXXXXXXXXXXX port = 58846 username = password = output-directory = remove = False path-mapping =

[qBittorrent] couchpotato-label = couchpotato sickbeard-label = sickbeard sickrage-label = sickrage sonarr-label = sonarr radarr-label = radarr bypass-label = bypass convert = False action-before = action-after = host = XXXXXXXXXX port = 8080 ssl = False username = XXXXXXXXXXX password = XXXXXXXXXXX output-directory = path-mapping =

[uTorrent] couchpotato-label = couchpotato sickbeard-label = sickbeard sickrage-label = sickrage sonarr-label = sonarr radarr-label = radarr bypass-label = bypass convert = True webui = False action-before = action-after = host = XXXXXXXXXXX ssl = False port = 8080 username = password = output-directory = path-mapping =

[Plex] host = XXXXXXXXXXXX port = XXXXXXXX refresh = False token = XXXXXXXXXXX username = password = servername = ssl = True ignore-certs = False path-mapping =

[Audio.Sorting] sorting = language, channels.d, map, d.comment default-sorting = channels.d, map, d.comment codecs =

[Audio.ChannelFilters]

[Subtitle.Sorting] sorting = language, d.comment, d.default.d, d.forced.d codecs = burn-sorting = language, d.comment, d.default.d, d.forced.d

[Subtitle.CleanIt] enabled = False config-path = tags =

[Subtitle.FFSubsync] enabled = False

[Subtitle.Subliminal] download-subs = False download-forced-subs = False include-hearing-impaired-subs = False providers =

[Subtitle.Subliminal.Auth]

Log files See the output from the manual file above.

FFMpeg headers fffmpeg -hwaccels ffmpeg version N-110987-g4ef5e7d472 Copyright (c) 2000-2023 the FFmpeg developers built with gcc 11 (GCC) configuration: --enable-nonfree --enable-cuda-nvcc --enable-libnpp --extra-cflags=-I/usr/local/cuda/include --extra-ldflags=-L/usr/local/cuda/lib64 --disable-static --enable-shared libavutil 58. 12.100 / 58. 12.100 libavcodec 60. 16.100 / 60. 16.100 libavformat 60. 5.100 / 60. 5.100 libavdevice 60. 2.100 / 60. 2.100 libavfilter 9. 8.101 / 9. 8.101 libswscale 7. 3.100 / 7. 3.100 libswresample 4. 11.100 / 4. 11.100 Hardware acceleration methods: cuda

Use -h to get full help or, even better, run 'man ffmpeg'

System Information

Expected behavior When running anything through Manual.py it all files fail after moving my settings to use my NVidia 1070TI graphics card.

Things work fine when running "ffmpeg -hwaccel cuda -i Test_Video_1.mkv Test_Video_1.mp4

Additional context Output of running manual.py is in above file

mdhiggins commented 1 year ago

The simple command and command being generated are very different. You're not specifically setting a codec on your simple command so it's probably just choosing a different codec allowing it to work. Does your FFMPEG build have libx265

mdhiggins commented 1 year ago

Also if your intent is to use your video card you also need to specific an nvenc hardware encoder, libx265 is a software encoder

h265_nvenc would be the equivalent

The 1070ti supports h265 4:2:0 but not h265 4:4:4

henryjb99 commented 1 year ago

hmmm, it appears the install of ffmpeg has hevc, but not specifically libx265. I have changed my codec to hvec_nvenc and manual.py seems to be working on this file, but it is using CPU instead of the graphics card. Have to try out a few more files to see if this fixes all of them

    PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND
  40431 root      20   0 9872.5m 293444 170148 R 203.0   1.2   6:48.10 /usr/local/bin/ffmpeg -fix_sub_duration -i /shared/Converted/new_folder/Star.Trek.Lower.Decks.S01E01.1080p.BluRay.REMUX.AVC.DTS-HD.MA.5+

ffmpeg -codecs | grep 265
ffmpeg version N-110987-g4ef5e7d472 Copyright (c) 2000-2023 the FFmpeg developers
  built with gcc 11 (GCC)
  configuration: --enable-nonfree --enable-cuda-nvcc --enable-libnpp --extra-cflags=-I/usr/local/cuda/include --extra-ldflags=-L/usr/local/cuda/lib64 --disable-static --enable-shared
  libavutil      58. 12.100 / 58. 12.100
  libavcodec     60. 16.100 / 60. 16.100
  libavformat    60.  5.100 / 60.  5.100
  libavdevice    60.  2.100 / 60.  2.100
  libavfilter     9.  8.101 /  9.  8.101
  libswscale      7.  3.100 /  7.  3.100
  libswresample   4. 11.100 /  4. 11.100
 DEV.L. hevc                 H.265 / HEVC (High Efficiency Video Coding) (decoders: hevc hevc_v4l2m2m hevc_cuvid ) (encoders: hevc_nvenc hevc_v4l2m2m )
henryjb99 commented 1 year ago

Using this command, I get the conversion using GPU instead of CPU

ffmpeg -y -vsync 0 -hwaccel cuda -hwaccel_output_format cuda -i input.mp4 -c:a copy -c:v h264_nvenc -b:v 5M output.mp4

henryjb99 commented 1 year ago

@mdhiggins

Ok, so I can get this to work if I change the max-width to 0 instead of 2160.

How would I get the sma to work properly if I want to convert videos to a specific resolution? Is this a problem with the ffmpeg install that I have? autoProcess.ini.txt

yum list installed | grep ffmpeg ffmpeg.x86_64 5.1.3-1.el9 @rpmfusion-free-updates ffmpeg-libs.x86_64 5.1.3-1.el9 @rpmfusion-free-updates

Works (Max width at 0): /usr/local/bin/ffmpeg -fix_sub_duration -init_hw_device cuda=sma:0 -hwaccel_device sma -hwaccel cuda -hwaccel_output_format cuda -vcodec hevc_cuvid -i /shared/Converted/new_folder/Test_Video_1.mkv -vcodec hevc_nvenc -map 0:0 -vb 10000k -metadata:s:v BPS=10000000 -metadata:s:v BPS-eng=10000000 -metadata:s:v "title=4K HDR" -metadata:s:v "handler_name=4K HDR" -tag:v hvc1 -c:a:0 aac -map 0:1 -ac:a:0 6 -b:a:0 1176k -metadata:s:a:0 BPS=1176000 -metadata:s:a:0 BPS-eng=1176000 -metadata:s:a:0 "title=5.1 Channel" -metadata:s:a:0 "handler_name=5.1 Channel" -metadata:s:a:0 language=eng -disposition:a:0 +default-dub-original-comment-lyrics-karaoke-forced-hearing_impaired-visual_impaired-captions -c:a:1 aac -map 0:1 -ac:a:1 2 -b:a:1 256k -metadata:s:a:1 BPS=256000 -metadata:s:a:1 BPS-eng=256000 -filter:a:1 "pan=stereo|FL=0.5*FC+0.707*FL+0.707*BL+0.5*LFE|FR=0.5*FC+0.707*FR+0.707*BR+0.5*LFE" -metadata:s:a:1 title=Stereo -metadata:s:a:1 handler_name=Stereo -metadata:s:a:1 language=eng -disposition:a:1 -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 -c:s:1 mov_text -map 0:3 -metadata:s:s:1 "title=Hearing Impaired" -metadata:s:s:1 "handler_name=Hearing Impaired" -metadata:s:s:1 language=eng -disposition:s:1 -default-dub-original-comment-lyrics-karaoke-forced+hearing_impaired-visual_impaired-captions -c:s:2 mov_text -map 0:4 -metadata:s:s:2 title= -metadata:s:s:2 handler_name= -metadata:s:s:2 language=fra -disposition:s:2 -default-dub-original-comment-lyrics-karaoke-forced-hearing_impaired-visual_impaired-captions -f mp4 -threads 0 -metadata:g encoding_tool=SMA -strict -2 -y /SWAP/Test_Video_1.mp4

Doesn't Work (Max width at 2160) /usr/local/bin/ffmpeg -fix_sub_duration -init_hw_device cuda=sma:0 -hwaccel_device sma -hwaccel cuda -hwaccel_output_format cuda -vcodec hevc_cuvid -i /shared/Converted/new_folder/Test_Video_1.mkv -vcodec hevc_nvenc -map 0:0 -vb 10000k -metadata:s:v BPS=10000000 -metadata:s:v BPS-eng=10000000 -s 2160x1215 -metadata:s:v "title=FHD HDR" -metadata:s:v "handler_name=FHD HDR" -tag:v hvc1 -c:a:0 aac -map 0:1 -ac:a:0 6 -b:a:0 1176k -metadata:s:a:0 BPS=1176000 -metadata:s:a:0 BPS-eng=1176000 -metadata:s:a:0 "title=5.1 Channel" -metadata:s:a:0 "handler_name=5.1 Channel" -metadata:s:a:0 language=eng -disposition:a:0 +default-dub-original-comment-lyrics-karaoke-forced-hearing_impaired-visual_impaired-captions -c:a:1 aac -map 0:1 -ac:a:1 2 -b:a:1 256k -metadata:s:a:1 BPS=256000 -metadata:s:a:1 BPS-eng=256000 -filter:a:1 "pan=stereo|FL=0.5*FC+0.707*FL+0.707*BL+0.5*LFE|FR=0.5*FC+0.707*FR+0.707*BR+0.5*LFE" -metadata:s:a:1 title=Stereo -metadata:s:a:1 handler_name=Stereo -metadata:s:a:1 language=eng -disposition:a:1 -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 -c:s:1 mov_text -map 0:3 -metadata:s:s:1 "title=Hearing Impaired" -metadata:s:s:1 "handler_name=Hearing Impaired" -metadata:s:s:1 language=eng -disposition:s:1 -default-dub-original-comment-lyrics-karaoke-forced+hearing_impaired-visual_impaired-captions -c:s:2 mov_text -map 0:4 -metadata:s:s:2 title= -metadata:s:s:2 handler_name= -metadata:s:s:2 language=fra -disposition:s:2 -default-dub-original-comment-lyrics-karaoke-forced-hearing_impaired-visual_impaired-captions -f mp4 -threads 0 -metadata:g encoding_tool=SMA -strict -2 -y /SWAP/Test_Video_1.mp4

henryjb99 commented 1 year ago

@mdhiggins,

I think I have a working ffmpeg script that converts the video to the size I want, but I am not sure how to do this with the max-width and the mp4 automator. The problem I am facing is because the -s option is for software scaling, not hardware scaling.

If I remove -s 3840x2160 and instead use -resize 3840x3160 before the -i, then everything works fine.

This is based on the processing filters section of Nvidia FFMPEG Transcoding Guide

Processing filters Transcoding often involves not only changing format or bitrate of the input stream, but also resizing it. Two options exist for resizing on the GPU: using the npp_scale filter or the nvcuvid resize option. The nvcuvid resize option can be used when transcoding from one input to one output stream with different resolution (1:1 transcode). See the next line for an example.

ffmpeg -vsync 0 –hwaccel cuvid -c:v h264_cuvid –resize 1280x720 -i input.mp4 -c:a copy -c:v h264_nvenc -b:v 5M output.mp4 If multiple output resolutions are needed (1:N transcode), the scale_npp filter can resize decoded frames on the GPU. This way we can generate multiple output streams with multiple different resolutions but only using one resize step for all streams. See the next line for an example of 1:2 transcode.

ffmpeg -vsync 0 -hwaccel cuda -hwaccel_output_format cuda -i input.mp4 \ -c:a copy –vf scale_npp=1280:720 -c:v h264_nvenc -b:v 5M output_720.mp4 \ -c:a copy -vf scale_npp=640:320 -c:v h264_nvenc -b:v 3M output_360.mp4 Using -vf "scale_npp=1280:720" will set scale_npp as filter for the decoded images

The interpolation algorithm can be defined for scale_npp as an additional argument. Cubic interpolation is used by default but other algorithms might give better results depending on scale factor and images. Using the super-sampling algorithm is recommended for best quality when downscaling. See below for an example:

ffmpeg -vsync 0 -hwaccel cuda -hwaccel_output_format cuda -i input.mp4 -c:a copy –vf scale_npp=1280:720:interp_algo=super -c:v h264_nvenc -b:v 5M output_720.mp4


Here is an example broken command (top) and working command (bottom)

/usr/local/bin/ffmpeg -fix_sub_duration -init_hw_device cuda=sma:0 -hwaccel_device sma -hwaccel cuda -hwaccel_output_format cuda -vcodec hevc_cuvid -i /shared/Converted/new_folder/Test_Video1.mkv -vcodec hevc_nvenc -map 0:0 -vb 10000k -metadata:s:v BPS=10000000 -metadata:s:v BPS-eng=10000000 -s 3840x2160 -metadata:s:v "title=FHD HDR" -metadata:s:v "handler_name=FHD HDR" -tag:v hvc1 -c:a:0 aac -map 0:1 -ac:a:0 6 -b:a:0 1176k -metadata:s:a:0 BPS=1176000 -metadata:s:a:0 BPS-eng=1176000 -metadata:s:a:0 "title=5.1 Channel" -metadata:s:a:0 "handler_name=5.1 Channel" -metadata:s:a:0 language=eng -disposition:a:0 +default-dub-original-comment-lyrics-karaoke-forced-hearing_impaired-visual_impaired-captions -c:a:1 aac -map 0:1 -ac:a:1 2 -b:a:1 256k -metadata:s:a:1 BPS=256000 -metadata:s:a:1 BPS-eng=256000 -filter:a:1 "pan=stereo|FL=0.5*FC+0.707*FL+0.707*BL+0.5*LFE|FR=0.5*FC+0.707*FR+0.707*BR+0.5*LFE" -metadata:s:a:1 title=Stereo -metadata:s:a:1 handler_name=Stereo -metadata:s:a:1 language=eng -disposition:a:1 -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 -c:s:1 mov_text -map 0:3 -metadata:s:s:1 "title=Hearing Impaired" -metadata:s:s:1 "handler_name=Hearing Impaired" -metadata:s:s:1 language=eng -disposition:s:1 -default-dub-original-comment-lyrics-karaoke-forced+hearing_impaired-visual_impaired-captions -c:s:2 mov_text -map 0:4 -metadata:s:s:2 title= -metadata:s:s:2 handler_name= -metadata:s:s:2 language=fra -disposition:s:2 -default-dub-original-comment-lyrics-karaoke-forced-hearing_impaired-visual_impaired-captions -f mp4 -threads 0 -metadata:g encoding_tool=SMA -strict -2 -y /SWAP/Test_Video1.mp4

/usr/local/bin/ffmpeg -fix_sub_duration -init_hw_device cuda=sma:0 -hwaccel_device sma -hwaccel cuda -hwaccel_output_format cuda -vcodec hevc_cuvid -resize 3840x2160 -i /shared/Converted/new_folder/Test_Video1.mkv -vcodec hevc_nvenc -map 0:0 -vb 10000k -metadata:s:v BPS=10000000 -metadata:s:v BPS-eng=10000000 -metadata:s:v "title=FHD HDR" -metadata:s:v "handler_name=FHD HDR" -tag:v hvc1 -c:a:0 aac -map 0:1 -ac:a:0 6 -b:a:0 1176k -metadata:s:a:0 BPS=1176000 -metadata:s:a:0 BPS-eng=1176000 -metadata:s:a:0 "title=5.1 Channel" -metadata:s:a:0 "handler_name=5.1 Channel" -metadata:s:a:0 language=eng -disposition:a:0 +default-dub-original-comment-lyrics-karaoke-forced-hearing_impaired-visual_impaired-captions -c:a:1 aac -map 0:1 -ac:a:1 2 -b:a:1 256k -metadata:s:a:1 BPS=256000 -metadata:s:a:1 BPS-eng=256000 -filter:a:1 "pan=stereo|FL=0.5*FC+0.707*FL+0.707*BL+0.5*LFE|FR=0.5*FC+0.707*FR+0.707*BR+0.5*LFE" -metadata:s:a:1 title=Stereo -metadata:s:a:1 handler_name=Stereo -metadata:s:a:1 language=eng -disposition:a:1 -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 -c:s:1 mov_text -map 0:3 -metadata:s:s:1 "title=Hearing Impaired" -metadata:s:s:1 "handler_name=Hearing Impaired" -metadata:s:s:1 language=eng -disposition:s:1 -default-dub-original-comment-lyrics-karaoke-forced+hearing_impaired-visual_impaired-captions -c:s:2 mov_text -map 0:4 -metadata:s:s:2 title= -metadata:s:s:2 handler_name= -metadata:s:s:2 language=fra -disposition:s:2 -default-dub-original-comment-lyrics-karaoke-forced-hearing_impaired-visual_impaired-captions -f mp4 -threads 0 -metadata:g encoding_tool=SMA -strict -2 -y /SWAP/Test_Video1.mp4

mdhiggins commented 1 year ago

I had some logic for applying the scale filters for some of the other hardware codecs but it wasn't fully implemented for nvenc, see if this patch adds that missing functionality

a488b105291f50978d7d9da5aa4b7ac028e80419

henryjb99 commented 1 year ago

Sorry for the delay. That seems to have done it.

THANK YOU! Will plan on using this on my OS for now with Radarr, and will move to the sickbeard-sma Docker once it moves there. You are doing awsome work. Thanks again.

mdhiggins commented 1 year ago

You can use the SMA_UPDATE environment variable to get the latest repo changes every time you restart the container without needing to wait for the automated rebuild