Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,6 @@ yarn-error.log
/.prettierrc
/.prettierignore
/.mcp.json
boost.json
.npmrc
pnpm-workspace.yaml
1 change: 0 additions & 1 deletion .npmrc

This file was deleted.

87 changes: 70 additions & 17 deletions app/Support/Clips/ClipProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,31 +97,84 @@ 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()) {
throw new RuntimeException($this->processFailureMessage('yt-dlp', $result->errorOutput()));
}
}

/**
* @param array<int, string> $extractorArgs
* @return array<int, string>
*/
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<int, string> $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
Expand Down
19 changes: 0 additions & 19 deletions boost.json

This file was deleted.

2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"laravel",
"framework"
],
"license": "MIT",
"license": "AGPL-3.0-or-later",
"require": {
"php": "^8.3",
"inertiajs/inertia-laravel": "^3.0",
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"license": "AGPL-3.0-or-later",
"$schema": "https://www.schemastore.org/package.json",
"private": true,
"type": "module",
Expand Down
5 changes: 0 additions & 5 deletions pnpm-workspace.yaml

This file was deleted.

78 changes: 78 additions & 0 deletions tests/Feature/ClipFlowTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand Down
Loading