How to Moderate Video Content (Scan for NSFW Content) in PHP
On most professional websites, videos containing NSFW content generally need to be tagged as NSFW (or deleted outright) to protect viewers from a potentially offensive experience. Thankfully, it’s easy to implement an automatic content moderation feature using the ready-to-run PHP code examples provided below.
These examples will allow you to easily implement & take full advantage of our “Scan a Video for NSFW Content” API. This API uses advanced AI to scan each frame within a video upload and classify the degree to which their contents contain NSFW images. Each frame will receive a score from 0.0–1.0, with values closer to 0.0 indicating clean content, and values closer to 1.0 indicating racy/pornographic content was found. The total number of racy and NSFW frames will be returned (along with the total number of scanned frames), making it easy to act on each video depending on your website’s standards.
All in all, the response body is structured like so:
{
"Successful": true,
"HighestClassificationResult": "string",
"HighestScore": 0,
"TotalRacyFrames": 0,
"TotalNsfwFrames": 0,
"TotalFrames": 0,
"NsfwScannedFrames": [
{
"FrameNumber": 0,
"TimeStamp": "string",
"Content": "string",
"ClassificationResult": "string",
"Score": 0
}
]
}
To install the client SDK, we can run the following command:
composer require cloudmersive/cloudmersive_video_api_client
To structure our API call, we can copy & paste from the code examples below. In our request, we’ll need to provide our video file (or file URL if the file is larger than 2 GB) and specify how many frames per second should be scanned (minimum value is 0.05; 1 frame per 20 seconds). In addition, we’ll need to supply an API key to authenticate our request.
<?php
require_once(__DIR__ . '/vendor/autoload.php');
// Configure API key authorization: Apikey
$config = Swagger\Client\Configuration::getDefaultConfiguration()->setApiKey('Apikey', 'YOUR_API_KEY');
$apiInstance = new Swagger\Client\Api\VideoApi(
new GuzzleHttp\Client(),
$config
);
$input_file = "/path/to/inputfile"; // \SplFileObject | Input file to perform the operation on.
$file_url = "file_url_example"; // string | Optional; URL of a video file being scanned. Use this option for files larger than 2GB.
$frames_per_second = 8.14; // float | Optional; How many video frames per second to be scanned. Minimum value is 0.05 (1 frame per 20 seconds), maximum is 1. Default is 0.33 frame per second (1 frame scanned every 3 seconds). Maximum of 1000 total frames can be scanned, potentially adjusting the framerate for longer videos.
try {
$result = $apiInstance->videoScanForNsfw($input_file, $file_url, $frames_per_second);
print_r($result);
} catch (Exception $e) {
echo 'Exception when calling VideoApi->videoScanForNsfw: ', $e->getMessage(), PHP_EOL;
}
?>
To get an API key for free, you can register a free account on our website (this provides a limit of 800 API calls per month and no additional commitments).
After that, you’re ready to start scanning!