From f780cb6073a2cdf7e577c1243b86eb86d95f1e3e Mon Sep 17 00:00:00 2001 From: WannProject Date: Sun, 6 Sep 2026 21:46:30 +0800 Subject: [PATCH] update repo --- .gitignore | 3 + .npmrc | 1 - app/Support/Clips/ClipProcessor.php | 87 +++++++++++++++++++++++------ boost.json | 19 ------- composer.json | 2 +- package.json | 1 + pnpm-workspace.yaml | 5 -- tests/Feature/ClipFlowTest.php | 78 ++++++++++++++++++++++++++ 8 files changed, 153 insertions(+), 43 deletions(-) delete mode 100644 .npmrc delete mode 100644 boost.json delete mode 100644 pnpm-workspace.yaml diff --git a/.gitignore b/.gitignore index ab79fb3..0d83863 100644 --- a/.gitignore +++ b/.gitignore @@ -41,3 +41,6 @@ yarn-error.log /.prettierrc /.prettierignore /.mcp.json +boost.json +.npmrc +pnpm-workspace.yaml diff --git a/.npmrc b/.npmrc deleted file mode 100644 index 97b895e..0000000 --- a/.npmrc +++ /dev/null @@ -1 +0,0 @@ -ignore-scripts=true diff --git a/app/Support/Clips/ClipProcessor.php b/app/Support/Clips/ClipProcessor.php index 0a77a6a..e3bd017 100644 --- a/app/Support/Clips/ClipProcessor.php +++ b/app/Support/Clips/ClipProcessor.php @@ -97,24 +97,24 @@ public function process(Clip $clip): void private function runYtDlp(Clip $clip, string $sourcePattern, int $downloadStart, int $downloadEnd): void { - try { - $result = Process::timeout($this->processingTimeout()) - ->run(array_filter([ - $this->ytDlpBinary(), - $this->jsRuntimeArgument(), - '--download-sections', - sprintf('*%s-%s', $this->timecode($downloadStart), $this->timecode($downloadEnd)), - '-f', - $clip->quality->ytDlpFormat(), - '--merge-output-format', - 'mp4', - '--no-playlist', - '-o', + $result = $this->runDownloadCommand( + $this->downloadCommand($clip, $sourcePattern, $downloadStart, $downloadEnd), + ); + + if ($result->failed() && $this->shouldRetryWithAlternativeClient($result->errorOutput())) { + // The default web player client returns IP-locked/expiring + // googlevideo.com stream URLs that fail with HTTP 403 when the + // download starts. Re-extracting with the android client avoids + // the IP-lock, so re-run the download with that client. + $result = $this->runDownloadCommand( + $this->downloadCommand( + $clip, $sourcePattern, - $clip->source_url, - ])); - } catch (ProcessTimedOutException $exception) { - throw new RuntimeException('yt-dlp terlalu lama mengambil stream video.', previous: $exception); + $downloadStart, + $downloadEnd, + ['youtube:player_client=android,web'], + ), + ); } if ($result->failed()) { @@ -122,6 +122,59 @@ private function runYtDlp(Clip $clip, string $sourcePattern, int $downloadStart, } } + /** + * @param array $extractorArgs + * @return array + */ + private function downloadCommand(Clip $clip, string $sourcePattern, int $downloadStart, int $downloadEnd, array $extractorArgs = []): array + { + $command = [ + $this->ytDlpBinary(), + $this->jsRuntimeArgument(), + '--download-sections', + sprintf('*%s-%s', $this->timecode($downloadStart), $this->timecode($downloadEnd)), + '-f', + $clip->quality->ytDlpFormat(), + '--merge-output-format', + 'mp4', + '--no-playlist', + ]; + + foreach ($extractorArgs as $extractorArg) { + $command[] = '--extractor-args'; + $command[] = $extractorArg; + } + + $command[] = '-o'; + $command[] = $sourcePattern; + $command[] = $clip->source_url; + + return array_values(array_filter($command)); + } + + /** + * @param array $command + */ + private function runDownloadCommand(array $command) + { + try { + return Process::timeout($this->processingTimeout())->run($command); + } catch (ProcessTimedOutException $exception) { + throw new RuntimeException('yt-dlp terlalu lama mengambil stream video.', previous: $exception); + } + } + + private function shouldRetryWithAlternativeClient(string $errorOutput): bool + { + $message = strtolower(trim($errorOutput)); + + return str_contains($message, 'http error 403') + || str_contains($message, '403 forbidden') + || str_contains($message, 'access denied') + || str_contains($message, 'sign in to confirm') + || str_contains($message, 'not a bot'); + } + /** * Build the optional `--js-runtimes` argument so yt-dlp can fully extract * YouTube data (including formats and captions). Returns null when diff --git a/boost.json b/boost.json deleted file mode 100644 index 03cf198..0000000 --- a/boost.json +++ /dev/null @@ -1,19 +0,0 @@ -{ - "agents": [ - "claude_code", - "codex", - "opencode" - ], - "cloud": false, - "guidelines": true, - "mcp": true, - "nightwatch": false, - "sail": false, - "skills": [ - "laravel-best-practices", - "wayfinder-development", - "pest-testing", - "inertia-react-development", - "tailwindcss-development" - ] -} diff --git a/composer.json b/composer.json index 4a0fa35..976a5dd 100644 --- a/composer.json +++ b/composer.json @@ -7,7 +7,7 @@ "laravel", "framework" ], - "license": "MIT", + "license": "AGPL-3.0-or-later", "require": { "php": "^8.3", "inertiajs/inertia-laravel": "^3.0", diff --git a/package.json b/package.json index 7f8e935..931bdcf 100644 --- a/package.json +++ b/package.json @@ -1,4 +1,5 @@ { + "license": "AGPL-3.0-or-later", "$schema": "https://www.schemastore.org/package.json", "private": true, "type": "module", diff --git a/pnpm-workspace.yaml b/pnpm-workspace.yaml deleted file mode 100644 index 04c96fd..0000000 --- a/pnpm-workspace.yaml +++ /dev/null @@ -1,5 +0,0 @@ -packages: - - '.' - -publicHoistPattern: - - '@inertiajs/core' diff --git a/tests/Feature/ClipFlowTest.php b/tests/Feature/ClipFlowTest.php index 7b6ca62..2c026bb 100644 --- a/tests/Feature/ClipFlowTest.php +++ b/tests/Feature/ClipFlowTest.php @@ -1051,6 +1051,84 @@ ->assertJsonPath('message', 'Nama file hanya bisa diubah setelah klip selesai diproses.'); }); +test('processing job retries yt dlp with an alternative player client on a 403 stream error', function () { + Storage::fake('local'); + Process::preventStrayProcesses(); + Process::fake([ + '*yt-dlp*' => Process::sequence() + ->push(Process::result( + errorOutput: '[https @ 0x5578] HTTP error 403 Forbidden [in#0 @ 0x5578] Error opening input: Server returned 403 Forbidden (access denied)', + exitCode: 1, + )) + ->push(Process::result()), + '*' => Process::result(), + ]); + + config([ + 'freekliping.download_buffer_seconds' => 3, + 'freekliping.output_disk' => 'local', + 'freekliping.processing_timeout' => 600, + 'freekliping.retention_hours' => 1, + ]); + + $clip = Clip::query()->create([ + 'source_url' => 'https://youtu.be/dQw4w9WgXcQ', + 'youtube_video_id' => 'dQw4w9WgXcQ', + 'title' => 'Example video', + 'channel' => 'Example channel', + 'duration_seconds' => 300, + 'start_seconds' => 30, + 'end_seconds' => 60, + 'status' => ClipStatus::Queued, + 'progress' => 5, + ]); + + $workDirectory = storage_path("app/clip-processing/{$clip->uuid}"); + File::ensureDirectoryExists($workDirectory); + File::put("{$workDirectory}/source.mp4", 'source-video'); + File::put("{$workDirectory}/output.mp4", 'processed-video'); + + (new ProcessClip($clip->id))->handle(app(ClipProcessor::class)); + + $clip->refresh(); + + expect($clip->status)->toBe(ClipStatus::Completed) + ->and($clip->progress)->toBe(100) + ->and($clip->output_path)->toBe("clips/{$clip->uuid}.mp4"); + + Process::assertRan(fn (PendingProcess $process, ProcessResult $result): bool => $process->command === [ + 'yt-dlp', + '--js-runtimes=node', + '--download-sections', + '*00:00:27-00:01:03', + '-f', + 'bv*+ba/b', + '--merge-output-format', + 'mp4', + '--no-playlist', + '-o', + "{$workDirectory}/source.%(ext)s", + 'https://youtu.be/dQw4w9WgXcQ', + ]); + + Process::assertRan(fn (PendingProcess $process, ProcessResult $result): bool => $process->command === [ + 'yt-dlp', + '--js-runtimes=node', + '--download-sections', + '*00:00:27-00:01:03', + '-f', + 'bv*+ba/b', + '--merge-output-format', + 'mp4', + '--no-playlist', + '--extractor-args', + 'youtube:player_client=android,web', + '-o', + "{$workDirectory}/source.%(ext)s", + 'https://youtu.be/dQw4w9WgXcQ', + ]); +}); + function ytDlpMetadataOutput(int $duration = 123, array $subtitles = [], array $automaticCaptions = []): string { return json_encode([