Skip to content

Commit

Permalink
Merge pull request #232 from norkunas/filesize
Browse files Browse the repository at this point in the history
Collect error with min/max filesize option
  • Loading branch information
norkunas authored Feb 28, 2024
2 parents 37e3bea + 8c4e030 commit 202c706
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/YoutubeDl.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ public function download(Options $options): VideoCollection

$currentVideo['extractor'] = $match['extractor'];
$currentVideo['id'] = $match['id'];
} elseif (preg_match('/\[download] File is (larger|smaller) than (min|max)-filesize/', $buffer, $match) === 1) {
$currentVideo['error'] = trim(substr($buffer, 11));
} elseif (str_starts_with($buffer, 'ERROR:')) {
$currentVideo['error'] = trim(substr($buffer, 6));
} elseif (preg_match('/Writing video( description)? metadata as JSON to:\s(?<metadataFile>.+)/', $buffer, $match) === 1) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[youtube] Extracting URL: https://www.youtube.com/watch?v=7x5lqqji9ww
[youtube] 7x5lqqji9ww: Downloading webpage
[youtube] 7x5lqqji9ww: Downloading ios player API JSON
[youtube] 7x5lqqji9ww: Downloading android player API JSON
[youtube] 7x5lqqji9ww: Downloading m3u8 information
[info] 7x5lqqji9ww: Downloading 1 format(s): 251
[download] File is larger than max-filesize (2960699 bytes > 1048576 bytes). Aborting.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[youtube] Extracting URL: https://www.youtube.com/watch?v=7x5lqqji9ww
[youtube] 7x5lqqji9ww: Downloading webpage
[youtube] 7x5lqqji9ww: Downloading ios player API JSON
[youtube] 7x5lqqji9ww: Downloading android player API JSON
[youtube] 7x5lqqji9ww: Downloading m3u8 information
[info] 7x5lqqji9ww: Downloading 1 format(s): 251
[download] File is smaller than min-filesize (2960699 bytes < 20971520 bytes). Aborting.
72 changes: 72 additions & 0 deletions tests/YoutubeDlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,78 @@ public static function provideAlreadyDownloadedVideoCases(): iterable
];
}

/**
* @param non-empty-string $url
*
* @dataProvider provideDownloadTooLargeFileCases
*/
public function testDownloadTooLargeFileCases(string $url, string $outputFile, Video $expectedEntity): void
{
$process = new StaticProcess();
$process->setOutputFile($outputFile);

$processBuilder = $this->createProcessBuilderMock($process, [
'--ignore-config',
'--ignore-errors',
'--write-info-json',
'--max-filesize=1m',
'--output=vfs://yt-dl/%(title)s-%(id)s.%(ext)s',
$url,
]);

$yt = new YoutubeDl($processBuilder);

self::assertEquals(new VideoCollection([$expectedEntity]), $yt->download(Options::create()->downloadPath($this->tmpDir)->maxFileSize('1m')->url($url)));
}

/**
* @return iterable<array{url: non-empty-string, outputFile: non-empty-string, expectedEntity: Video}>
*/
public static function provideDownloadTooLargeFileCases(): iterable
{
yield [
'url' => 'https://www.youtube.com/watch?v=7x5lqqji9ww',
'outputFile' => __DIR__.'/Fixtures/youtube-dl/youtube/larger_than_max_filesize.txt',
'expectedEntity' => new Video(['error' => 'File is larger than max-filesize (2960699 bytes > 1048576 bytes). Aborting.', 'extractor' => 'youtube']),
];
}

/**
* @param non-empty-string $url
*
* @dataProvider provideDownloadTooSmallFileCases
*/
public function testDownloadTooSmallFileCases(string $url, string $outputFile, Video $expectedEntity): void
{
$process = new StaticProcess();
$process->setOutputFile($outputFile);

$processBuilder = $this->createProcessBuilderMock($process, [
'--ignore-config',
'--ignore-errors',
'--write-info-json',
'--min-filesize=20m',
'--output=vfs://yt-dl/%(title)s-%(id)s.%(ext)s',
$url,
]);

$yt = new YoutubeDl($processBuilder);

self::assertEquals(new VideoCollection([$expectedEntity]), $yt->download(Options::create()->downloadPath($this->tmpDir)->minFileSize('20m')->url($url)));
}

/**
* @return iterable<array{url: non-empty-string, outputFile: non-empty-string, expectedEntity: Video}>
*/
public static function provideDownloadTooSmallFileCases(): iterable
{
yield [
'url' => 'https://www.youtube.com/watch?v=7x5lqqji9ww',
'outputFile' => __DIR__.'/Fixtures/youtube-dl/youtube/smaller_than_min_filesize.txt',
'expectedEntity' => new Video(['error' => 'File is smaller than min-filesize (2960699 bytes < 20971520 bytes). Aborting.', 'extractor' => 'youtube']),
];
}

/**
* @param non-empty-string $url
* @param list<non-empty-string> $metadataFiles
Expand Down

0 comments on commit 202c706

Please sign in to comment.