PHP library for searching video by screenshot

Video Color Project

Description

We all face the challenge of finding information every day. It is required to find text, images, audio or video information. Most often, text is used for the search query. Less often – images. There are services like “Shazam” that search for music using sound recording. We focused on creating a search service that searches for videos. We use images as parameters for the request.

Website

The site is located at www.videocolor.aapsoftware.ru

List of articles

Video list

Search Library

The Video Color Search Client library was written in PHP to search for video by screenshot for third-party websites.

There are two options for obtaining this library:

GitHub

To clone a project from GitHub, open a terminal window, navigate to your working directory, and run the following command:

git clone https://github.com/alekseev23/VideoColorSearchClientPHP.git

Packagist.org

To install a package using Composer enter the following command in the console:

php composer.phar require aap_software/video_color_search_client

After its execution in the directory vendor a subdirectory will appear aap_software.

Using the Library

Test script

  • Open a terminal window

  • Change to the project directory where the file is sample.php

  • Run the following command

php sample.php test.jpg

As a result of the script, text information about the movie will appear. Try using a different screenshot. Where to get it? The internet is full of movie stills.

Example code

Let’s have our test script example.php and let’s take a look at his work.

<?php
declare(strict_types=1);
require("vendor/autoload.php");
$img = imagecreatefromjpeg("test.jpg");
$search = new AapSoftwareVideoColorSearchClient();
$obj = $search->get($img);
imagedestroy($img);
if ($obj === null) {
    echo "Server not connected!n";
} elseif (!$obj->result) {
    echo "Not foundn";
} else {
    var_dump($obj);
}

Save, copy the image to the directory test.jpg and run it:

php example.php

As a result of the execution, we will see the result of the search query.

Detailed analysis of the code

Enable strict typing.

<?php
declare(strict_types=1);

Connecting our library

require("vendor/autoload.php");

Create an image in memory JPEG file test.jpg

$img = imagecreatefromjpeg("test.jpg");

Create an instance of the class SearchClient.

$search = new AapSoftwareVideoColorSearchClient();

We perform a search query by simply passing our image.

$obj = $search->get($img);

We delete the image that has become unnecessary.

imagedestroy($img);

We analyze the result and if there are no errors, then we display the information received.

if ($obj === null) {
    echo "Server not connected!n";
} elseif (!$obj->result) {
    echo "Not foundn";
} else {
    var_dump($obj);
}

Remark 1

If you do not use class autoloading, then you can write differently.

require("src/SearchClient.php");

Remark 2

If you need to get information about the video not in English (works by default) but in Russian, then the search query should be slightly changed and a second parameter added.

$obj = $search->get($img,"ru");

Similar Posts

Leave a Reply