Difference between revisions of "Upload Videos to YouTube via Raspberry Pi + Python"
Jump to navigation
Jump to search
m (Ethanshry moved page Directly Upload Videos to YouTube with Raspberry Pi and Python to Upload Videos to YouTube via Raspberry Pi + Python: Fall 2018 Wiki Reworks) |
|||
(3 intermediate revisions by 2 users not shown) | |||
Line 12: | Line 12: | ||
==Authentication in the Python Program== | ==Authentication in the Python Program== | ||
− | + | :1. Declare a method for authentication. | |
− | + | :2. Import the following packages: | |
import http.client | import http.client | ||
import httplib2 | import httplib2 | ||
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> | ||
− | + | :3. 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())) | ||
− | + | :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== | ==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 update | ||
sudo apt-get install gpac | sudo apt-get install gpac | ||
y | 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") | 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. | |
+ | [[Category:HowTos]] | ||
+ | [[Category:Raspberry_Pi]] | ||
+ | [[Category:Programming]] |
Latest revision as of 18:50, 18 August 2018
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
- 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[0]) 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.