PHP Client Library: Uploading videos now 99% more efficient !
March 2nd, 2009 | Published in Youtube API
Posted by Jochen Hartmann, YouTube APIs and Tools Team
If you are using the PHP Client Library to upload your videos to YouTube.com you may have had to deal with memory issues, such as the common Fatal error: Allowed memory size of ... bytes exhausted error message.
I am happy to announce that as of version 1.7.6 of the client library, this should no longer be a problem. Jeff Fisher and I have added support for streaming large video files to our API in manageable 1 MB chunks. The change is completely transparent, so you won't need to do anything besides upgrade your copy of the client library.
Prior to this change, our client library used to rely solely on the Zend_Http_Client object to handle HTTP communication between servers. The client makes requests by reading the entire body of your HTTP POST message into a string, which then gets sent to our API server. This behavior is perfectly acceptable for normal use since most of the time we are just sending XML strings or small media files such as images, but doesn't work quite so well for uploading 1 GB video files.
To address this problem, I designed a Zend_Gdata_MediaMimeStream object which only stores a handle to the video file being uploaded. Jeff built a Zend_Gdata_HttpAdapterStreamingSocket which then reads from the media stream in 1 MB chunks and sends to the socket until the entire message is read. We have tested this code extensively and are always open to feedback on how to improve performance issues in our client library, so check out the source code if you are interested.
While I have your attention, let me also share another trick for those obsessed with efficiency: If you are only interested in working with the raw XML instead of the complete Zend_Gdata object model, you can flip a minor switch that is available in all of our service classes (Zend_Gdata_YouTube, Zend_Gdata_Docs, etc.):
$yt = new Zend_Gdata_YouTube(); $yt->useObjectMapping(false); $xmlString = $yt->getRecentlyFeaturedVideoFeed(); echo gettype($xmlString); # will return 'string'
As you can see in the snippet above, the $xmlString variable is now just a regular string instead of a Zend_Gdata_VideoFeed object. My testing shows that this can make fetching video feeds from YouTube faster by up to 35 times. Of course you would need to add a little bit of time parsing the XML with the tool of your choice (XPath, etc.). I should also add that those interested in parsing XML without the aid of the client library may want to check out the Backward Compatibility Guidelines for the YouTube Data API.
If you are using the PHP Client Library to upload your videos to YouTube.com you may have had to deal with memory issues, such as the common Fatal error: Allowed memory size of ... bytes exhausted error message.
I am happy to announce that as of version 1.7.6 of the client library, this should no longer be a problem. Jeff Fisher and I have added support for streaming large video files to our API in manageable 1 MB chunks. The change is completely transparent, so you won't need to do anything besides upgrade your copy of the client library.
Prior to this change, our client library used to rely solely on the Zend_Http_Client object to handle HTTP communication between servers. The client makes requests by reading the entire body of your HTTP POST message into a string, which then gets sent to our API server. This behavior is perfectly acceptable for normal use since most of the time we are just sending XML strings or small media files such as images, but doesn't work quite so well for uploading 1 GB video files.
To address this problem, I designed a Zend_Gdata_MediaMimeStream object which only stores a handle to the video file being uploaded. Jeff built a Zend_Gdata_HttpAdapterStreamingSocket which then reads from the media stream in 1 MB chunks and sends to the socket until the entire message is read. We have tested this code extensively and are always open to feedback on how to improve performance issues in our client library, so check out the source code if you are interested.
While I have your attention, let me also share another trick for those obsessed with efficiency: If you are only interested in working with the raw XML instead of the complete Zend_Gdata object model, you can flip a minor switch that is available in all of our service classes (Zend_Gdata_YouTube, Zend_Gdata_Docs, etc.):
$yt = new Zend_Gdata_YouTube(); $yt->useObjectMapping(false); $xmlString = $yt->getRecentlyFeaturedVideoFeed(); echo gettype($xmlString); # will return 'string'
As you can see in the snippet above, the $xmlString variable is now just a regular string instead of a Zend_Gdata_VideoFeed object. My testing shows that this can make fetching video feeds from YouTube faster by up to 35 times. Of course you would need to add a little bit of time parsing the XML with the tool of your choice (XPath, etc.). I should also add that those interested in parsing XML without the aid of the client library may want to check out the Backward Compatibility Guidelines for the YouTube Data API.