Skip to content

Commit

Permalink
organize transcoding parameters in config files
Browse files Browse the repository at this point in the history
  • Loading branch information
gael-connan-cybex committed Aug 16, 2024
1 parent 8bef342 commit 8d84d77
Show file tree
Hide file tree
Showing 11 changed files with 71 additions and 68 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
14 changes: 2 additions & 12 deletions app/Enums/Decoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) ?? [];
}
}
20 changes: 10 additions & 10 deletions app/Enums/Encoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) ?? [],
);
}

Expand Down
2 changes: 1 addition & 1 deletion app/Enums/StreamingFormat.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
6 changes: 3 additions & 3 deletions app/Jobs/TranscodeVideo.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}

/**
Expand Down Expand Up @@ -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)
);
Expand Down
7 changes: 7 additions & 0 deletions config/decoder/nvidia_cuda.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

return [
'-hwaccel', 'cuda',
'-hwaccel_output_format','cuda',
'-extra_hw_frames', env('TRANSMORPHER_VIDEO_DECODER_NVIDIA_HWFRAMES', 10),
];
10 changes: 10 additions & 0 deletions config/encoder/cpu_h264.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

return [
// -omit data streams (e.g. timecodes). Transcoding sometimes failed when data streams were not omitted.
'-dn',
//omit attachments (e.g. metadata files). Metadata should not be publicly available.
'-map', '-0:t?',
// omit subtitles. Subtitles would need an encoder configuration for DASH, and possibly HLS.
'-sn',
];
10 changes: 10 additions & 0 deletions config/encoder/cpu_hevc.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

return [
// -omit data streams (e.g. timecodes). Transcoding sometimes failed when data streams were not omitted.
'-dn',
//omit attachments (e.g. metadata files). Metadata should not be publicly available.
'-map', '-0:t?',
// omit subtitles. Subtitles would need an encoder configuration for DASH, and possibly HLS.
'-sn',
];
11 changes: 11 additions & 0 deletions config/encoder/nvidia_h264.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

return [
'-preset', env('TRANSMORPHER_VIDEO_ENCODER_NVIDIA_PRESET', 'p4'),
// -omit data streams (e.g. timecodes). Transcoding sometimes failed when data streams were not omitted.
'-dn',
//omit attachments (e.g. metadata files). Metadata should not be publicly available.
'-map', '-0:t?',
// omit subtitles. Subtitles would need an encoder configuration for DASH, and possibly HLS.
'-sn',
];
11 changes: 11 additions & 0 deletions config/encoder/nvidia_hevc.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

return [
'-preset', env('TRANSMORPHER_VIDEO_ENCODER_NVIDIA_PRESET', 'p4'),
// -omit data streams (e.g. timecodes). Transcoding sometimes failed when data streams were not omitted.
'-dn',
//omit attachments (e.g. metadata files). Metadata should not be publicly available.
'-map', '-0:t?',
// omit subtitles. Subtitles would need an encoder configuration for DASH, and possibly HLS.
'-sn',
];
45 changes: 4 additions & 41 deletions config/transmorpher.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,34 +87,25 @@
|--------------------------------------------------------------------------
|
| The decoder used when transcoding videos.
| These are defined through the `config/decoder` files.
|
| You can choose from:
| cpu, nvidia_cuda
*/
'decoder' => [
'name' => env('TRANSMORPHER_VIDEO_DECODER', 'cpu'),
'nvidia' => [
'hwFrames' => env('TRANSMORPHER_VIDEO_DECODER_NVIDIA_HWFRAMES', 10),
],
],
'decoder' => env('TRANSMORPHER_VIDEO_DECODER', 'cpu'),

/*
|--------------------------------------------------------------------------
| Video Encoder
|--------------------------------------------------------------------------
|
| 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'),

/*
|--------------------------------------------------------------------------
Expand All @@ -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
Expand Down

0 comments on commit 8d84d77

Please sign in to comment.