我编写了一个脚本,可以通过API将多个视频上传到YouTube。
问题在于,当我运行脚本时,它会上传第一个视频,但在第二次迭代中,它没有正确上传视频并引发异常:
PHP Fatal error: Uncaught exception 'Google_Service_Exception' with
message 'Error calling PUT
https://www.googleapis.com/upload/youtube/v3/videos?part=status%2Csnippet&uploadType=resumable&upload_id=AEnB2Uov0hJdmXu0UKN65WejqwsCvdhkXuIxgftiNPfNQYRv2MLwWaqSDHBlq5rUmn7xLwAl7ZXdbK8YcFprx_6uao8of119Fw:
(400) Failed to parse Content-Range header.' in
/var/www/html/YoutubeApi/Google/Http/REST.php:80 Stack trace:
#0 /var/www/html/YoutubeApi/Google/Http/MediaFileUpload.php(185): Google_Http_REST::decodeHttpResponse(Object(Google_Http_Request))
#1 /var/www/html/YoutubeApi/upload_video.php(79): Google_Http_MediaFileUpload->nextChunk('E(?03?F????N???...')
#2 /var/www/html/YoutubeApi/upload_video.php(272): UploadVideo->upload_video('Ireland Video W...', 'Welcome to Tour...',
'/var/www/html/Y...', Array)
#3 /var/www/html/YoutubeApi/upload_video.php(28): UploadVideo->run()
#4 /var/www/html/YoutubeApi/upload_video.php(308): UploadVideo->__construct()
#5 {main} thrown in /var/www/html/YoutubeApi/Google/Http/REST.php on line 80
这是我的剧本:
private function run() {
$dir = "";
$videos = $arr = $this->get_active_videos();
$directory = __DIR__ . "/temp/";
if (!file_exists($directory)) {
$dir = mkdir(__DIR__ . "/temp", 0777);
}
$this->youtube_client->setAccessToken($this->get_access_token(1));
if ($this->youtube_client->getAccessToken()) {
foreach ($videos as $video) {
$this->set_youtube_video_obj();
//$channels = explode(",", $video['channel']);
if (!strlen($video['category']) && !strlen($video['channel'])) {
continue;
}
file_put_contents("temp/video", file_get_contents($video['media_link']));
sleep(5);
$tags = $this->get_video_tags($video['location'], $video['event'], $video['country'], $video['video_cat']);
$status = $this->upload_video($video['title'], $video['description'], $directory . "/video", $tags);
if ($status['status']['uploadStatus'] == 'uploaded') {
if (strlen($video['category'])) {
$categories = explode(",", $video['category']);
// $video_files = $this->get_files();
foreach ($categories as $category) {
$playlists = $this->get_playlists(1);
//foreach ($playlists as $playlist) {
$cat_name = $this->get_category_name($category);
$this->add_video_to_playlist($status, $video['title'], $video['description'], $cat_name);
sleep(3);
}
} else if (strlen($video['channel'])) {
$tags = $this->get_video_tags($video['location'], $video['event'], $video['country'], $video['video_cat']);
$status = $this->upload_video($video['title'], $video['description'], $directory . "/video", $tags);
$channels = explode(",", $video['channel']);
//$video_files = $this->get_files();
//foreach ($video_files as $video_file) {
foreach ($channels as $channel) {
if ($status['status']['uploadStatus'] == 'uploaded') {
$channel_name = $this->get_channel_name($channel);
$category_name = $this->get_playlist_from_channel($channel_name);
$this->add_video_to_playlist($status, $video['title'], $video['description'], $category_name);
}
}
//}
}
}
$this->unset_youtube_video_obj();
}
}
}
}
我尝试过很多东西,但无济于事,感谢任何帮助。
答案 0 :(得分:0)
我得到了同样的错误,我发现视频文件夹是空的 所以我认为你的问题与你未能正确设置视频路径有关