Skip to content

Commit

Permalink
feat: added new option to replace audio model instead of creating a n…
Browse files Browse the repository at this point in the history
…ew one when backflling (#766)
  • Loading branch information
Kyrch authored Dec 12, 2024
1 parent a38c023 commit d343094
Show file tree
Hide file tree
Showing 16 changed files with 257 additions and 228 deletions.
8 changes: 5 additions & 3 deletions app/Actions/Discord/DiscordVideoNotificationAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace App\Actions\Discord;

use App\Enums\Actions\Models\Wiki\Video\NotificationType;
use App\Enums\Actions\Models\Wiki\Video\ShouldForceThread;
use App\Models\Wiki\Video;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Arr;
Expand All @@ -24,8 +26,8 @@ class DiscordVideoNotificationAction
*/
public function handle(Collection $videos, array $fields): void
{
$type = Arr::get($fields, 'notification-type');
$shouldForce = Arr::get($fields, 'should-force-thread');
$type = Arr::get($fields, NotificationType::getFieldKey());
$shouldForce = ShouldForceThread::from(intval(Arr::get($fields, ShouldForceThread::getFieldKey())));

$newVideos = [];

Expand All @@ -42,7 +44,7 @@ public function handle(Collection $videos, array $fields): void
$anime = $theme->anime;

if ($anime->discordthread === null) {
if ($shouldForce === 'no') return;
if ($shouldForce === ShouldForceThread::NO) return;

$threadAction = new DiscordThreadAction();

Expand Down
38 changes: 33 additions & 5 deletions app/Actions/Models/Wiki/Video/Audio/BackfillAudioAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@

use App\Actions\ActionResult;
use App\Actions\Models\BackfillAction;
use App\Actions\Storage\Wiki\Audio\MoveAudioAction;
use App\Actions\Storage\Wiki\Audio\UploadAudioAction;
use App\Constants\Config\VideoConstants;
use App\Enums\Actions\ActionStatus;
use App\Enums\Actions\Models\Wiki\Video\DeriveSourceVideo;
use App\Enums\Actions\Models\Wiki\Video\OverwriteAudio;
use App\Enums\Actions\Models\Wiki\Video\ReplaceRelatedAudio;
use App\Models\Wiki\Anime;
use App\Models\Wiki\Anime\AnimeTheme;
use App\Models\Wiki\Anime\Theme\AnimeThemeEntry;
Expand Down Expand Up @@ -47,7 +49,8 @@ class BackfillAudioAction extends BackfillAction
public function __construct(
Video $video,
protected readonly DeriveSourceVideo $deriveSourceVideo = DeriveSourceVideo::YES,
protected readonly OverwriteAudio $overwriteAudio = OverwriteAudio::NO
protected readonly OverwriteAudio $overwriteAudio = OverwriteAudio::NO,
protected readonly ReplaceRelatedAudio $replaceRelatedAudio = ReplaceRelatedAudio::NO,
) {
parent::__construct($video);
}
Expand Down Expand Up @@ -137,6 +140,16 @@ protected function overwriteAudio(): bool
return OverwriteAudio::YES === $this->overwriteAudio;
}

/**
* Determine if the new audio should replace a related one.
*
* @return bool
*/
protected function replaceRelatedAudio(): bool
{
return ReplaceRelatedAudio::YES === $this->replaceRelatedAudio;
}

/**
* Get or Create Audio.
*
Expand All @@ -148,7 +161,7 @@ protected function getAudio(): ?Audio
{
// Allow bypassing of source video derivation
$sourceVideo = $this->deriveSourceVideo()
? $this->getSourceVideo()
? $this->getSourceVideo($this->replaceRelatedAudio() ? '<' : '>')
: $this->getModel();

// It's possible that the video is not attached to any themes, exit early.
Expand All @@ -161,6 +174,16 @@ protected function getAudio(): ?Audio
// First, attempt to set audio from the source video
$audio = $sourceVideo->audio;

// When uploading a BD version we should get the parent audio of a WEB version and
// move the file overwriting the content later. Therefore, the old model is not deleted.
if ($this->replaceRelatedAudio() && $audio instanceof Audio) {
$moveAction = new MoveAudioAction($audio, $this->getModel()->path());

$storageResults = $moveAction->handle();

$audio = $moveAction->then($storageResults);
}

// Anticipate audio path for FFmpeg save file
$audioPath = $audio === null
? Str::replace('webm', 'ogg', $sourceVideo->path)
Expand All @@ -172,7 +195,7 @@ protected function getAudio(): ?Audio
}

// Finally, extract audio from the source video
if ($audio === null || $this->overwriteAudio()) {
if ($audio === null || $this->overwriteAudio() || $this->replaceRelatedAudio()) {
Log::info("Extracting Audio from Video '{$sourceVideo->getName()}'");

return $this->extractAudio($sourceVideo);
Expand All @@ -184,16 +207,21 @@ protected function getAudio(): ?Audio
/**
* Get the source video for the given video.
*
* @param string $operation
* @return Video|null
*/
protected function getSourceVideo(): ?Video
protected function getSourceVideo(string $operation = '>'): ?Video
{
$source = null;

$sourceCandidates = $this->getAdjacentVideos();

foreach ($sourceCandidates as $sourceCandidate) {
if (! $source instanceof Video || $sourceCandidate->getSourcePriority() > $source->getSourcePriority()) {
if ($operation === '>' && (! $source instanceof Video || $sourceCandidate->getSourcePriority() > $source->getSourcePriority())) {
$source = $sourceCandidate;
}

if ($operation === '<' && (! $source instanceof Video || $sourceCandidate->getSourcePriority() < $source->getSourcePriority())) {
$source = $sourceCandidate;
}
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use App\Actions\Models\Wiki\Video\Audio\BackfillAudioAction as BackfillAudio;
use App\Enums\Actions\Models\Wiki\Video\DeriveSourceVideo;
use App\Enums\Actions\Models\Wiki\Video\OverwriteAudio;
use App\Enums\Actions\Models\Wiki\Video\ReplaceRelatedAudio;
use App\Filament\Components\Fields\Select;
use App\Models\Wiki\Audio;
use App\Models\Wiki\Video;
Expand All @@ -30,6 +31,7 @@ trait BackfillAudioActionTrait

final public const DERIVE_SOURCE_VIDEO = 'derive_source_video';
final public const OVERWRITE_AUDIO = 'overwrite_audio';
final public const REPLACE_RELATED_AUDIO = 'replace_related_audio';

/**
* Initial setup for the action.
Expand Down Expand Up @@ -60,8 +62,9 @@ public function handle(Video $video, array $data): void
{
$deriveSourceVideo = DeriveSourceVideo::from(intval(Arr::get($data, self::DERIVE_SOURCE_VIDEO)));
$overwriteAudio = OverwriteAudio::from(intval(Arr::get($data, self::OVERWRITE_AUDIO)));
$replaceRelatedAudio = ReplaceRelatedAudio::from(intval(Arr::get($data, self::REPLACE_RELATED_AUDIO)));

$action = new BackfillAudio($video, $deriveSourceVideo, $overwriteAudio);
$action = new BackfillAudio($video, $deriveSourceVideo, $overwriteAudio, $replaceRelatedAudio);

try {
$result = $action->handle();
Expand Down Expand Up @@ -95,17 +98,24 @@ public function getForm(Form $form): Form
->schema([
Select::make(self::DERIVE_SOURCE_VIDEO)
->label(__('filament.actions.video.backfill.fields.derive_source.name'))
->helperText(__('filament.actions.video.backfill.fields.derive_source.help'))
->options(DeriveSourceVideo::asSelectArray())
->rules(['required', new Enum(DeriveSourceVideo::class)])
->default(DeriveSourceVideo::YES->value)
->helperText(__('filament.actions.video.backfill.fields.derive_source.help')),
->default(DeriveSourceVideo::YES->value),

Select::make(self::OVERWRITE_AUDIO)
->label(__('filament.actions.video.backfill.fields.overwrite.name'))
->helperText(__('filament.actions.video.backfill.fields.overwrite.help'))
->options(OverwriteAudio::asSelectArray())
->rules(['required', new Enum(OverwriteAudio::class)])
->default(OverwriteAudio::NO->value)
->helperText(__('filament.actions.video.backfill.fields.overwrite.help')),
->default(OverwriteAudio::NO->value),

Select::make(self::REPLACE_RELATED_AUDIO)
->label(__('filament.actions.video.backfill.fields.replace_related.name'))
->helperText(__('filament.actions.video.backfill.fields.replace_related.help'))
->options(ReplaceRelatedAudio::asSelectArray())
->rules(['required', new Enum(ReplaceRelatedAudio::class)])
->default(ReplaceRelatedAudio::NO->value),
]);
}
}
28 changes: 28 additions & 0 deletions app/Enums/Actions/Models/Wiki/Video/NotificationType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace App\Enums\Actions\Models\Wiki\Video;

use App\Concerns\Enums\LocalizesName;

/**
* Enum NotificationType.
*/
enum NotificationType: string
{
use LocalizesName;

case ADDED = 'added';
case UPDATED = 'updated';

/**
* Get the field key to use in the admin panel.
*
* @return string
*/
public static function getFieldKey(): string
{
return 'notification-type';
}
}
18 changes: 18 additions & 0 deletions app/Enums/Actions/Models/Wiki/Video/ReplaceRelatedAudio.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace App\Enums\Actions\Models\Wiki\Video;

use App\Concerns\Enums\LocalizesName;

/**
* Enum ReplaceRelatedAudio.
*/
enum ReplaceRelatedAudio: int
{
use LocalizesName;

case NO = 0;
case YES = 1;
}
18 changes: 18 additions & 0 deletions app/Enums/Actions/Models/Wiki/Video/ShouldBackfillAudio.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace App\Enums\Actions\Models\Wiki\Video;

use App\Concerns\Enums\LocalizesName;

/**
* Enum ShouldBackfillAudio.
*/
enum ShouldBackfillAudio: int
{
use LocalizesName;

case NO = 0;
case YES = 1;
}
28 changes: 28 additions & 0 deletions app/Enums/Actions/Models/Wiki/Video/ShouldForceThread.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace App\Enums\Actions\Models\Wiki\Video;

use App\Concerns\Enums\LocalizesName;

/**
* Enum ShouldForceThread.
*/
enum ShouldForceThread: int
{
use LocalizesName;

case NO = 0;
case YES = 1;

/**
* Get the field key to use in the admin panel.
*
* @return string
*/
public static function getFieldKey(): string
{
return 'should-force-thread';
}
}
18 changes: 18 additions & 0 deletions app/Enums/Actions/Models/Wiki/Video/ShouldSendNotification.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace App\Enums\Actions\Models\Wiki\Video;

use App\Concerns\Enums\LocalizesName;

/**
* Enum ShouldSendNotification.
*/
enum ShouldSendNotification: int
{
use LocalizesName;

case NO = 0;
case YES = 1;
}

This file was deleted.

Loading

0 comments on commit d343094

Please sign in to comment.