Difference between revisions of "Upload Videos to YouTube via Raspberry Pi + Python"

From ESE205 Wiki
Jump to navigation Jump to search
Line 14: Line 14:
 
# Declare a method for authentication.
 
# Declare a method for authentication.
 
# Import the following packages:<br />
 
# Import the following packages:<br />
<code>import http.client
+
  <code>import http.client
import httplib2
+
  import httplib2
import os
+
  import os
from apiclient.discovery import build
+
  from apiclient.discovery import build
from apiclient.errors import HttpError
+
  from apiclient.errors import HttpError
from apiclient.http import MediaFileUpload
+
  from apiclient.http import MediaFileUpload
from oauth2client.client import flow_from_clientsecrets
+
  from oauth2client.client import flow_from_clientsecrets
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:
 
# Follow the following example code:
 
   def get_authenticated_service(args):
 
   def get_authenticated_service(args):

Revision as of 04:53, 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

  1. Go to https://console.developers.google.com/projectselector/apis/credentials.
  2. Create a new project by clicking "Create".
  3. Enter the name for your project and obtain a project ID.
  4. Click "Create Credentials" and choose "OAuth client ID."
  5. Configure the OAuth consent screen by entering the corresponding information.
  6. Choose the application type to be "Other."
  7. Give a name to the application type, such as "Python program."
  8. 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
  1. 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()))
  1. Define the CLIENT_SECRETS_FILE to be the file directory to the json file of the newly created OAuth 2.0 client ID.
  2. 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
  1. Import subprocess in the Python 3 program.
  2. Include the following code in the Python 3 program to convert the video:

subprocess.getoutput("MP4Box -add [video directory]"+".h264 "+"[video directory]+".mp4")

  1. Go through the Google APIs to find the corresponding methods for video uploads (initialize upload and resumable upload) to upload the converted video file.