Difference between revisions of "Upload Videos to YouTube via Raspberry Pi + Python"
Jump to navigation
Jump to search
Line 12: | Line 12: | ||
==Authentication in the Python Program== | ==Authentication in the Python Program== | ||
− | + | * Declare a method for authentication. | |
− | + | * Import the following packages: | |
− | + | import http.client | |
import httplib2 | import httplib2 | ||
import os | import os | ||
Line 23: | Line 23: | ||
from oauth2client.file import Storage | from oauth2client.file import Storage | ||
from oauth2client.tools import argparser, run_flow</code> | from oauth2client.tools import argparser, run_flow</code> | ||
− | + | * Follow the following example code: | |
def get_authenticated_service(args): | def get_authenticated_service(args): | ||
flow = flow_from_clientsecrets(CLIENT_SECRETS_FILE, | flow = flow_from_clientsecrets(CLIENT_SECRETS_FILE, | ||
Line 32: | Line 32: | ||
return build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION, | return build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION, | ||
http=credentials.authorize(httplib2.Http())) | http=credentials.authorize(httplib2.Http())) | ||
− | + | * Define the CLIENT_SECRETS_FILE to be the file directory to the json file of the newly created OAuth 2.0 client ID. | |
− | + | * 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== | ==Converting the video file from .h264 to .mp4 and Uploading it to YouTube== | ||
− | + | * Install the converter through Terminal on Raspberry Pi by using the following code: | |
sudo apt-get update | sudo apt-get update | ||
sudo apt-get install gpac | sudo apt-get install gpac | ||
y | y | ||
− | + | * Import subprocess in the Python 3 program. | |
− | + | * Include the following code in the Python 3 program to convert the video: | |
− | subprocess.getoutput("MP4Box -add [video directory]"+".h264 "+"[video directory]+".mp4") | + | subprocess.getoutput("MP4Box -add [video directory]"+".h264 "+"[video directory]+".mp4") |
− | + | * Go through the Google APIs to find the corresponding methods for video uploads (initialize upload and resumable upload) to upload the converted video file. |
Revision as of 04:54, 28 April 2017
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
- Declare a method for authentication.
- 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
- 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[0]) credentials = run_flow(flow, storage, args) return build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION, http=credentials.authorize(httplib2.Http()))
- Define the CLIENT_SECRETS_FILE to be the file directory to the json file of the newly created OAuth 2.0 client ID.
- 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
- Install the converter through Terminal on Raspberry Pi by using the following code:
sudo apt-get update sudo apt-get install gpac y
- Import subprocess in the Python 3 program.
- Include the following code in the Python 3 program to convert the video:
subprocess.getoutput("MP4Box -add [video directory]"+".h264 "+"[video directory]+".mp4")
- Go through the Google APIs to find the corresponding methods for video uploads (initialize upload and resumable upload) to upload the converted video file.