Send Instagram Videos to Youtube with Raspberry Pi and Bash

Making Instagram videos is quick and easy.  Making Youtube videos is a laborious editing process that sucks the sunlight out of my soul.  People kept asking me if I have a Youtube channel and I was embarrassed to admit that yes, sort of, but no, not really.  I needed a way to send my Instagram videos to Youtube… a way to make the rock that kills the first bird bounce and kill the second bird!  With a bit of Bash scripting I was able to get my Raspberry Pi to automatically do this for me.  Read on for all the deets.

I used to use Zapier but they got dumb.  First they wanted to charge me to add extra steps to my one and only zap, then they said they couldn’t work ANYWAYS because Instagram tightened the data access rules.  I’m a big fan of IFTT but they can’t do it, either.  So!  Hand-rolled it is.

The pieces someone else made

I already had a Raspberry Pi 3 (?) set up to take timelapse pictures of my Prusa 3D prints.  I figure it’s idle most of the time, why not make it work a little harder?  Firstly I logged in and updated all the underlying framework.

sudo apt-get update
sudo apt-get upgrade
mkdir ig2yt
cd ig2yt

Then I installed https://github.com/rarcega/instagram-scraper to grab my instagram content.

sudo apt-get install python3
sudo apt-get install python3-venv
python3 -m venv env
/env/bin/python -m pip install instagram-scraper

and tested the instagram-scraper

/env/bin/instagram-scraper yourTargetChannel -u yourIGName -p yourIGPassword -t video

Next I installed https://github.com/tokland/youtube-upload by following the instructions in their readme.

wget https://github.com/tokland/youtube-upload/archive/master.zip
unzip master.zip
cd youtube-upload-master
sudo python setup.py install
cd ..
rm -rf youtube-upload-master

There were also a number of steps (in their readme) to install the .youtube-upload-credentials.json file to /home/pi/.  The file will contain your specific Youtube upload credentials and should remain secret.

That which binds them in darkness

Finally I was ready to test a bash script that would glue these other pieces together.  What you see here is the end result of an afternoon’s work.  It doesn’t show the testing steps along the way, most of which were later removed for brevity.  Test first!  Don’t blow up your following with a crazy upload machine.

#!/bin/bash

# the folder where mp4s will be stored
DESTINATION=imakerobots
PASSWORD=yourPasswordHere
MYACCOUNT=yourAccountNameHere
TARGETACCOUNT=nameItHere

# instagram-scraper looks in $DESTINATION to see what videos you've got
# and only grabs newer videos. youtube-upload uploads indiscriminately.
# we need the list of old videos so we don't upload old stuff again.
# get the list of files already in $DESTINATION
shopt -s nullglob
fileList=($DESTINATION/*.mp4)
#echo $fileList

# grab the 10 newest instagram videos
env/bin/instagram_scraper $TARGETACCOUNT -u $MYACCOUNT -p $PASSWORD -t video --maximum 10 --template {datetime} --latest

# upload each new video to youtube
for filename in $DESTINATION/*.mp4; do
if [[ ! "${fileList[@]}" =~ "${filename}" ]]; then
#echo $filename is new
filename2="${filename##*/}"
filename3="${filename2%.*}"
youtube-upload --title "$filename3" "$filename" --privacy="private"
fi;
done

# delete all but the 3 newest files so we don't fill the drive.
ls $DESTINATION -1tr | head -n -3 | xargs -d '\n' rm -f --

Final Thoughts

  • I didn’t setup youtube-upload in env because I’m a python newb.  I can barely ask “where it the toilet” in parseltongue.
  • I did setup this script to run as a cron job once an hour, every hour.
  • videos will be private so they don’t smash your subscriber’s data plan.  I had them originally set to unlisted but youtube still told everyone!  Many ugly emojis were received.
  • videos will be titled in youtube with the date and time of their original publication.
  • if you make the mistake of publishing a few of them and then pulling older instagrams and publishing those in a second wave your videos will be all out of sequence.  “Ha, ha,” says Youtube, “No fix for you!”
  • hese scripts don’t email me if the process worked, failed, or what have you.  They could be way more robust.
  • Special thanks to the helpful people in IRC freenode channel #bash and #python for helping with the many (many) different versions of python.

As always please let me know if I missed a step, if this was helpful to you, and so on.