25

Hi I am using Google PHP API to upload video in PHP. I have followed this tutorial to implement this and it is working fine. But there is a problem if video is already exist in my channel it is also returning the video details and I can't find it is a rejected for duplicate.

So I cant find this is a duplicate video or not and if it is duplicate then what is the main video. Means the video details of main video from which it is compared as duplicate.

Please help me to find this is a duplicate video or not?

Bik
  • 553
  • 10
  • 31
  • Is my question not clear? – Bik May 20 '15 at 09:32
  • @bik, you want to check the duplicate video against your db, or your youtube channel? elaborate a little more to understand what is your workflow – Emilio Gort Jun 01 '15 at 18:35
  • @EmilioGort thanks for your reply. I want to check the duplicate video in youtube channel. Actually when i am uploading the video through the api it is returning uploaded. But after processing of video youtube is declraing this is a duplicate video. – Bik Jun 02 '15 at 05:41

2 Answers2

6

The only solution I could come up with to check the video status is to use a field in the table to mark the video as processed or not. Then set a cron to run every hour (or however often you want) to check the video status.

The field in my videos table is processed. NULL for not processed, 0 if it's processed. My api field stores the YouTube's video ID in json format.

Here's my cron script:

# 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');
        }
    }
}
Draven
  • 1,467
  • 2
  • 19
  • 47
  • Thanks for reply. But it is not working....it is showing error for "listVideos"... – Bik May 20 '15 at 14:29
  • @Bik I guess I should have mentioned that I use wrapper methods for the YouTube library in my own YouTube class. I have edited my answer to use the original methods. – Draven May 20 '15 at 14:37
  • @Draven your answer is not working for me, because in my case i can't do this using cron.............. Pls help – Learner May 24 '15 at 07:29
  • @Learner This is the only solution I've been able to come up with. If you can't use cron, I can't help you. – Draven May 24 '15 at 12:47
  • 1
    @Web_Developer Sorry, but It is you that is not understanding his question. – Draven Aug 22 '15 at 15:03
4

Without the ability to run a cron job, you'll probably have to check if it's a duplicate the next time you try to access the video.

Alternatively, after the video is uploaded you could launch another php script that a while loop checking the status periodically (sleeping for a time before making another API request to check.)

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')

You'll need to make sure that script can be run from the command line and can accept arguments to get the video ID from the original upload script. http://php.net/manual/en/function.getopt.php

Personally I don't like the idea of launching additional scripts with loops like this as you can end up with rouge processes if you don't check all your edge cases, but without cron jobs your options are limited.

jfadich
  • 6,110
  • 2
  • 20
  • 31