Kutluhan AktarDFRobot
Published © CC BY

YouTube Night Vision Video Recorder and Uploader GUI on Pi

Record videos with selectable image effects and upload them to YouTube with user-defined settings via the YouTube Data API, coded in Python.

ExpertFull instructions provided5 hours3,409
YouTube Night Vision Video Recorder and Uploader GUI on Pi

Things used in this project

Hardware components

Raspberry Pi 3 Model B+
Raspberry Pi 3 Model B+
×1
Flash Memory Card, SD Card
Flash Memory Card, SD Card
×1
7'' HDMI Display with Capacitive Touchscreen
DFRobot 7'' HDMI Display with Capacitive Touchscreen
×1
DFRobot 5MP Night Vision Camera for Raspberry Pi
×1

Software apps and online services

Raspberry Pi Thonny Python IDE
Google APIs Client Library for Python
Google YouTube Data API v3

Story

Read more

Custom parts and enclosures

YouTube Video Recorder and Uploader GUI (Zipped)

Schematics

Screen

Code

YouTube_Video_Recorder_and_Uploader_GUI.py

Python
# YouTube Video Recorder and Uploader with Night Vision on Raspberry Pi
#
# Raspberry Pi 3B+
# 
# By Kutluhan Aktar
#
# Develop a GUI to record videos with the selected image settings and effects and upload them to your YouTube channel
# with the given parameters (description, title, category, e.g.,).
# 
# Get more information on the project page:
# https://theamplituhedron.com/projects/YouTube-Video-Recorder-and-Uploader-GUI-with-Night-Vision-on-Raspberry-Pi/


from guizero import App, Box, Text, TextBox, PushButton, ButtonGroup, Combo, Slider, ListBox, MenuBar, info
from picamera import PiCamera
from time import sleep
from subprocess import call, Popen
import datetime
import webbrowser
import glob

# Create the YouTube_Recorder_Uploader class with the required settings:
class YouTube_Recorder_Uploader:
    def __init__(self, r_x, r_y, framerate):
        # Define the camera module settings.
        self.camera = PiCamera()
        self.camera.resolution = (r_x, r_y)
        self.camera.framerate = framerate
    def open_folder(self):
        # Open the parent folder to inspect videos.
        webbrowser.open("//home//pi//YouTube-Recorder-and-Uploader//")
    def about(self):
        # Define the information box.
        info('About', 'Learn how create a GUI by which you can record videos with the selected image settings and upload them to your YouTube channel using Google API (YouTube Data v3) with the given parameters :)')
    def tutorial(self):
        # Go to the project tutorial page.
        webbrowser.open("https://theamplituhedron.com/projects/YouTube-Video-Recorder-and-Uploader-GUI-with-Night-Vision-on-Raspberry-Pi/")
    def get_existing_videos(self):
        # Get the mp4 videos in the Recorded folder to be able to select a video to upload or play via this GUI.
        files = [f for f in glob.glob("/home/pi/YouTube-Recorder-and-Uploader/Recorded/*.mp4")]
        # Insert new list to the ListBox and remove the old items.
        u_select_input.clear()
        for f in files:
            u_select_input.append(f)
    def show_selected_video(self):
        # Create a pop-up including the selected video.
        info("Selected Video", u_select_input.value)
    def record(self):
        # Get the current date as the timestamp to generate unique file names.
        date = datetime.datetime.now().strftime('%m-%d-%Y_%H.%M.%S')
        # Get the entered video settings to record a video.
        filename = r_name_input.value[:-1]
        annotate = r_annotate_input.value[:-1]
        effect = r_effect_input.value
        duration = r_duration_input.value
        # Define video file path and location.
        path = '/home/pi/YouTube-Recorder-and-Uploader/Recorded/'
        video_h264 = path + filename + '-' + date + '.h264'
        video_mp4 = path + filename + '-' + date + '.mp4'
        # Record a video with the given settings.
        print("\r\nRecording Settings:\r\nLocation => " + video_h264 + "\r\nAnnotate => " + annotate + "\r\nEffect => " + effect + "\r\nDuration => " + str(duration))
        self.camera.annotate_text = annotate
        self.camera.image_effect = effect
        self.camera.start_preview()
        self.camera.start_recording(video_h264)
        sleep(int(duration))
        self.camera.stop_recording()
        self.camera.stop_preview()
        print("Rasp_Pi => Video Recorded! \r\n")
        # Convert the h264 format to the mp4 format to upload videos in mp4 format to YouTube.
        command = "MP4Box -add " + video_h264 + " " + video_mp4
        call([command], shell=True)
        print("\r\nRasp_Pi => Video Converted! \r\n")
        # Update the video list after recording a new video.
        self.get_existing_videos()
    def upload(self):
        # Get the entered YouTube video parameters (title, description, e.g.,).
        title = u_title_input.value
        description = u_description_input.value
        keywords = u_keywords_input.value
        category = u_category_input.value
        privacy = u_privacy_input.value
        selected_video = u_select_input.value
        # Print the given uploading settings (parameters).
        print("\r\nYouTube Uploading Settings:\r\nTitle => " + title + "Description => " + description + "Keywords => " + keywords + "Category => " + category + "\r\nPrivacy Status => " + privacy + "\r\nSelected Video => " + selected_video)
        # Upload video to the registered YouTube account by transferring uploading settings to the upload_video.py file.
        command = (
           'sudo python /home/pi/YouTube-Recorder-and-Uploader/upload_video.py --file="'+ selected_video
         + '" --title="' + title[:-1]
         + '" --description="' + description[:-1]
         + '" --keywords="' + keywords[:-1]
         + '" --category="' + category
         + '" --privacyStatus="' + privacy + '"'
        )
        print("\r\nTerminal Command => " + command + "\r\n")
        call([command], shell=True)
        print("\r\nRasp_Pi => Attempted to upload the selected video via Google Client API! \r\n")
    def play(self):
        # Play the selected video using omxplayer.
        print("\r\nRasp_Pi => Selected Video Played on the omxplayer! \r\n")
        selected_video = u_select_input.value
        omxplayer = Popen(['omxplayer',selected_video])
      
        
