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

From ESE205 Wiki
Jump to navigation Jump to search
 
(5 intermediate revisions by 2 users not shown)
Line 12: Line 12:
  
 
==Authentication in the Python Program==
 
==Authentication in the Python Program==
* Declare a method for authentication.
+
:1. Declare a method for authentication.
* Import the following packages:
+
: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>
* Follow the following example 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()))
* Define the CLIENT_SECRETS_FILE to be the file directory to the json file of the newly created OAuth 2.0 client ID.
+
:4. 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".
+
: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==
* Install the converter through Terminal on Raspberry Pi by using the following code:
+
: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
* Import subprocess in the Python 3 program.
+
:2. Import subprocess in the Python 3 program.
* Include the following code in the Python 3 program to convert the video:
+
: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")
* Go through the Google APIs to find the corresponding methods for video uploads (initialize upload and resumable upload) to upload the converted video file.
+
: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

  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
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.