Upload Videos to YouTube via Raspberry Pi + Python
From ESE205 Wiki
(Redirected from Directly Upload Videos to YouTube with Raspberry Pi and Python)
This guide demonstrates how to directly upload videos recorded by the Raspberry Pi to YouTube by using a Python 3 program.
Creating Google APIs Credentials
- Go to https://console.developers.google.com/projectselector/apis/credentials.
- Create a new project by clicking "Create".
- Enter the name for your project and obtain a project ID.
- Click "Create Credentials" and choose "OAuth client ID."
- Configure the OAuth consent screen by entering the corresponding information.
- Choose the application type to be "Other."
- Give a name to the application type, such as "Python program."
- Download the json file of the newly created OAuth 2.0 client ID.
Authentication in the Python Program
- 1. Declare a method for authentication.
- 2. Import the following packages:
import http.client import httplib2 import os from apiclient.discovery import build from apiclient.errors import HttpError from apiclient.http import MediaFileUpload from oauth2client.client import flow_from_clientsecrets from oauth2client.file import Storage from oauth2client.tools import argparser, run_flow</code>
- 3. Follow the following example code:
def get_authenticated_service(args): flow = flow_from_clientsecrets(CLIENT_SECRETS_FILE, scope=YOUTUBE_UPLOAD_SCOPE, message="") storage = Storage("%s-oauth2.json" % sys.argv) credentials = run_flow(flow, storage, args) return build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION, http=credentials.authorize(httplib2.Http()))
- 4. Define the CLIENT_SECRETS_FILE to be the file directory to the json file of the newly created OAuth 2.0 client ID.
- 5. Define the YOUTUBE_UPLOAD_SCOPE to be "https://www.googleapis.com/auth/youtube.upload", YOUTUBE_API_SERVICE_NAME to be "youtube", and YOUTUBE_API_VERSION to be "v3".
Converting the video file from .h264 to .mp4 and Uploading it to YouTube
- 1. Install the converter through Terminal on Raspberry Pi by using the following code:
sudo apt-get update sudo apt-get install gpac y
- 2. Import subprocess in the Python 3 program.
- 3. Include the following code in the Python 3 program to convert the video:
subprocess.getoutput("MP4Box -add [video directory]"+".h264 "+"[video directory]+".mp4")
- 4. Go through the Google APIs to find the corresponding methods for video uploads (initialize upload and resumable upload) to upload the converted video file.