How can it be determined if a youtube video is an actual video or just a static image?
Since it's likely not possible using the youtube API, is there a workaround using javascript/jquery to scan pixels in a given area of the window and determine if they've changed?
This rough idea is based on the answer of Stichoza, but much simpler.
You can get video thumbnails http://i.ytimg.com/vi/VIDEO_ID/X.jpg. For example, if Video ID is 500Pm4mQZQQ (static image video), you will have this thumbnails:
http://i.ytimg.com/vi/500Pm4mQZQQ/1.jpg http://i.ytimg.com/vi/500Pm4mQZQQ/2.jpg http://i.ytimg.com/vi/500Pm4mQZQQ/3.jpg
If these images are very similar, the video is static (they won't be exactly identical because of compression noise). In order to compare the three available still images in a simple way, you don't need to apply actual image comparison algorithms. Just compare their file sizes.
These are JPEG images. Their file size varies, depending on how well the image can be compressed. Similar images will result in similar file sizes.
The above examples have 3534, 3539, and 3539 bytes. Checking some random non-static video, I get much bigger differences: 4179, 4726, and 4779 bytes. Very similar file sizes = static video.
Getting the byte size of an image is not (easily) possible with Javascript. But it should be trivial with any server-side technique. Here's an easy way with PHP:
$head = array_change_key_case(get_headers("http://example.com/file.ext", TRUE));
$filesize = $head['content-length'];
Edit:
<?php
$urls = array(
// Actual videos
'https://www.youtube.com/watch?v=1iTg20x7w2s',
'https://www.youtube.com/watch?v=uY6ooLaM3_U',
'https://www.youtube.com/watch?v=E0vNU6pEQLU',
// Static videos
'https://www.youtube.com/watch?v=wWiC_I7R2iI',
'https://www.youtube.com/watch?v=ytbpMht-7OA',
'https://www.youtube.com/watch?v=x_38wF6bYCw'
);
foreach($urls as $url) {
echo $url . ":\n";
echo thumbSizeStandardDeviation($url) . "\n\n";
}
/**
* This is the main function
*/
function thumbSizeStandardDeviation($url) {
$videoId = extractVideoId($url);
for($i = 1; $i <= 3; $i++) {
$thumbnailUrl =
"http://i.ytimg.com/vi/" . $videoId . "/" . $i . ".jpg";
$fileSizes[] = getRemoteFileSize($thumbnailUrl);
}
return standardDeviation($fileSizes);
}
/**
* https://stackoverflow.com/a/3393008/376138
*/
function extractVideoId($url) {
parse_str( parse_url( $url, PHP_URL_QUERY ), $queryParams );
return $queryParams['v'];
}
/**
* https://stackoverflow.com/a/12249536/376138
*/
function getRemoteFileSize($url) {
$headers = array_change_key_case(get_headers($url, TRUE));
return $headers['content-length'];
}
/**
* https://en.wikipedia.org/wiki/Standard_deviation#Basic_examples
*/
function standardDeviation($numbers) {
$mean = array_sum($numbers) / count($numbers);
$differenceSum = 0;
foreach($numbers as $number) {
$differenceSum += pow($number - $mean, 2);
}
return sqrt($differenceSum / count($numbers));
}
I've used three "normal" videos and three completely static videos, their URLs are in the code. Running the script at the command line, I get:
$ php youtube-is-static-video.php
https://www.youtube.com/watch?v=1iTg20x7w2s:
271.21496189472
https://www.youtube.com/watch?v=uY6ooLaM3_U:
28.335294049805
https://www.youtube.com/watch?v=E0vNU6pEQLU:
182.70620010157
https://www.youtube.com/watch?v=wWiC_I7R2iI:
4.1899350299922
https://www.youtube.com/watch?v=ytbpMht-7OA:
7.5424723326565
https://www.youtube.com/watch?v=x_38wF6bYCw:
5.1854497287013
In this (admittedly small) sample it is indeed possible to tell the normal (first three) from the static ones (last three).
A big problem will be videos made of multiple still images (slideshow), which is quite common for music uploads.
There is no official way to determine if video is static image on not, but you can still do some tricks.
You can get video thumbnails http://i.ytimg.com/vi/VIDEO_ID/X.jpg
. For example, if Video ID is 500Pm4mQZQQ (static image video), you will have this thumbnails:
Now you can use Image Similarity Detection libraries to determine thumbnails' similarity. For example, you can use this js-image-similarity JavaScript algorithm: https://github.com/bitlyfied/js-image-similarity
Note: This JS library is only supposed to work in latest WebKit. Supports for Canvas and Array.forEach is required.
You can also use PHP. Read more about algorithm here and also check this PHP Class: http://www.phpclasses.org/package/6478-PHP-Compare-two-images-to-find-the-differences.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With