# Define a new class object named as 'video'.
video = YouTube_Recorder_Uploader(800, 600, 15)
        
# Create the YouTube Video Recorder and Uploader GUI:
appWidth = 1024
appHeight = 600
app = App(title="YouTube Video Recorder and Uploader", bg="#1F2020", width=appWidth, height=appHeight)
# Define menu bar options.
menubar = MenuBar(app, toplevel=["Files", "About"],
                  options=[
                      [ ["Open In Folder", video.open_folder] ],
                      [ ["Tutorial", video.tutorial], ["Information", video.about] ]
                  ])
# Design the application interface.
app_header = Box(app, width="fill", height=50, align="top")
app_header_text = Text(app_header, text="YouTube Video Recorder and Uploader", color="white", size=20)
app_record = Box(app, width="fill", height="fill", layout="grid", align="left")
app_upload = Box(app, width="fill", height="fill", layout="grid", align="right")
app_upload.bg = "#A5282C"
# Create the record menu to be able to change the video settings.
r_name_label = Text(app_record, text="Filename : ", color="#A5282C", size=15, grid=[0,0], align="left")
r_name_input = TextBox(app_record, width=40, grid=[1,0], height=2, multiline=True)
r_name_input.bg = "#A5282C"
r_name_input.text_color = "white"
r_annotate_label = Text(app_record, text="Annotate : ", color="#A5282C", size=15, grid=[0,1], align="left")
r_annotate_input = TextBox(app_record, width=40, grid=[1,1], height=2, multiline=True)
r_annotate_input.bg = "#A5282C"
r_annotate_input.text_color = "white"
r_effect_label = Text(app_record, text="Image Effect : ", color="#A5282C", size=15, grid=[0,2], align="left")
r_effect_input = Combo(app_record, grid=[1,2], align="right", options=["none", "negative", "solarize", "sketch", "pastel", "watercolor", "film", "blur", "saturation", "posterise", "cartoon", "colorpoint", "colorbalance"], selected="none")
r_effect_input.bg = "#A5282C"
r_effect_input.text_color = "white"
r_effect_input.text_size = 20
r_duration_label = Text(app_record, text="Duration : ", color="#A5282C", size=15, grid=[0,3], align="left")
r_duration_input = Slider(app_record, end=250, grid=[1,3], align="right")
r_duration_input.bg = "#A5282C"
r_duration_input.text_color = "white"
r_duration_input.text_size = 20
r_submit = PushButton(app_record, text="Record", width=10, grid=[0,4], command=video.record, padx=15, pady=15)
r_submit.bg = "#A5282C"
r_submit.text_color = "white"
r_submit.text_size = 25
# Create the upload menu to be able to upload the selected video with the given parameters to YouTube.
u_title_label = Text(app_upload, text="Title : ", color="#F3D060", size=15, grid=[0,0], align="left")
u_title_input = TextBox(app_upload, grid=[1,0], width=35, height=2, multiline=True)
u_title_input.bg = "#F3D060"
u_title_input.text_color = "white"
u_description_label = Text(app_upload, text="Description : ", color="#F3D060", size=15, grid=[0,1], align="left")
u_description_input = TextBox(app_upload, grid=[1,1], width=35, height=2, multiline=True)
u_description_input.bg = "#F3D060"
u_description_input.text_color = "white"
u_keywords_label = Text(app_upload, text="Keywords : ", color="#F3D060", size=15, grid=[0,2], align="left")
u_keywords_input = TextBox(app_upload, grid=[1,2], width=35, height=2, multiline=True)
u_keywords_input.bg = "#F3D060"
u_keywords_input.text_color = "white"
u_category_label = Text(app_upload, text="Category : ", color="#F3D060", size=15, grid=[0,3], align="left")
# You can find more information regarding the YouTube category numbers on the project page.
u_category_input = Combo(app_upload, grid=[1,3], align="right", options=["22", "1", "10", "20", "21", "23", "24", "25", "26", "27", "28", "30"], selected="22")
u_category_input.bg = "#F3D060"
u_category_input.text_color = "white"
u_category_input.text_size = 20
u_privacy_label = Text(app_upload, text="Privacy Status : ", color="#F3D060", size=15, grid=[0,4], align="left")
u_privacy_input = ButtonGroup(app_upload, grid=[1,4], horizontal=True, align="right", options=["public", "private", "unlisted"], selected="private")
u_privacy_input.bg = "#F3D060"
u_privacy_input.text_color = "white"
u_privacy_input.text_size = 12
u_select_label = Text(app_upload, text="Select a Video : ", color="#F3D060", size=15, grid=[0,5], align="left")
# Get the paths of the recorded videos in the Recorded folder.
u_select_input = ListBox(app_upload, grid=[1,5], align="right", width=200, height=150, command=video.show_selected_video, items=["none"], scrollbar=True)
video.get_existing_videos()
u_select_input.bg = "#F3D060"
u_select_input.text_color = "white"
u_select_input.text_size = 15
u_p_submit = PushButton(app_upload, text="Play", grid=[0,6], align="left", command=video.play, padx=30, pady=10)
u_p_submit.bg = "#F3D060"
u_p_submit.text_color = "white"
u_p_submit.text_size = 25
u_u_submit = PushButton(app_upload, text="Upload", grid=[0,7], align="left", command=video.upload, padx=15, pady=10)
u_u_submit.bg = "#F3D060"
u_u_submit.text_color = "white"
u_u_submit.text_size = 25
# Initiate the application loop.
app.display()

upload_video.py

Python
#!/usr/bin/python

import httplib
import httplib2
import os
import random
import sys
import time

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


# Explicitly tell the underlying HTTP transport library not to retry, since
# we are handling retry logic ourselves.
httplib2.RETRIES = 1

# Maximum number of times to retry before giving up.
MAX_RETRIES = 10

# Always retry when these exceptions are raised.
RETRIABLE_EXCEPTIONS = (httplib2.HttpLib2Error, IOError, httplib.NotConnected,
  httplib.IncompleteRead, httplib.ImproperConnectionState,
  httplib.CannotSendRequest, httplib.CannotSendHeader,
  httplib.ResponseNotReady, httplib.BadStatusLine)

# Always retry when an apiclient.errors.HttpError with one of these status
# codes is raised.
RETRIABLE_STATUS_CODES = [500, 502, 503, 504]

# The CLIENT_SECRETS_FILE variable specifies the name of a file that contains
# the OAuth 2.0 information for this application, including its client_id and
# client_secret. You can acquire an OAuth 2.0 client ID and client secret from
# the Google API Console at
# https://console.developers.google.com/.
# Please ensure that you have enabled the YouTube Data API for your project.
# For more information about using OAuth2 to access the YouTube Data API, see:
#   https://developers.google.com/youtube/v3/guides/authentication
# For more information about the client_secrets.json file format, see:
#   https://developers.google.com/api-client-library/python/guide/aaa_client_secrets
CLIENT_SECRETS_FILE = "/home/pi/YouTube-Recorder-and-Uploader/client_secrets.json"

# This OAuth 2.0 access scope allows an application to upload files to the
# authenticated user's YouTube channel, but doesn't allow other types of access.
YOUTUBE_UPLOAD_SCOPE = "https://www.googleapis.com/auth/youtube.upload"
YOUTUBE_API_SERVICE_NAME = "youtube"
YOUTUBE_API_VERSION = "v3"

# This variable defines a message to display if the CLIENT_SECRETS_FILE is
# missing.
MISSING_CLIENT_SECRETS_MESSAGE = """
WARNING: Please configure OAuth 2.0

To make this sample run you will need to populate the client_secrets.json file
found at:

   %s

with information from the API Console
https://console.developers.google.com/

For more information about the client_secrets.json file format, please visit:
https://developers.google.com/api-client-library/python/guide/aaa_client_secrets
""" % os.path.abspath(os.path.join(os.path.dirname(__file__),
                                   CLIENT_SECRETS_FILE))

VALID_PRIVACY_STATUSES = ("public", "private", "unlisted")


def get_authenticated_service(args):
  flow = flow_from_clientsecrets(CLIENT_SECRETS_FILE,
    scope=YOUTUBE_UPLOAD_SCOPE,
    message=MISSING_CLIENT_SECRETS_MESSAGE)

  storage = Storage("%s-oauth2.json" % sys.argv[0])
  credentials = storage.get()

  if credentials is None or credentials.invalid:
    credentials = run_flow(flow, storage, args)

  return build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION,
    http=credentials.authorize(httplib2.Http()))

def initialize_upload(youtube, options):
  tags = None
  if options.keywords:
    tags = options.keywords.split(",")

  body=dict(
    snippet=dict(
      title=options.title,
      description=options.description,
      tags=tags,
      categoryId=options.category
    ),
    status=dict(
      privacyStatus=options.privacyStatus
    )
  )

  # Call the API's videos.insert method to create and upload the video.
  insert_request = youtube.videos().insert(
    part=",".join(body.keys()),
    body=body,
    # The chunksize parameter specifies the size of each chunk of data, in
    # bytes, that will be uploaded at a time. Set a higher value for
    # reliable connections as fewer chunks lead to faster uploads. Set a lower
    # value for better recovery on less reliable connections.
    #
    # Setting "chunksize" equal to -1 in the code below means that the entire
    # file will be uploaded in a single HTTP request. (If the upload fails,
    # it will still be retried where it left off.) This is usually a best
    # practice, but if you're using Python older than 2.6 or if you're
    # running on App Engine, you should set the chunksize to something like
    # 1024 * 1024 (1 megabyte).
    media_body=MediaFileUpload(options.file, chunksize=-1, resumable=True)
  )

  resumable_upload(insert_request)

# This method implements an exponential backoff strategy to resume a
# failed upload.
def resumable_upload(insert_request):
  response = None
  error = None
  retry = 0
  while response is None:
    try:
      print "Uploading file..."
      status, response = insert_request.next_chunk()
      if response is not None:
        if 'id' in response:
          print "Video id '%s' was successfully uploaded." % response['id']
        else:
          exit("The upload failed with an unexpected response: %s" % response)
    except HttpError, e:
      if e.resp.status in RETRIABLE_STATUS_CODES:
        error = "A retriable HTTP error %d occurred:\n%s" % (e.resp.status,
                                                             e.content)
      else:
        raise
    except RETRIABLE_EXCEPTIONS, e:
      error = "A retriable error occurred: %s" % e

    if error is not None:
      print error
      retry += 1
      if retry > MAX_RETRIES:
        exit("No longer attempting to retry.")

      max_sleep = 2 ** retry
      sleep_seconds = random.random() * max_sleep
      print "Sleeping %f seconds and then retrying..." % sleep_seconds
      time.sleep(sleep_seconds)

if __name__ == '__main__':
  argparser.add_argument("--file", required=True, help="Video file to upload")
  argparser.add_argument("--title", help="Video title", default="Test Title")
  argparser.add_argument("--description", help="Video description",
    default="Test Description")
  argparser.add_argument("--category", default="22",
    help="Numeric video category. " +
      "See https://developers.google.com/youtube/v3/docs/videoCategories/list")
  argparser.add_argument("--keywords", help="Video keywords, comma separated",
    default="")
  argparser.add_argument("--privacyStatus", choices=VALID_PRIVACY_STATUSES,
    default=VALID_PRIVACY_STATUSES[0], help="Video privacy status.")
  args = argparser.parse_args()

  if not os.path.exists(args.file):
    exit("Please specify a valid file using the --file= parameter.")

  youtube = get_authenticated_service(args)
  try:
    initialize_upload(youtube, args)
  except HttpError, e:
    print "An HTTP error %d occurred:\n%s" % (e.resp.status, e.content)

client_secrets.json

JSON
{
  "web": {
    "client_id": "[[INSERT CLIENT ID HERE]]",
    "client_secret": "[[INSERT CLIENT SECRET HERE]]",
    "redirect_uris": [],
    "auth_uri": "https://accounts.google.com/o/oauth2/auth",
    "token_uri": "https://accounts.google.com/o/oauth2/token"
  }
}

Credits

Kutluhan Aktar

Kutluhan Aktar

79 projects • 289 followers
Self-Taught Full-Stack Developer | @EdgeImpulse Ambassador | Maker | Independent Researcher
DFRobot

DFRobot

62 projects • 143 followers
Empowering Creation for Future Innovators

Comments