Youtube PHP APi检查VIdeo是否重复

时间:2015-05-20 08:20:23

标签: php video youtube youtube-api google-api-php-client

您好我正在使用Google PHP API在PHP上传视频。我已经按照this tutorial来实现它,它运行正常。但是如果我的频道中已经存在视频,则会出现问题,它也会返回视频详细信息,但我发现它不会被重复拒绝。

所以我无法发现这是一个重复的视频,如果它是重复的那么主视频是什么。表示主视频的视频详细信息,将其作为重复进行比较。

请帮我看一下这是一个重复的视频吗?

2 个答案:

答案 0 :(得分:7)

我可以用来检查视频状态的唯一解决方案是使用表格中的字段将视频标记为已处理或未处理。然后设置一个cron,每小时运行一次(或者经常需要),以检查视频状态。

我的videos表格中的字段为processed。未处理时为NULL,如果已处理则为0。 我的api字段以json格式存储YouTube的视频ID。

这是我的cron脚本:

# Starts the YouTubeService.
$youtube_obj=new Google_Service_YouTube($client);

# Get new uploaded videos from the database.
$unprocessed_videos=$db->get_results('SELECT `id`, `file_name`, `contributor`, `api` FROM `'.DBPREFIX.'videos` WHERE `processed` IS NULL');

# If there are new videos...
if($unprocessed_videos>0)
{
    # Loop through the new videos
    foreach($unprocessed_videos as $new_video)
    {
        # Has the video been processed? Default is TRUE. will be changed to FALSE if the video still has "uploaded" status.
        $video_processed=TRUE;

        # Decode the `api` field.
        $api_decoded=json_decode($new_video->api);
        # Get the YouTube Video ID.
        $video_yt_id=$api_decoded->youtube_id;

        if(isset($new_video->file_name))
        {
            # Set the path to the video on the server.
            $video_path='videos'.DS.$new_video->file_name;
        }

        $to='uploaders email';
        $reply_to='whomever';
        $subject="Video status from ".DOMAIN_NAME;
        $body='';

        # Check the video status.
        $check_status=$youtube_obj->videos->listVideos('status', array('id' => $video_yt_id));

        # Did YouTube return results?
        if(!empty($check_status['items']))
        {
            # Loop through the videos from YouTube.
            foreach($check_status['items'] as $status)
            {
                if($status['status']['uploadStatus']=="uploaded")
                {
                    # The video has not been processed yet so do not send an email.
                    $video_processed=FALSE;
                }
                # Check to see if the YouTube upload was a success.
                elseif($status['status']['uploadStatus']=="processed")
                {
                    # Tell the user the video was uploaded.
                    $body.='Your video has been uploaded to YouTube and can be viewed at http://'.FULL_DOMAIN.'media/videos/?video='.$new_video->id;
                }
                # Check if the uploaded video status is rejected.
                elseif($status['status']['uploadStatus']=="rejected")
                {
                    if(isset($new_video->file_name))
                    {
                        # Get the Upload class.
                        require_once 'Form'.DS.'Upload.php');
                        # Instantiate an Upload object.
                        $upload_obj=new Upload($video_path);
                        # Delete video file from server.
                        $upload_obj->deleteFile($video_path);

                        # Delete rejected video from YouTube
                        $delete_response=$youtube_obj->videos->delete($video_yt_id);
                    }

                    # Need to delete the entry from the database as well.
                    $db->query('DELETE FROM `'.DBPREFIX.'videos` WHERE `id` = '.$db->quote($new_video->id).' LIMIT 1');

                    # Check if the rejection status was a duplicate.
                    if($status['status']['rejectionReason']=="duplicate")
                    {
                        # Tell the user the video was a duplicate.
                        $body.='Your video was rejected because it was a duplicate video';
                    }
                }
            }
        }
        else
        {
            $body.='Your video was not found on YouTube';
            $video_processed=TRUE;
        }
        # Update database if the video has been "processed".
        if($video_processed===TRUE)
        {
            # Get the Email class.
            require_once 'Email'.DS.'Email.php');
            # Instantiate a new Email object.
            $mail_obj=new Email();
            $mail_obj->sendEmail($subject, $to, $body, $reply_to);
            # Set video to processed.
            $db->query('UPDATE `'.DBPREFIX.'videos` SET `processed` = 0 WHERE `id` = '.$db->quote($new_video->id).' LIMIT 1');
        }
    }
}

答案 1 :(得分:5)

如果没有能力运行cron作业,您可能必须在下次尝试访问视频时检查它是否重复。

或者,在上传视频后,您可以启动另一个php脚本,while循环定期检查状态(在进行另一个API请求检查之前暂停一段时间。)

do
{
    $videos = $youtubeClient->videos->listVideos('status', ['id' => $videoId]);
    $video = $videos['items'][0]; // assuming you want one video. Iterate over the items array otherwise

    if($video['status']['uploadStatus'] == 'rejected' && $video['status']['rejectionReason'] == 'duplicate') {
        // Notify user of duplicate and/or note failed upload in DB
    }
    else {
        sleep(180);
    }
} while ($video['status']['uploadStatus'] == 'processing')

您需要确保可以从命令行运行脚本,并且可以接受参数以从原始上载脚本中获取视频ID。 http://php.net/manual/en/function.getopt.php

就我个人而言,我不喜欢使用这样的循环启动其他脚本的想法,因为如果不检查所有边缘情况,最终可以使用rouge进程,但如果没有cron作业,则选项有限。