Determine the duration of a Flash video in PHP

I need a function in PHP to determine the duration of a video at xvideos dot com [NSFW], for example:

$video = "http://flashservice.xvideos dot com/embedframe/1606356";
function duration ($video);
echo duration;

This returns 11:49.

Please note: The site is not appropriate for work. It's a pornographic site. Please visit with caution if you are at work.

Upvotes: 0

Views: 3536

Answers (2)

user849137
user849137

Reputation:

Well, you only have a link to the embedded player, not the actual hot link to the video (FLV) file, so I'm assuming you don't know how to get that.

Here's how:

Your link has a number at the end, which is 1606356. That number is the video ID, so it represents the video you want to play with. Now we have the video ID, we need to load the original page, where users would visit when attempting to watch the video from the original site (not through an embedded player). The original page link is: http://www.xvideos.com/video1606356. See the same video ID at the end of that link, too?

The reason for the need to load the original page is actually because it contains some flash vars, where, one of which lies the FLV hot link to the specific video. So we need to strip that hotlink from the HTML from the original page.

Let's code!

//load the original page
$originalPage = file_get_contents( 'http://www.xvideos dot com/video1606356' );

//strip the FLV link
if ( !preg_match( '|flv_url=(.*)&url_bigthumb|', $originalPage , $match ) )
{

    echo "Sorry, I could not find the hot link.";
    //bye
    exit(); 


}

$hotLink = $match[1];

//decode the hotlink (because it's URL encoded)
$hotLink = urldecode( $hotLink );

So we now have our hot link! Next, we need to download the flv file and store it locally, so we can use fopen() on it. This will slow things down, but it will also mean we don't have to open a security hole on our server.

//download the flv and store it in a folder called 'videos'
file_put_contents( 'videos/myVideoFileName.flv' , file_get_contents( $hotLink ) );

So we now have a downloaded copy of the flv video. We now need to use the method that I stole from here, to get the length:

$flv = fopen("videos/myVideoFileName.flv", "rb");
fseek($flv, -4, SEEK_END);
$arr = unpack('N', fread($flv, 4));
$last_tag_offset = $arr[1];
fseek($flv, -($last_tag_offset + 4), SEEK_END);
fseek($flv, 4, SEEK_CUR);
$t0 = fread($flv, 3);
$t1 = fread($flv, 1);
$arr = unpack('N', $t1 . $t0);
$milliseconds_duration = $arr[1];

And there you have it.

Lastly, what you're doing here is something that I don't think XVIDEOS will be okay with (no body likes hot link thieves). I'm not responsible for anything you do, using any of the code/content above.

Upvotes: 5

user1490674
user1490674

Reputation:

You need to first get the hot link of the video and then take a look here:

calculate flv video file length ? using pure php

Upvotes: 0

Related Questions