diff options
| author | ilotterytea <iltsu@alright.party> | 2025-10-01 22:19:15 +0500 |
|---|---|---|
| committer | ilotterytea <iltsu@alright.party> | 2025-10-01 22:19:15 +0500 |
| commit | 7022695c057d2d3cb526db92de2ffdf54ba8d10e (patch) | |
| tree | cbd251b9beec397607e5dda6895806ebe4fbefa3 | |
| parent | 21c125471b96176bbee0ea8cb8e3ef3b708eb7eb (diff) | |
feat: remove letterboxing from videos
| -rw-r--r-- | lib/file.php | 21 | ||||
| -rw-r--r-- | public/index.php | 8 | ||||
| -rw-r--r-- | public/upload.php | 14 |
3 files changed, 43 insertions, 0 deletions
diff --git a/lib/file.php b/lib/file.php index 0b250ca..ebfc009 100644 --- a/lib/file.php +++ b/lib/file.php @@ -129,4 +129,25 @@ function strip_exif(string $file_path) $file_path = escapeshellarg($file_path); $output = shell_exec("exiftool -q -EXIF= $file_path $file_path"); return empty($output); +} + +function remove_video_letterbox(string $input_path, string $output_path) +{ + $input_path = escapeshellarg($input_path); + $output_path = escapeshellarg($output_path); + $output = shell_exec("ffmpeg -nostdin -i $input_path -vf cropdetect=24:16:0 -f null - 2>&1"); + + if (preg_match_all('/crop=\d+:\d+:\d+:\d+/', $output, $matches)) { + $area = end($matches); + $area = end($area); + } else { + throw new RuntimeException('Could not detect crop parameters. Try upload the video without "Remove letterbox" option.'); + } + + $area = escapeshellarg($area); + + $output = []; + exec("ffmpeg -nostdin -i $input_path -vf $area -c:a copy $output_path 2>&1", $output, $code); + + return $code == 0; }
\ No newline at end of file diff --git a/public/index.php b/public/index.php index 735f49d..a55199c 100644 --- a/public/index.php +++ b/public/index.php @@ -446,6 +446,14 @@ $privacy_exists = is_file($_SERVER['DOCUMENT_ROOT'] . '/static/PRIVACY.txt'); <td><input type="checkbox" name="strip_exif_data" value="1" checked></td> </tr> <?php endif; ?> + <?php if (FILE_REMOVE_LETTERBOXES): ?> + <tr> + <th>Remove letterboxing<span class="help" + title="Removes black bars from the video, may be inaccurate, and only applies to videos">[?]</span>: + </th> + <td><input type="checkbox" name="remove_letterbox" value="1"></td> + </tr> + <?php endif; ?> </table> <button type="submit" class="fancy">Upload</button> </form> diff --git a/public/upload.php b/public/upload.php index 123c30a..bfa1990 100644 --- a/public/upload.php +++ b/public/upload.php @@ -200,6 +200,20 @@ try { throw new RuntimeException('This file is not allowed for upload.' . (isset($row['reason']) ? ' Reason: ' . $row['reason'] : '')); } + // remove letterbox + $remove_letterbox = FILE_REMOVE_LETTERBOXES && boolval($_POST['remove_letterbox'] ?? '0'); + if ($remove_letterbox && str_starts_with($file_data['mime'], 'video/')) { + $output_path = $file_path; + $input_path = FILE_UPLOAD_DIRECTORY . sprintf('/%s.temporary.%s', $file_id, $file_data['extension']); + rename($output_path, $input_path); + if (!remove_video_letterbox($input_path, $output_path)) { + rename($input_path, $output_path); + delete_file($file_id, $file_data['extension']); + throw new RuntimeException('Failed to remove letterbox from the video'); + } + unlink($input_path); + } + $file_data['size'] = filesize($file_path); if (FILE_THUMBNAILS && !is_dir(FILE_THUMBNAIL_DIRECTORY) && !mkdir(FILE_THUMBNAIL_DIRECTORY, 0777, true)) { |
