TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 02-16-2008, 12:51 AM   #1 (permalink)
The Frequenter
Prolific Welcomer Upcoming Programmer 
 
Join Date: Sep 2007
Posts: 360
Thanks: 24
Haris is on a distinguished road
Default Youtube Videos

Is there possibly a way to download youtube videos via PHP?
__________________
Necessity is the mother of invention.

My blog
Haris is offline  
Reply With Quote
Old 02-16-2008, 03:49 AM   #2 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Asterix

Of course ! I wrote a script once that did this. In fact, I decided to polish up that script seeing as how I haven't contributed much of anything to TalkPHP for a week or 2! Sorry guys. Hopefully this makes it all better.

The example is in the file, but here it is anyway to show you how easy I made it for you all:

php Code:
$pYouTube   -> setAddress('http://www.youtube.com/watch?v=ggShSZUBu5s')
            -> getVideo()
            -> asDownload();

It even gets the proper file name and appends FLV so it's not downloading get_video, as most do. Feel free to offer any suggestions/improvements!
Attached Files
File Type: php TalkPHP_YouTube.class.php (2.2 KB, 196 views)
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
The Following 3 Users Say Thank You to Wildhoney For This Useful Post:
Andrew (02-16-2008), Haris (02-16-2008), owenj2o (02-16-2008)
Old 02-16-2008, 07:51 PM   #3 (permalink)
The Wanderer
 
Join Date: Jan 2008
Posts: 14
Thanks: 2
owenj2o is on a distinguished road
Default

Would you have any concerns if I used that class as a library in a framework I'm developing?
owenj2o is offline  
Reply With Quote
Old 02-16-2008, 09:03 PM   #4 (permalink)
The Frequenter
 
ReSpawN's Avatar
 
Join Date: Nov 2007
Location: Netherlands
Posts: 460
Thanks: 49
ReSpawN is on a distinguished road
Default

Holy crap Adam, very impressive work on my behalf. Is this also easy to recode to PHP4 OOP?

Welcome back, so to speak, by the way.
__________________
"Life is a bitch, take that bitch on a ride"
Send a message via MSN to ReSpawN
ReSpawN is offline  
Reply With Quote
Old 02-17-2008, 01:16 AM   #5 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

Thank you ! You could port it over to PHP 4, although such functions as get_header are not available in PHP 4, though there are ways around that by using cURL.

Owen: Help yourself! Give TalkPHP a little mention though when you can please!

I modified the setAddress function earlier. Merely to ensure it can always extract the ID where available. Whereas the previous version, if there's any data trailing the YouTube ID, then it wouldn't work correctly.

php Code:
public function setAddress($szIdentifier)
{
    if(!preg_match('~^[a-z]+$~', $szIdentifier))
    {
        preg_match('~.+?v=(?P<identifier>[^&]+).*~i', $szIdentifier, $aMatches);
        $szIdentifier = $aMatches['identifier'];
    }
           
    $this->m_szIdentifier = $szIdentifier;
           
    return true;
}
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 02-17-2008, 02:34 AM   #6 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

Hmm, this script downloads a blank flv for me, correct name and all.
__________________

Village Idiot is offline  
Reply With Quote
Old 02-17-2008, 03:35 AM   #7 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

For a moment there you had me going, as I never actually tested a downloaded FLV. But I'm glad to say it does actually play for me. Albeit in VLC. Windows Media Player never takes kindly to being given an FLV.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 02-17-2008, 04:34 AM   #8 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

Hmm, I use flv player where I have used many youtube downloaded videos and it doesnt work. The file is 0 bytes.
__________________

Village Idiot is offline  
Reply With Quote
Old 02-17-2008, 04:10 PM   #9 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

That's rather peculiar. Does this happen with anybody else?
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 02-17-2008, 04:45 PM   #10 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

VI, give us some test cases which don't work for you and we can see if they also don't work for us. :)
Salathe is offline  
Reply With Quote
Old 02-17-2008, 09:35 PM   #11 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

Theres nothing to give, I just upload the file as is and it gives me a blank file. It does the same with any address I put in there.

Anyway, here is a modification I made that truncates the name after 10 letters (you can set it differently if you want to).

PHP Code:
        public function asDownload()
        {
            
$szDownloadURL sprintf
            
(
                
self::URL_DOWNLOAD,
                
$this->m_aVideoData['video_id'],
                
$this->m_aVideoData['l_id'],
                
$this->m_aVideoData['t_id']
            );

            
$aHeaders get_headers($szDownloadURL1);
            
            if(
array_key_exists('Content-Length'$aHeaders))
            {
                
$iRemoteSize $aHeaders['Content-Length'];
            }
            
            
header('HTTP/1.1 200 OK');
            
header('Content-Type: video/flv');
            
            if(isset(
$iRemoteSize))
            {
                
header('Content-Length: ' $iRemoteSize);
            }
            
            if(
strlen($this->m_aVideoData['filename']) > 10)
            {
                
$DownloadFileName substr($this->m_aVideoData['filename'],0,10) . '...flv';
            }
            else
            {
                
$DownloadFileName $this->m_aVideoData['filename'];
            }
            
            
header('Content-Disposition: attachment; filename="' $DownloadFileName '"');
            
header('Content-Transfer-Encoding: Binary');
            
            
readfile($szDownloadURL);
        } 
__________________

Village Idiot is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 06:40 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design