How to Upload AVI or MP4 Movie Files from Raspberry Pi to Google Drive

This was way harder than it looked. I used the uploader.py script written by this guy at Jeremy’s Blog. What I like about it is its  simplicity – it uses a single Python script – you don’t have to download a ton of files that you don’t know what they do. You can look at the script and see what it’s doing. The hardest part was getting the Google Authentication, because the instructions on Jeremy’s Blog are WRONG WRONG WRONG. So here’s how to do it, as of today.

Note that this script is designed to be used with the “Motion” security/surveillance module, which I don’t use. I intend to use it with PiKrellCam – hopefully I can get the two working together.

Set Up Google API Authentication

You can use the first part (Step 1) of this guide by Google, or follow along. Since it sounds like Google changes their authentication process every so often, these instructions might go out of date. What you really want is to get Google to set up and generate an authentication JSON file for you to download.

  1. Log in to the Google account that you’re going to use as a repository.
  2. Go to the Google API page.
  3. At the top of the page, click the dropdown and select Create Project. Name the project: Uploader.
  4. Click Dashboard (left menu), if you’re not already on it, and click Enable an API at the top of the page. It will enable the API and automatically send you to the Library tab.
  5. Under Google Apps APIs, click Drive API. The Drive API details displays.
  6. Click the Create credentials button.
  7. Click Client ID, and then click skip this.
  8. Click Configure consent screen.
  9. In the Product name shown to users, enter Uploader. Leave all other fields blank.
  10. Click Save. The Create client Id page displays.
  11. Select/Enter the following:
  12. Application type: Other
  13. Name: Uploader
  14. Click Create. The OAuth client popup displays your authentication details – you don’t need to copy these, but you can if you want.
  15. Close the popup. You will see your API authorization in a list. On the right side of the Uploader line, click the Download icon. It displays a download file dialog box – change the file name to client_secrets.json and put it somewhere like /home/pi/motion-uploader.

OK – that was hard enough. Jeremy’s instructions helped me hobble along, but they were so far off that I blindly felt my way through the process. I hope I captured all the steps, but I might not have. The rest of Jeremy’s instructions are pretty close.

Download Script and Python Client

Go to your home directory.

$ cd /home/pi/

Get the uploader.py from github.

$ git clone https://github.com/jerbly/motion-uploader.git

Update/install Google Python API:

$ sudo pip install --upgrade google-api-python-client
$ cd motion-uploader

Make uploader.py executable:

$ chmod a+x uploader.py

Configure Script

If you used the git clone command you’ll find the example uploader.cfg file in the motion-uploader directory. Edit it.

Make sure the folder is set to where you put the client_secrets.json file and where the script has write credentials. I put mine in /home/pi/motion-uploader/.

Under [docs], set the name of the folder on your Google Drive where you want the videos to be saved and where you want photos to be saved. I created a folder in Google Drive called Motion to match this.

$ sudo nano  uploader.cfg

[oauth]
folder = /home/pi/motion-uploader/

[gmail]
# GMail account credentials
name = Joe Shmo
user = joshmo
password = dh4Kfufus0f654qmdk
sender = joshmo@gmail.com

# Recipient email address (could be same as from_addr)
recipient = joshmo@gmail.com

# Subject line for email
subject = Motion detected

# First line of email message
message = Video uploaded

[docs]
# Folder (or collection) in Docs where you want the videos to go
folder = motion
snapshot-folder = public

[options]
# Delete the local video file after the upload
delete-after-upload = true

# Send an email after the upload
send-email = true

Initial Authentication

You need to be logged in to your Google account for this. So open a browser and log in to the target account. Keep that browser open.

The initial authentication script will look like it failed. Get a test.AVI file from somewhere, or just touch test.avi to create a fake one. I used a renamed JPG I had laying around. Note that it will fail if it isn’t an AVI.

From the command line run the script from the /home/pi/motion-uploader/ directory.

$ cd /home/pi/motion-uploader/
$ ./uploader.py /home/pi/motion-uploader/uploader.cfg /home/pi/test.avi

