视频文件通过 YouTube API 上传时停留在 0%% [英] Video file is stuck at 0% percent when being uploaded via YouTube API

查看:88
本文介绍了视频文件通过 YouTube API 上传时停留在 0%%的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如标题所示,可以通过 YouTube 的上传功能上传和处理视频文件.但是,当我尝试以编程方式(通过 OAuth2 和 YouTube API v3)上传它时,它总是卡在 0% 的处理上.SO上有youtuber吗?是否有一些特殊的上传问题论坛?(PS,有一个类似的问题 没有结果.)

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:

无法启动可续传上传(HTTP 400:youtube.video,请求元数据指定了无效的视频标题.)

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

不幸的是,YouTube API v3 的错误页面并没有真正受到影响来自 logorhoea... 有谁知道错误是什么意思?

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;
    }

推荐答案

那么,未处理视频可能还有其他问题,但我的问题是要插入的视频标题太长.YouTube 有不超过 100 个字符的限制.如果您尝试插入更长的视频标题,则会引发上述异常.也许他们应该在 API 文档的某处注明这一点.

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.

这篇关于视频文件通过 YouTube API 上传时停留在 0%%的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