Skip to content

Commit

Permalink
Download files from url & fix cropdetect (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tschucki authored Dec 22, 2024
1 parent 8cdd05e commit 6d45ba4
Show file tree
Hide file tree
Showing 28 changed files with 455 additions and 49 deletions.
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,5 @@ FFPROBE_BINARIES=/usr/bin/ffprobe
REVERB_APP_ID=my-app-id
REVERB_APP_KEY=my-app-key
REVERB_APP_SECRET=my-app-secret

YT_DLP_PATH=
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class AudioQualityFilterOperation implements MediaFormatOperation
{
public Conversion $conversion;

private int $currentBitrate;
private ?int $currentBitrate;

public function __construct(Conversion $conversion)
{
Expand All @@ -23,6 +23,10 @@ public function __construct(Conversion $conversion)

public function applyToFormat(DefaultVideo $format): DefaultVideo
{
if ($this->conversion->audio === false || $this->currentBitrate === null) {
return $format;
}

$maxQuality = $this->conversion->audio_quality;

if ($maxQuality < 0 || $maxQuality > 1) {
Expand All @@ -44,7 +48,7 @@ public function applyToFormat(DefaultVideo $format): DefaultVideo
private function prepareData(): void
{
$probe = app(FFProbe::class);

$this->currentBitrate = null;
$streams = $probe->streams(Storage::disk($this->conversion->file->disk)->path($this->conversion->file->filename));
foreach ($streams as $stream) {
if ($stream->get('codec_type') === 'audio') {
Expand Down
2 changes: 1 addition & 1 deletion app/Conversion/MediaOperations/AutoCropFilterOperation.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ private function prepareData(): void
$path = Storage::disk($this->conversion->file->disk)->path($this->conversion->file->filename);
$ffmpeg = config('laravel-ffmpeg.ffmpeg.binaries');

$process = Process::run("{$ffmpeg} -flags2 +export_mvs -i {$path} -vf cropdetect=mode=mvedges -f null - 2>&1 | awk '/crop/ { print \$NF }' | tail -1");
$process = Process::run("{$ffmpeg} -flags2 +export_mvs -i {$path} -vf cropdetect -f null - 2>&1 | awk '/crop/ { print \$NF }' | tail -1");

$this->crop = trim($process->output());
}
Expand Down
2 changes: 1 addition & 1 deletion app/Conversion/MediaOperations/MaxSizeOperation.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function applyToFormat(DefaultVideo $format): DefaultVideo
$maxSizeInBits = $maxSizeInMB * 1024 * 1024 * 8;
$videoBitrate = floor(($maxSizeInBits / $duration) - floor($audioBitrateInKiloBits));

if ($videoBitrate > $this->format->get('bit_rate')) {
if (($videoBitrate * $duration) >= $this->format->get('bit_rate')) {
return $format;
}

Expand Down
2 changes: 2 additions & 0 deletions app/Enums/ConversionStatus.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

enum ConversionStatus
{
public const DOWNLOADING = 'downloading';

public const PREPARING = 'preparing';

public const PENDING = 'pending';
Expand Down
3 changes: 2 additions & 1 deletion app/Events/ConversionFinished.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class ConversionFinished implements ShouldBroadcast
class ConversionFinished implements ShouldBroadcast, ShouldQueue
{
use Dispatchable;
use InteractsWithSockets;
Expand Down
5 changes: 3 additions & 2 deletions app/Events/ConversionProgressEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class ConversionProgressEvent implements ShouldBroadcast
class ConversionProgressEvent implements ShouldBroadcast, ShouldQueue
{
use Dispatchable;
use InteractsWithSockets;
Expand All @@ -35,7 +36,7 @@ public function __construct(
$this->percentage = $percentage;
$this->remaining = $remaining;
$this->rate = $rate;
$this->sessionId = Conversion::find($conversionId)->file->session_id;
$this->sessionId = Conversion::find($conversionId)->session_id;
}

public function broadcastOn(): array
Expand Down
5 changes: 3 additions & 2 deletions app/Events/ConversionUpdated.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class ConversionUpdated implements ShouldBroadcast
class ConversionUpdated implements ShouldBroadcast, ShouldQueue
{
use Dispatchable;
use InteractsWithSockets;
Expand All @@ -26,7 +27,7 @@ public function __construct(string $conversionId)
$this->conversionId = $conversionId;
$conversion = Conversion::with('file')->where('id', $conversionId)->first();
$this->conversion = $conversion->toArray();
$this->sessionId = Conversion::find($conversionId)->file->session_id;
$this->sessionId = Conversion::find($conversionId)->session_id;
}

public function broadcastOn(): array
Expand Down
54 changes: 54 additions & 0 deletions app/Events/DownloadProgress.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace App\Events;

use App\Models\Conversion;
use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class DownloadProgress implements ShouldBroadcast, ShouldQueue
{
use Dispatchable;
use InteractsWithSockets;
use SerializesModels;

public ?string $conversionId;

public ?string $sessionId;

public ?string $progressTarget;

public ?string $percentage;

public ?string $size;

public ?string $speed;

public ?string $eta;

private ?string $totalTime;

public function __construct(string $conversionId, ?string $progressTarget = null, ?string $percentage = null, ?string $size = null, ?string $speed = null, ?string $eta = null, ?string $totalTime = null)
{
$this->conversionId = $conversionId;
$conversion = Conversion::find($this->conversionId);
$this->sessionId = $conversion->session_id;
$this->progressTarget = $progressTarget;
$this->percentage = $percentage;
$this->size = $size;
$this->speed = $speed;
$this->eta = $eta;
$this->totalTime = $totalTime;
}

public function broadcastOn(): array
{
return [
new Channel('session.' . $this->sessionId),
];
}
}
8 changes: 2 additions & 6 deletions app/Events/FileUploadFailed.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,18 @@
use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class FileUploadFailed implements ShouldBroadcast
class FileUploadFailed implements ShouldBroadcast, ShouldQueue
{
use Dispatchable;
use InteractsWithSockets;
use SerializesModels;

public function __construct(public string $sessionId) {}

/**
* Get the channels the event should broadcast on.
*
* @return array<int, \Illuminate\Broadcasting\Channel>
*/
public function broadcastOn(): array
{
return [
Expand Down
3 changes: 2 additions & 1 deletion app/Events/FileUploadSuccessful.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class FileUploadSuccessful implements ShouldBroadcast
class FileUploadSuccessful implements ShouldBroadcast, ShouldQueue
{
use Dispatchable;
use InteractsWithSockets;
Expand Down
3 changes: 2 additions & 1 deletion app/Events/PreviousFilesDeleted.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class PreviousFilesDeleted implements ShouldBroadcast
class PreviousFilesDeleted implements ShouldBroadcast, ShouldQueue
{
use Dispatchable;
use InteractsWithSockets;
Expand Down
10 changes: 3 additions & 7 deletions app/Http/Controllers/ListConverterController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,16 @@ public function __construct(Request $request)

public function index(Request $request): Response
{
$conversions = Conversion::with('file')->whereHas('file', function ($query) {
$query->where('session_id', $this->sessionId);
})->get();
$conversions = Conversion::with('file')->where('session_id', $this->sessionId)->get();

return Inertia::render('Converter/List', [
'conversions' => $conversions,
'conversions' => $conversions->toArray(),
]);
}

public function myConversions(Request $request): array
{
$conversions = Conversion::with('file')->whereHas('file', function ($query) {
$query->where('session_id', $this->sessionId);
})->get();
$conversions = Conversion::with('file')->where('session_id', $this->sessionId)->get();

return $conversions->toArray();
}
Expand Down
21 changes: 13 additions & 8 deletions app/Http/Controllers/StartConverterController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Http\Controllers;

use App\Conversion\ConversionSettings;
use App\Enums\ConversionStatus;
use App\Events\FileUploadFailed;
use App\Events\FileUploadSuccessful;
use App\Events\PreviousFilesDeleted;
Expand All @@ -28,29 +29,32 @@ public function __invoke(StartConverterRequest $request)

$this->deleteOldFiles();

$file = $this->handleUploadedFile($request);
if ($request->hasFile('file')) {
$file = $this->handleUploadedFile($request);
}

$conversionSettings = ConversionSettings::fromRequest($validated);

$conversion = Conversion::create([
...$conversionSettings->toArray(),
'file_id' => $file->id,
'status' => ConversionStatus::PENDING,
'session_id' => $this->sessionId,
'file_id' => $file->id ?? null,
'url' => $validated['url'] ?? null,
]);

//ConversionJob::dispatchSync($conversion->id);

ConversionJob::dispatch($conversion->id)->onQueue('converter');
if ($request->hasFile('file')) {
ConversionJob::dispatch($conversion->id)->onQueue('converter');
}

return redirect()->route('conversions.list');
}

private function handleUploadedFile(StartConverterRequest $request): File
{
$file = null;

if ($request->hasFile('file')) {
$file = $this->storeUploadedFile($request->file('file'));
}
$file = $this->storeUploadedFile($request->file('file'));

if ($file === null) {
FileUploadFailed::dispatch($this->sessionId);
Expand Down Expand Up @@ -87,6 +91,7 @@ private function deleteOldFiles(): void
{
$countOldFiles = File::where('session_id', $this->sessionId)->count();

Conversion::where('session_id', $this->sessionId)->delete();
File::where('session_id', $this->sessionId)->delete();

if ($countOldFiles > 0) {
Expand Down
5 changes: 1 addition & 4 deletions app/Http/Middleware/HandleInertiaRequests.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace App\Http\Middleware;

use App\Models\Conversion;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\Request;
use Inertia\Middleware;

Expand Down Expand Up @@ -42,9 +41,7 @@ public function share(Request $request): array
'session' => [
'id' => $request->session()->getId(),
],
'conversions' => fn () => Conversion::whereHas('file', static function (Builder $query) use ($request) {
$query->where('session_id', $request->session()->getId());
})->select('id')->get(),
'conversions' => fn () => Conversion::where('session_id', $request->session()->getId())->select('id')->get(),
]);
}
}
2 changes: 2 additions & 0 deletions app/Jobs/ConversionJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use FFMpeg\Format\Video\X264;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Foundation\Queue\Queueable;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;
Expand All @@ -15,6 +16,7 @@

class ConversionJob implements ShouldBeUnique, ShouldQueue
{
use Dispatchable;
use Queueable;

private string $conversionId;
Expand Down
Loading

0 comments on commit 6d45ba4

Please sign in to comment.