A bloody error message displays some text about copying a URL. DO THAT. It also says: enter the Auth Code or something like that. Copy the URL in the error message, and paste it into the browser where you are already logged in to Google as the correct user. A web page displays two lines of text – the second line is the authentication code. Copy that code, go back to your terminal, and paste the auth code – press Enter. It will be happy.

This will authenticate you and create a credentials file which will be used for future logins (uploader_credentials.txt).

For me, it failed the first time, because I tried uploading a file that was NOT an AVI. But when I ran it the second time, with an AVI, it worked – and it didn’t ask for credentials.

Does It Upload MP4s?

But YES! The hippopotamus!

./uploader.py /home/pi/motion-uploader/uploader.cfg _
     /home/pi/pikrellcam/media/videos/manual_2017-03-25_10.10.55_0.mp4

Well, that’s a relief. I was expecting to have to modify the Python to allow it. Fortunately, video files all have some level of similarity – it can use the AVI MIME type for MP4s.

Next Steps

Add the upload script to the PiKrellCam script to upload after a movie file is created. Or maybe, since PKC sends me a photo of the relevant video, I don’t want it to upload the video. I would rather have a manual step there.

  1. Motion detected.
  2. Movie captured, photo emailed to me.
  3. I view the photo – if it’s a bird, I don’t do anything. If it’s a bad guy, I send a command to upload the movie to Google Drive.

I’ve been considering a way to send commands to the Pi without opening up a port to my home network. I have an idea where I set a cron job on the Pi to monitor a text file on my web server (read txt every minute). The file is writeable by me and is empty by default. I can write a PHP script to add a command to the text file. When the Pi detects a command, it kicks off a process. For example, after I get a suspicious photo as an email attachment:

  1. Go to my PHP page.
  2. Enter upvid, which adds “upvid” to my text file (or maybe just the name of the file, like manual_2017-03-25_10.10.55_0).
  3. Pi reads that there’s a command in the text file, and logs that it read it (so it doesn’t execute it twice).
  4. Pi runs the uploader.py script and uploads the file to Google Drive.

I think it will work.

Copied from Jeremy’s Blog:

Motion comes with a feature to periodically take a snapshot regardless of whether motion has been detected. This is a nice feature if you want to have a web site with the latest view from your webcam. You can use Google Drive to host this image rather than installing a web server on your Pi and opening firewalls etc.

  1. Create a public folder in your Google Drive:
    • Create a new folder called ‘public’
    • Double click on it
    • Go to the sharing menu (person icon with + sign)
    • Click “Advanced” and then “Change…”
    • Set it to “On – Public on the Web”
  2. Configure your uploader.cfg so docs/snapshot-folder is set to this folder ‘public’.
  3. Configure motion.conf to take a snapshot every n seconds named lastsnap.jpg and upload it:
    • snapshot_interval 300
    • snapshot_filename lastsnap
    • on_picture_save /home/pi/motion-uploader/uploader.py /home/pi/uploader.cfg %f snap

To find the public URL that you can bookmark and embed in other pages run the command line with the ‘snapurl’ option:

./uploader.py /home/pi/uploader.cfg /home/pi/lastsnap.jpg snapurl

 

Advertisements

3 thoughts on “How to Upload AVI or MP4 Movie Files from Raspberry Pi to Google Drive

  1. Thank you, very helpful article.
    One correction: under the heading Initial Authentication the upload command should be:

    ./uploader.py /home/pi/motion-uploader/uploader.cfg /home/pi/test.avi

    not

    ./uploader.py /home/pi/uploader.cfg /home/pi/test.avi
    as this does not correctly reference the folder for the config file.

    Unfortunately I can’t get it to work; followed all the steps and got all the same results but when I try and upload I get

    Error: [Could not find the motion folder]

    1. I have created a folder called motion on Google Drive
    2. I was able to generate the uploader_credentials.txt file by following the URL.

    My thought is that it is not correctly authenticating to Google Drive…any way I can test that.

    Thanks,

    Anthony

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s