File size: 2,799 Bytes
74acf19
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<?php

namespace App\Services;

use App\Models\SocialMediaAsset;
use App\Models\SocialPost;

class SocialPostMediaPreparationService
{
    public function __construct(
        private FreepikMusicService $music,
        private VideoMusicComposerService $composer
    ) {
    }

    public function prepareForPublishing(SocialPost $post): bool
    {
        if (! $this->shouldAutoApplyBgm($post)) {
            return false;
        }

        $bgm = data_get($post->meta, 'bgm', []);

        $prompt = trim((string) data_get($bgm, 'prompt', ''));
        if ($prompt === '') {
            throw new \RuntimeException('BGM auto aktif, tapi prompt musik masih kosong. Isi dulu sebelum publish otomatis dijalankan.');
        }

        $duration = (int) data_get($bgm, 'duration', 15);
        $volume = (float) data_get($bgm, 'volume', 0.24);

        $sourceVideoUrl = (string) $post->video_url;
        $track = $this->music->generate($prompt, $duration);
        $mixed = $this->composer->compose($sourceVideoUrl, (string) ($track['url'] ?? ''), $volume);

        $meta = is_array($post->meta) ? $post->meta : [];
        $meta['bgm'] = array_merge($bgm, [
            'provider' => 'freepik',
            'applied' => true,
            'applied_at' => now()->toIso8601String(),
            'mixed_video_url' => (string) ($mixed['url'] ?? ''),
            'track_url' => (string) ($track['url'] ?? ''),
        ]);

        $post->update([
            'video_url' => (string) ($mixed['url'] ?? $post->video_url),
            'meta' => $meta,
        ]);

        SocialMediaAsset::query()->create([
            'user_id' => $post->user_id,
            'type' => 'video',
            'provider' => 'freepik-music-mix',
            'prompt' => $prompt,
            'url' => (string) ($mixed['url'] ?? ''),
            'thumbnail_url' => null,
            'width' => 1080,
            'height' => 1920,
            'duration_seconds' => (int) ($track['duration_seconds'] ?? $duration),
            'meta' => [
                'source_post_id' => $post->id,
                'source_video_url' => $sourceVideoUrl,
                'music_url' => $track['url'] ?? null,
                'music_provider' => 'freepik',
                'music_duration' => $duration,
                'music_volume' => $volume,
                'source' => 'pre_publish_automation',
            ],
        ]);

        return true;
    }

    public function shouldAutoApplyBgm(SocialPost $post): bool
    {
        if (blank($post->video_url)) {
            return false;
        }

        $bgm = data_get($post->meta, 'bgm', []);

        return (bool) data_get($bgm, 'enabled', false)
            && ! (bool) data_get($bgm, 'applied', false)
            && data_get($bgm, 'provider', 'freepik') === 'freepik';
    }
}