Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Video file is stuck at 0% percent when being uploaded via YouTube API

As the title reads, a video file can be uploaded and processed via YouTube's upload function. However, when I try to upload it programmatically (via OAuth2 and YouTube API v3), it gets always stuck at 0% percent processing. Are there any youtubers on SO? Is there some special forum for upload issues? (PS, there is a similar question with no results.)

UPDATED ERROR: Digging deeper, it seems to be related to the video metadata. I do get the following error occasionally:

Failed to start the resumable upload (HTTP 400: youtube.video, The request metadata specifies an invalid video title.)

Unfortunately, the error page for YouTube's API v3 does not really suffer from logorrhoea... Does anyone know what the error means?

UPDATED CODE: At the moment the files are uploaded chunk by chunk (which usually works pretty well but not all the times):

    function uploadFile($dbfile) {
        $client = $this->client;
        $youtube = new Google_Service_YouTube($client);
        $htmlBody = "";
        try {

            // Create a snippet with title, description, tags and category ID
            // Create an asset resource and set its snippet metadata and type.
            // This example sets the video's title, description, keyword tags, and
            // video category.
            $snippet = new Google_Service_YouTube_VideoSnippet();
            $snippet->setTitle($dbfile->displayname);
            // Numeric video category. See
            // https://developers.google.com/youtube/v3/docs/videoCategories/list 
            $snippet->setCategoryId("22");

            // Set the video's status to "private"
            $status = new Google_Service_YouTube_VideoStatus();
            $status->privacyStatus = "private";

            // Associate the snippet and status objects with a new video resource.
            $video = new Google_Service_YouTube_Video();
            $video->setSnippet($snippet);
            $video->setStatus($status);

            $chunkSizeBytes = 1 * 1024 * 1024;
            $client->setDefer(true);

            $insertRequest = $youtube->videos->insert("status,snippet", $video);

            // Create a MediaFileUpload object for resumable uploads.
            $media = new Google_Http_MediaFileUpload(
                $client,
                $insertRequest,
                'video/*',
                null,
                true,
                $chunkSizeBytes
            );
            $media->setFileSize(filesize($dbfile->localfile));

            // Read the media file and upload it chunk by chunk.
            $status = false;
            $handle = fopen($dbfile->localfile, "rb");
            while (!$status && !feof($handle)) {
              $chunk = fread($handle, $chunkSizeBytes);
              $status = $media->nextChunk($chunk);
            }

            fclose($handle);
            $client->setDefer(false);

            $log = array("success" => true, "snippet_id" => $status["id"]);
        } catch (Google_ServiceException $e) {
            $log = array("success" => false, "errormsg" => $e->getMessage());
        } catch (Google_Exception $e) {
            $log = array("success" => false, "errormsg" => $e->getMessage());
        }
        return $log;
    }
like image 900
Jan Avatar asked Sep 29 '15 19:09

Jan


1 Answers

Well then, there might be other issues with videos not being processed but mine was that the title of the video to be inserted was simply too long. YouTube has a limitation of no more than 100 characters. If you try to insert longer video titles, it throws the above exception. Perhaps they should note this somewhere in their API documentation.

like image 191
Jan Avatar answered Sep 22 '22 13:09

Jan