diff --git a/README.md b/README.md index 8e37291..36ae491 100644 --- a/README.md +++ b/README.md @@ -452,7 +452,8 @@ The default preset is `p4`. To set the high quality preset, use the following en TRANSMORPHER_VIDEO_ENCODER_NVIDIA_PRESET=p6 ``` -See the transformer config for more available options. +Each encoder has its own configuration file in the `config/encoder` folder, containing FFmpeg parameters. + Note that the optional GPU video decoding setting is experimental and unstable. By default, videos are decoded using the CPU. diff --git a/app/Enums/Decoder.php b/app/Enums/Decoder.php index 8ab5ab7..5436a5c 100644 --- a/app/Enums/Decoder.php +++ b/app/Enums/Decoder.php @@ -8,18 +8,8 @@ enum Decoder: string case CPU = 'cpu'; case NVIDIA_CUDA = 'nvidia_cuda'; - public function getInitialParameters(bool $forMp4Fallback = false): array + public function getInitialParameters(): array { - return array_merge( - match ($this) { - Decoder::NVIDIA_CUDA => [ - '-hwaccel', 'cuda', - '-hwaccel_output_format','cuda', - '-extra_hw_frames', config('transmorpher.decoder.nvidia.hwFrames'), - ], - default => [] - }, - config('transmorpher.initial_transcoding_parameters') - ); + return config(sprintf('decoder.%s', $this->value)) ?? []; } } diff --git a/app/Enums/Encoder.php b/app/Enums/Encoder.php index 36f8823..7af87ed 100644 --- a/app/Enums/Encoder.php +++ b/app/Enums/Encoder.php @@ -11,21 +11,21 @@ enum Encoder: string public function getAdditionalParameters(bool $forMp4Fallback = false): array { - return array_merge( - $forMp4Fallback ? ['-b:v', config('transmorpher.encoder.bitrate')] : [], + return array_merge($forMp4Fallback ? match ($this) { - Encoder::NVIDIA_H264 => [ + Encoder::NVIDIA_H264, Encoder::NVIDIA_HEVC => [ '-c:v', 'h264_nvenc', - '-preset', config('transmorpher.encoder.nvidia.preset') + '-b:v', env('TRANSMORPHER_VIDEO_ENCODER_BITRATE', '6000k'), ], - Encoder::NVIDIA_HEVC => [ - // Fallback MP4 video should be h264 - '-c:v', $forMp4Fallback ? 'h264_nvenc' : 'hevc_nvenc', - '-preset', config('transmorpher.encoder.nvidia.preset') + default => [ + '-b:v', env('TRANSMORPHER_VIDEO_ENCODER_BITRATE', '6000k'), ], - default => [] + } : match ($this) { + Encoder::NVIDIA_H264 => ['-c:v', 'h264_nvenc'], + Encoder::NVIDIA_HEVC => ['-c:v', 'hevc_nvenc'], + default => [], }, - config('transmorpher.additional_transcoding_parameters') + config(sprintf('decoder.%s', $this->value)) ?? [], ); } diff --git a/app/Enums/StreamingFormat.php b/app/Enums/StreamingFormat.php index e6448de..8251d6f 100644 --- a/app/Enums/StreamingFormat.php +++ b/app/Enums/StreamingFormat.php @@ -25,6 +25,6 @@ public function configure(StreamingMedia $video, Encoder $encoder): Streaming return $video->$format() ->$codec() ->autoGenerateRepresentations(config('transmorpher.representations')) - ->setAdditionalParams(config('transmorpher.additional_transcoding_parameters')); + ->setAdditionalParams($encoder->getAdditionalParameters()); } } diff --git a/app/Jobs/TranscodeVideo.php b/app/Jobs/TranscodeVideo.php index 6e055da..8f2c509 100644 --- a/app/Jobs/TranscodeVideo.php +++ b/app/Jobs/TranscodeVideo.php @@ -74,8 +74,8 @@ public function __construct( \Log::info(sprintf('Constructing job for media %s and version %s with uploadToken %s.', $version->Media->identifier, $version->getKey(), $uploadSlot->token)); $this->originalFilePath = $version->originalFilePath(); $this->uploadToken = $this->uploadSlot->token; - $this->decoder = Decoder::from(config('transmorpher.decoder.name')); - $this->encoder = Encoder::from(config('transmorpher.encoder.name')); + $this->decoder = Decoder::from(config('transmorpher.decoder')); + $this->encoder = Encoder::from(config('transmorpher.encoder')); } /** @@ -270,7 +270,7 @@ protected function generateMp4(StreamingMedia $video): void // GPU accelerated encoding cannot be set via setVideoCodec(). h264_nvenc may be set through the additional params. $video->save( (new X264()) - ->setInitialParameters($this->decoder->getInitialParameters(forMp4Fallback: true)) + ->setInitialParameters($this->decoder->getInitialParameters()) ->setAdditionalParameters($this->encoder->getAdditionalParameters(forMp4Fallback: true)), $this->localDisk->path($tempMp4Filename) ); diff --git a/config/decoder/nvidia_cuda.php b/config/decoder/nvidia_cuda.php new file mode 100644 index 0000000..e8c43dc --- /dev/null +++ b/config/decoder/nvidia_cuda.php @@ -0,0 +1,7 @@ + [ - 'name' => env('TRANSMORPHER_VIDEO_DECODER', 'cpu'), - 'nvidia' => [ - 'hwFrames' => env('TRANSMORPHER_VIDEO_DECODER_NVIDIA_HWFRAMES', 10), - ], - ], + 'decoder' => env('TRANSMORPHER_VIDEO_DECODER', 'cpu'), /* |-------------------------------------------------------------------------- @@ -104,17 +100,12 @@ |-------------------------------------------------------------------------- | | The encoder used when transcoding videos. + | Additional FFmpeg parameters are controlled through the according `config/encoder` files. | | You can choose from: | cpu_h264, cpu_hevc, nvidia_h264, nvidia_hevc */ - 'encoder' => [ - 'name' => env('TRANSMORPHER_VIDEO_ENCODER', 'cpu_h264'), - 'bitrate' => env('TRANSMORPHER_VIDEO_ENCODER_BITRATE', '6000k'), - 'nvidia' => [ - 'preset' => env('TRANSMORPHER_VIDEO_ENCODER_NVIDIA_PRESET', 'p4'), - ], - ], + 'encoder' => env('TRANSMORPHER_VIDEO_ENCODER', 'cpu_h264'), /* |-------------------------------------------------------------------------- @@ -131,34 +122,6 @@ 360, 480, 720, 1080, 1440, 2160 ], - /* - |-------------------------------------------------------------------------- - | Initial Transcoding Parameters - |-------------------------------------------------------------------------- - | - | These parameters will be added to the FFmpeg transcoding command before the input parameter. - | They take precedence over other options in this file. - */ - 'initial_transcoding_parameters' => [ - // - ], - - /* - |-------------------------------------------------------------------------- - | Additional Transcoding Parameters - |-------------------------------------------------------------------------- - | - | These parameters will be added to the FFmpeg transcoding command. - | They take precedence over other options in this file. - | - | -dn: omit data streams (e.g. timecodes). Transcoding sometimes failed when data streams were not omitted. - | -map -0:t?: omit attachments (e.g. metadata files). Metadata should not be publicly available. - | -sn: omit subtitles. Subtitles would need an encoder configuration for DASH, and possibly HLS. - */ - 'additional_transcoding_parameters' => [ - '-dn', '-map', '-0:t?', '-sn' - ], - /* |-------------------------------------------------------------------------- | CDN Helper