Jump to content

Php and Youtube Time Duration


Recommended Posts

Hello everyone. I hope you're having a great day!
I have a series of codes on my page that pic up different things when someone enters a youtube link in a form to send to the database. It works beautifully. I am now to where I need to Add to this to get a duration.
The code below successfully gets the title. There is a lot more code on the page but it's not significant to this issue.
What I need to do is get the duration and if it's more than 7 minutes long, then a message appears saying that the video is too long, try again.
I've been searching for a couple weeks now when I get the time and all I can find is stuff to use with an api key, etc. I don't have that and I really don't want to alter a very long page (over 1000 lines) to adjust to that.
So below is my code that gets the title. Is there a way to achieve the time duration limit from this as well?

Thank you so very much

The example below, if someone enters (aka this link is the $ytvideo1 value) https://www.youtube.com/watch?v=6ROwVrF0Ceg
it will output (entered in database function is later in the page not showing below) Chuck Berry - Johnny B. Goode (Live 1958)

$linkurl = "$ytvideo1";
parse_str( parse_url( $linkurl, PHP_URL_QUERY ), $vid );
preg_match('%(?:youtube(?:-nocookie)?\.com/(?:[^/]+/.+/|(?:shorts/)?|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})%i', $linkurl, $match);
$youtube_id = $match[1];

$preurl = "https://www.youtube.com/watch?v=$match[1]";

$ch = curl_init();


curl_setopt($ch, CURLOPT_URL, $preurl);
        
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$output = curl_exec($ch);

$document = htmlspecialchars($output);
curl_close($ch);     
        
$line = explode("\n", $document);
$judul = "";
foreach($line as $strline){

      preg_match('/\<title\>(.*?)\<\/title\>/s', $strline, $hasil);
      if (!isset($hasil[0]) || $hasil[0] == "") continue;
            $title =  str_replace(array("<title>", "</title>"), "", $hasil[0]);
}

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.