Helpful PHP scripts Pt 1: Upload videos from Google Drive to API video
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:
- Ingest Videos from Google Drive - You can transfer all your videos from a folder in Google Drive to your api.video account with this CLI script.
- Ingest Videos from an FTP Server - Transfer all your videos from the FTP server of your choice to your api.video account.
- Export All Your Video Data to .csv - With this script you can grab details about each video in your api.video account and gather it in one .csv spreadsheet.
- Update Metadata for api.video Videos Using CSV - You can update your metadata in bulk with this script.
- Update Video Settings in Bulk Without a CSV File - Update videos to all be public or private or offer an mp4 download or disable an mp4 download.
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:
- Make sure you are logged in to your Google account.
- Navigate to developers.google.com/drive/api/v3/quickstart/php.
- Click Enable the Drive API. This will launch a page for starting your own developer project.
- 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.
- Agree to the Terms of Service by choosing the Yes radio button.
- Click Next.
- On the Configure your OAuth client screen, from the options choose Desktop app.
- Click Create.
- On the You're all set! screen click Download Client Configuration. This will be a file titled credentials.json.
- Navigate to api.video's helpful-scripts repository on GitHub.
- Click Code and choose Download ZIP. (You can also download using the CLI if you prefer.)
- Open the ZIP file and put the folder somewhere on your computer where you can access it easily.
- Add your credentials.json file to your folder.
- Open a terminal. (You can click the search icon in the upper right of your screen, type Terminal and pull it up that way.)
- In the terminal window you opened, navigate to the helpful-scripts folder.
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
NOTE: PHP is case sensitive, so type the file name exactly the same.
- 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.
- You're prompted for your production API key. If you haven't grabbed your key already, go to https://my.api.video/ and get the production key.
- 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.
- Next you are asked if you have your credentials.json file. If you followed the instructions so far you are ready. Type yes.
- 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.
- 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.)
- You will receive a warning that the app is requesting access to sensitive information in your Google Account. Click Advanced.
- 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).
- 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.
- Next, you are asked to confirm your choices. You are again warned about trusting your application Quickstart. Click Allow.
- 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.
- In the terminal, you'll see a prompt reading 'Enter verification code.' Paste the code you just copied in here.
- 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.
- 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.
- 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:
- Navigate to myaccount.google.com/security.
- Scroll down until you see 'Third-party apps with account access.'
- Click Manage third-party access. It opens a page titled Apps with access to your account.
- Click the Quickstart application (or whatever you named your project). It will expand to show details about what kind of access you allowed.
- Click Remove Access.
Done! Stay tuned for more helpful PHP scripts!
- How do I check what version of PHP I have?
Open a terminal window. Type
- 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.