Tutorials · 7 min read

cables lightening

Helpful PHP scripts Pt 1: Upload videos from Google Drive to API video

If you love PHP and need to do a bulk video transfer to api.video using Google Drive, this script will make it easy.

Erikka Innes

January 28, 2021

If you need to upload videos in bulk from Google Drive to api.video, we have a helpful PHP script from api.video Project Manager Hervé Pita!

This script is part of a collection of scripts that help with bulk video uploads. All three scripts are available in our Helpful PHP Scripts repository on Github. In each part of this series, we'll walk through how to set up the script. The scripts you'll find include:

The script we'll focus on today is 'Ingest Videos from Google Drive.' We'll use the video endpoints. You can check out the API reference documentation for the video endpoints here: Video endpoints

Let's get started!

NOTE: Discussion is Mac focused. If you have a PC and need help with the steps, contact us in our community forum.

Ingest videos from Google Drive

If you have videos on Google Drive that you need to transfer, our Ingest_google_drive.php script may be perfect. The script will ask you for your api.video and Google Drive credentials, then move all the videos from a Google Drive folder you specify into api.video.

Prerequisites for ingest videos from Google Drive

For this project, you'll need to ensure that you have:

  • PHP 5.4 or greater with the command-line interface (CLI) and JSON extension installed
  • The Composer dependency management tool (or another way to manage your dependency management). See the Composer website for installation options.
  • A Google account with Google Drive enabled (we'll help you do this in the walkthrough) - for now make sure you are logged into the Google account you want to transfer files from
  • A GitHub account
  • An api.video account
  • Folder with video files you would like to transfer

Enable Google Drive API

To enable the Google Drive API and install the script:

  1. Make sure you are logged in to your Google account.
  2. Navigate to developers.google.com/drive/api/v3/quickstart/php.
google drive api, api.video, upload videos from google drive
  1. Click Enable the Drive API. This will launch a page for starting your own developer project.
Enable the drive api
  1. By default, the name Quickstart is suggested. This is fine for a project title for this walkthrough. If you want to change it to something else, go ahead.
  2. Agree to the Terms of Service by choosing the Yes radio button.
  3. Click Next.
Configure client
  1. On the Configure your OAuth client screen, from the options choose Desktop app.
  2. Click Create.
screenshot, start developing end of process
  1. On the You're all set! screen click Download Client Configuration. This will be a file titled credentials.json.

Installation

  1. Navigate to api.video's helpful-scripts repository on GitHub.
  2. Click Code and choose Download ZIP. (You can also download using the CLI if you prefer.)
  3. Open the ZIP file and put the folder somewhere on your computer where you can access it easily.
  4. Add your credentials.json file to your folder.
  5. Open a terminal. (You can click the search icon in the upper right of your screen, type Terminal and pull it up that way.)
  6. In the terminal window you opened, navigate to the helpful-scripts folder.
  7. Type:
composer require api-video/php-sdk
composer require google/apiclient:^2.0

NOTE: If you choose to do a local install of composer to your project directory, your commands will instead be:

php composer.phar require api-video/php-sdk
php composer.phar require google/apiclient:^2.0

Try the script

  1. Type:
php Ingest_google_drive.php

NOTE: PHP is case sensitive, so type the file name exactly the same.

  1. In the terminal, you are prompted with 'Choose now your environment ('prod' or 'sandbox'):'. Choose what is best. If you pick sandbox, every video is watermarked so you can't use it besides trying out this script. For this walkthrough, we will choose prod.
  2. You're prompted for your production API key. If you haven't grabbed your key already, go to your dashboard and get the production key.
  3. You're prompted asking whether you want to add a playerId. This will associate a specific player for the videos you import. You can always add one later (here's a tutorial about how to customise a player), so for the sake of simplicity we will choose n.
  4. Next you are asked if you have your credentials.json file. If you followed the instructions so far you are ready. Type yes.
  5. You will receive a long URL in the terminal. Take this entire URL and paste it into your browser. If all goes well, you will get Google's 'Sign in with Google' screen along with a note asking you to choose an account to continue to your project (which we titled Quickstart earlier). NOTE: Make sure you leave the terminal open! We are coming back to it soon and it needs to be in the same place.
  6. Choose the account you want to use Google Drive for. (It should be the same account that you have been using or it will not work.)
  7. You will receive a warning that the app is requesting access to sensitive information in your Google Account. Click Advanced.
  8. You will see a note saying 'Continue only if you understand the risks and trust the developer. The email listed will be for your Google account. Click Quickstart (unsafe).
  9. You'll get a note asking you to grant your application Quickstart some permissions. These permissions include seeing your Google Drive files, downloading your Google Drive files and seeing the names and emails of people you share files with. Click Allow.
  10. Next, you are asked to confirm your choices. You are again warned about trusting your application Quickstart. Click Allow.
  11. You receive sign-in credentials. Follow the instructions asking you to please copy the code presented. The simplest way to copy it is to press the copy icon to the right of the code.
  12. In the terminal, you'll see a prompt reading 'Enter verification code.' Paste the code you just copied in here.
  13. You are prompted for the name of a Google Drive folder to import videos from. Enter the name of the folder you want to use exactly as it appears. For this walkthrough, we are using a folder called videos.
  14. The terminal will show you the name of each file as it's being copied, and the ID for referencing that file. When it's done copying the files over, you'll get a note saying All Done, and the script ends.
  15. Navigate to my.api.video, log in if necessary, and check the Production area in the My videos section. You'll see your new videos posted!

Revoke application access

If you are done trying out the application, and you don't want to leave access, do the following:

  1. Navigate to myaccount.google.com/security.
  2. Scroll down until you see 'Third-party apps with account access.'
  3. Click Manage third-party access. It opens a page titled Apps with access to your account.
  4. Click the Quickstart application (or whatever you named your project). It will expand to show details about what kind of access you allowed.
  5. Click Remove Access.

Done! Stay tuned for more helpful PHP scripts!

FAQs

  1. How do I check what version of PHP I have? Open a terminal window. Type php -v.
  2. What can I do if I don't want to use composer? We don't offer support for setting up without composer at this time. If you set up this script without using composer, please share your work with us in our community forum. We'll add your method to our documentation for future users.

Try out more than 80 features for free

Access all the features for as long as you need.
No commitment or credit card required

Video API, simplified

Fully customizable API to manage everything video. From encoding to delivery, in minutes.

Built for Speed

The fastest video encoding platform. Serve your users globally with 140+ points of presence. 

Let end-users upload videos

Finally, an API that allows your end-users to upload videos and start live streams in a few clicks.

Affordable

Volume discounts and usage-based pricing to ensure you don’t exceed your budget.