Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hardware Transcoding #592

Closed
headcrushedindoor opened this issue Jan 13, 2023 · 5 comments
Closed

Hardware Transcoding #592

headcrushedindoor opened this issue Jan 13, 2023 · 5 comments

Comments

@headcrushedindoor
Copy link

Hey,

I'm trying to enable hw transcoding (vaapi) for video converting.
Tried to add -hwaccel vaapi; -hwaccel_output_format vaapi; -vaapi_device /dev/dri/renderD128 to custom options and h264_vaapi to codec but log tells me;

[FFmpeg] Error: ffmpeg exited with code 1: Option hwaccel (use HW accelerated decoding) cannot be applied to output url -hwaccel_output_format vaapi -- you are trying to apply an input option to an output file or vice versa. Move this option before the file it belongs to.

So is it possible to send custom options before "-i" of ffmpeg?

@bpatrik
Copy link
Owner

bpatrik commented Jan 14, 2023

Hi, I made a config to add custom input options to ffmpeg. Api doc that I'm using: https://www.npmjs.com/package/fluent-ffmpeg#inputoptionsoption-add-custom-input-options.
This is where it is used:

// set custom input options
if (input.input.customOptions) {
command.inputOptions(input.input.customOptions);

Can you report back about your findings? Like: are you using docker and Raspberry pi 4? Could you make hardware acceleration work? If yes, with what configuration. If its an easy addition I will make it a default settings.

@bpatrik bpatrik added this to the v2.0 milestone Jan 14, 2023
@bpatrik
Copy link
Owner

bpatrik commented Jan 14, 2023

Looking a bit around. Might not work out of the box as size breaks with vaapi according to this: fluent-ffmpeg/node-fluent-ffmpeg#1120

I'm using size:

if (input.output.resolution) {
command.size('?x' + input.output.resolution);
}

Also if you set logging to silly, it should print executed ffmpeg command.

@a-ilin
Copy link

a-ilin commented Jan 22, 2023

Hello! Thanks a lot for great gallery!

I can confirm that the options specified in the field Video transcoding / Custom Options are going after the input file.
It would be very nice to have this fixed, as HW acceleration speeds up conversion in 8-13 times in my case!

This was tested with version 1.9.5 using Alpine Docker image.

Here are options I specified for VAAPI video conversion: -init_hw_device vaapi=intel:/dev/dri/renderD128; -hwaccel vaapi; -hwaccel_output_format vaapi; -hwaccel_device intel; -filter_hw_device intel;
Codec: h264_vaapi

Pigallery2 runs ffmpeg with the following command line (taken from logs):
ffmpeg -i input.mp4 -y -b:v 5120k -vcodec h264_vaapi -r 25 -filter:v scale=w=trunc(oh*a/2)*2:h=720 -crf 23 -preset medium -init_hw_device vaapi=intel:/dev/dri/renderD128 -hwaccel vaapi -hwaccel_output_format vaapi -hwaccel_device intel -filter_hw_device intel -f mp4 output.mp4

As you see, the specified options are placed before the output file. The correct location would be before the input file, as below:
ffmpeg -init_hw_device vaapi=intel:/dev/dri/renderD128 -hwaccel vaapi -hwaccel_output_format vaapi -hwaccel_device intel -filter_hw_device intel -i input.mp4 -y -b:v 5120k -vcodec h264_vaapi -r 25 -filter:v 'scale_vaapi=w=trunc(oh*a/2)*2:h=720' -crf 23 -preset medium -f mp4 output.mp4

Also a change in scaling filter is needed: instead of scale there is scale_vaapi.

Can you consider making ffmpeg options more customizable?

@a-ilin
Copy link

a-ilin commented Jan 23, 2023

I've just noticed the input options are already added in the nightly builds.

With that I've managed to workaround other issues and enable HW acceleration with the following approach.

  1. Use custom input options: -init_hw_device vaapi=intel:/dev/dri/renderD128;-hwaccel vaapi;-hwaccel_output_format vaapi;-hwaccel_device intel;-filter_hw_device intel

Note the absence of spaces in the options near ; semicolons. Adding spaces causes incorrect empty arguments being passed to ffmpeg.

  1. Use codec: h264_vaapi.

  2. Apply the commits from PR: Detect hardware accelerated codec and use the correct scale filter fluent-ffmpeg/node-fluent-ffmpeg#1121

This may be done with patch-package tool: https://www.npmjs.com/package/patch-package

The PR has a bug when videoCodec is not the first option, as in pigallery2. To fix the bug apply the following patch to fluent-ffmpeg (again, patch-package would work):

diff --git a/lib/options/videosize.js b/lib/options/videosize.js
--- a/lib/options/videosize.js
+++ b/lib/options/videosize.js
@@ -7,8 +7,8 @@


 function getScaleFilter(output) {
-  if (output.video.get('-vcodec').length > 1) {
-    let codec = output.video.get('-vcodec')[1];
+  if (!!output.video.find('-vcodec', 1)) {
+    let codec = output.video.find('-vcodec', 1)[0];

     if (codec.endsWith('_vaapi')) {
       return 'scale_vaapi';

After those steps are done HW acceleration works fine in my case.

@bpatrik
Copy link
Owner

bpatrik commented Oct 7, 2023

I'm not sure what is the status of this issue.

Looking at the cod of the current "fluent-ffmpeg": "2.1.2", function getScaleFilter(output) function does not exist anymore and also did not exist for a while. There was no new release in the last 6 years: https://www.npmjs.com/package/fluent-ffmpeg.

I'm closing this bug with the assumption that no further action is needed.

@bpatrik bpatrik closed this as completed Oct 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants