Twenty Steps to a Youtube Uploader with Raspberry Pi

Raspberry Pi Youtube Uploader: Full Instructions

1.) Download the raspbian image file here

http://www.raspberrypi.org/downloads/

2.) Use the instructions from the same website to write the image to the SD Card

3.) Plug the card into your raspberry Pi. Plug the network connection into your raspberry Pi. Plug in the power supply (cell phone charger). The specific requirements for power supplies vary, but the best is a run-of-the mill high quality cell phone charger. Avoid the rapid charging kind.

4.) Figure out the local IP address for your raspberry pi. This can take some effort, and there are many ways to do it.

5.) SSH into the raspberry pi. In windows you will have to use putty. In linux this is:

ssh pi@<local-IP>

To check the fingerprint run either of these:

ssh-keygen -lf /etc/ssh/ssh_host_ecdsa_key.pub
ssh-keygen -lf /etc/ssh/ssh_host_rsa_key.pub

6.) You now have command line access to the raspberry pi. First change the password as soon as possible.

sudo raspi-config

In this menu there are several tasks to do. If you didn’t already change the default password, do so here ASAP. There are other options, use the space bar to make/unmake selections.

  • Expand the file system to fill out all 4GB of your SD card (or more)
  • Set it to boot to desktop
  • It is best to change the keyboard in the internationalization options (the default I believe
  • is the UK keyboard layout).
  • Set the timezone/location
  • Set the domain name in advanced if you like

(NOTE: If you would rather not use a VNC, and feel comfortable with the CLI don’t set boot to desktop skip ahead to step 13)

REBOOT!

7.) Update! There are several ways of doing this, but I took the liberty of combining them into a single sweet command. It will take a while and a lot of things will fly by on the window.

sudo apt-get update && sudo apt-get upgrade -y && sudo apt-get clean && sudo apt-get autoremove

REBOOT!

8.)Install x11vnc. This software serves up the :0 screen, whereas tightvncserver typically serves up :1, and closes it when you quit. This is not ideal. Thus x11vnc is better for this use, but tightvnc is better for other uses.

sudo apt-get install x11vnc

9.) Setup x11vnc to work by setting the password.

x11vnc -storepasswd

10.) Make x11vnc auto start. There are two methods so pick one

a.) Have the .xsessionrc file load the program.

nano ~/.xsessionrc

Add this to the file. If you would like to configure your own options, this link describes them all.
http://www.karlrunge.com/x11vnc/x11vnc_opts.html

# Start X11VNC
x11vnc -bg -nevershared -forever -tightfilexfer -scale 800×600 -usepw -display :0

Make it executable

chmod 775 ~/.xsessionrc

b.) Use the autostart method

cd .config
mkdir autostart
cd autostart
nano x11vnc.desktop

Paste in the normal stuff

[Desktop Entry]
Encoding=UTF-8
Type=Application
Name=x11vnc
Comment=VNC
Exec=x11vnc -bg -nevershared -forever -tightfilexfer -scale 800×600 -usepw -display :0
StartupNotify=false
Terminal=false
Hidden=false

11.) Install chromium because it works best with youtube.

sudo apt-get install chromium

12.) At this point you can login and use chromium to upload via a VNC client. The process is simple.

a.) Put your videos on a thumb drive (preferably a 16GB or more drive with a flashing light)

b.) VNC into the machine. There are multiple tools and a variety of ways to accomplish this task, not just this way:

In windows, launch putty configured to allow a tunnel and then launch tightvncviewerl. For the tunnel to work tightvncviewer will connect to localhost. Meaning that all you type into tightvncviewer is 127.0.0.1:5900, or localhost:5900. Putty will then send the data back and forth between pi and your desktop computer safely. This means both programs must be active and working together.

c.) If you feel secure you can save your passwords in the chromium profile, and even have it launch to the youtube upload page by default. Simply start the upload

d.) Close the VNC client and wait. You do NOT have to log back into the raspberry pi VNC to check progress. This is why having a flash drive with a falshing light comes in handy, you can tell that the upload is finished/stalled easier.

13.) There is still an Achilles heel of security leftover, and it is somewhat cumbersome to have to VNC into the pi every time you want to start an upload.

If someone gets a hold of the pi, they might conceivably have access to your youtube account. However, there is a way to make it unable to be used in this fashion.

It is also possible to make the operation happen automagically when you plug in the flash drive. It will basically initiate an upload that you can edit later.

Here is a link that might scare you, or whet your appetite: https://developers.google.com/youtube/v3/guides/uploading_a_video

Start by making a working directory

cd ~
mkdir youtube-uploader
cd youtube-uploader

14.) To enable uploading with the google developer api. Go to the developer console and login (preferably NOT on the raspberry pi)

https://cloud.google.com/console/project

Create a project, it doesn’t matter what you name it. It may take a few seconds.

Click on the project you created, and click “Enable an API”. Here you want to turn on the Youtube data API in the list. If you ever feel your pi has been compromised, this is the place you come to turn off it’s ability to upload.

Click credentials on the menu to see the keys/codes/whatever. These long strings are the passwords that lets all the magic happen.

Create a new ID of type ‘other’ to get the certificate active

15.) Make a file so that the keys are accessible to the scripts, and paste/type in the info from the google page.

sudo nano client_secrets.json

Paste this in and edit accordingly.

{
“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”
}
}

14.) Download and unzip the api straight from google

wget https://google-api-python-client.googlecode.com/files/google-api-python-client-1.2.tar.gz
tar xzvf google-api-python-client-1.2.tar.gz

You can choose to work from the directory, or the sub-directory

a.) Move the json file into the created directory and work inside

cd google-api-python-client-1.2
mv apiclient oauth2client uritemplate ..
cd ..

b.) OR move the directories up to the top level

mv client_secrets.json google-api-python-client-1.2
cd google-api-python-client-1.2

15.) Download the sample script directly from google

wget https://code.google.com/p/youtube-api-samples/source/browse/samples/python/upload_video.py

Same thing, but from github:

wget https://github.com/youtube/api-samples/raw/master/python/upload_video.py

16.) Some last minute installations…

A python library that is used in the script (that somehow does not come installed by default)

sudo apt-get install pip
sudo pip install httplib2

If you have not done so because you skipped ahead install chromium

sudo apt-get install chromium

17.) Test that the script works without giving arguments. This way you aren’t actually uploading a video, you’re just making sure the script runs.

python upload_video.py

The output should be something like this:

usage: upload_video.py [–auth_host_name AUTH_HOST_NAME]
[–noauth_local_webserver]
[–auth_host_port [AUTH_HOST_PORT [AUTH_HOST_PORT …]]]
[–logging_level {DEBUG,INFO,WARNING,ERROR,CRITICAL}]
–file FILE [–title TITLE] [–description DESCRIPTION]
[–category CATEGORY] [–keywords KEYWORDS]
[–privacyStatus {public,private,unlisted}]
upload_video.py: error: argument –file is required

If it doesn’t look like this something is not working.

18.) At this point we can now upload a file to youtube if we plug all the commands in, but we don’t want to type everything out each time we put a flash drive in the raspberry pi. Furthermore, some flash drives cause the raspberry pi to reboot upon being plugged in. This is a minor annoyance, but is not that bad.

We want something to scan for video files every minute and upload if anything is there. This way we achieve complete automagic. Plug the stick in, and nothing more is required. It get’s a little tricky here so bear with me. Start a scrip file and fill out all the default information for the upload. This information will show up on every single upload to youtube. It doesn’t matter that much because you can change it later.

nano youtube_upload.sh

Paste in this and edit the upload settings at the end to your liking

cd ~/youtube-upload

for _file in /media/*/*.mov /media/*/*.mp4 /media/*/*.flv /media/*/*.avi; do

     clean=$(echo $_file|wc -m)

     if [ $clean -gt 15 ]; then

          did=$(grep “$_file” ~/youtube-upload/uploaded_videos.txt | wc -l)

          if [ $did = 0 ]; then

               #Edit this to your liking

               python upload_video.py –file=”$_file” –title=”Temp” –description=”Temp” –keywords=”Temp” –category=”22″ –privacyStatus=”private” –noauth_local_webserver

               echo $_file uploaded to youtube via google developer api | logger

               echo $_file >> ~/youtube-upload/uploaded_videos.txt

         fi

     fi

done

 


 

19.) This script will upload everything in any attached drive, but it won’t run itself without a cron job. It would be wise to test it out prior to setting up the cron job. It can be run using this command to see if it works.

./youtube_upload.sh

If all goes well you can check the log for the upload, or your browser on your desktop to see if it’s working.

Open the cron editor:

crontab -e

To set the cron job add this line to the end of the file. The flock parts prevent it from running multiple instances simultaneously uploading the same file over and over every minute.

*/1 * * * * flock -n /var/run/runlock -c ~/youtube-upload/youtube_upload.sh || flock -u /var/run/runlock

There is an occasional problem with the lockfile staying locked between two boots, this is especially true if you unplug the pi in the middle of an upload. If this is the case add this to the sudo cron

sudo crontab -e

Careful! This one says sudo, its different!

@reboot flock -n /var/run/runlock -c ~/youtube-upload/youtube_upload.sh || flock -u /var/run/runlock

That’s it!! Now every time a thumb drive is plugged in it will start an upload to youtube within a few minutes.

20.) If an upload is interrupted…

If a file errors in the middle of the upload, and you want to upload it again, then you can either delete the uploaded_videos.txt log, or manually edit and remove the entry. This is because the crude script logs every upload, but won’t upload the same file twice.

rm ~/youtube-upload/uploaded_videos.txt

I set up a .bashrc alias to do the above command, it’s way easier. Just add this to the bottom of the .bashrc file:

nano .bashrc

alias yclean=”rm ~/youtube-upload/uploaded_videos.txt”

AFTERTHOUGHTS:

If you want to check the log dmesg will not help you must use this:

cat /var/log/messages

Don’t name your video files the same thing every-time, as this will confuse the script… and you.

If you opt for the automagic method, it doesn’t matter if someone steals your pi. They can’t hijack your account. You can disable the attackers ability to upload and remove the malicious uploads. This doesn’t mean that they can’t do other harmful things, it just means that they do not have permanent control.

If you maintain multiple youtube accounts automatic uploads can become cumbersome, so the manual method using the VNC may be preferable… unless you want to write your own script for that…

I have specifically chosen not to develop a full fledged app. This is for a very good set of reasons. The raspberry pi is not well suited for apps, and api are prone to being tweaked, updated, or canceled. The way I see it is that If I spent the time to develop a full fledged app Google would cancel the api out of malice, or linux/raspbian updates would eat me alive.

Be careful copy/pasting from this article, as there are some issues with certain characters not pasting exactly correct into the cli. For example, quotations and dashes don’t show up right when copy/pasted. Just to keep you on your toes.

Leave a Reply